<?php
/**
 * File Path: templates/admin/tab-email.php
 *
 * Email / Newsletter Settings Tab -- HTML partial (R-011)
 *
 * Loaded via mbs_get_template_part( 'admin/tab-email', [...] ).
 * Only rendered when mbs_is_pro() -- gate enforced in MBS_Tab_Email::render().
 *
 * Variables injected by MBS_Tab_Email::render():
 *   @var string $platform            Active ESP slug.
 *   @var array  $platforms           All platform slug => label pairs.
 *   @var array  $url_platforms       Platform slugs that require an Account URL field.
 *   @var bool   $is_url_platform     Whether the active platform needs an Account URL.
 *   @var string $default_list_id     Saved default list ID.
 *   @var string $arc_list_id         Saved ARC reviewer list ID.
 *   @var string $broadcast_list_id   Saved broadcast list ID.
 *   @var string $double_optin        '1' | '0'.
 *   @var string $consent_text        Saved consent label text.
 *   @var string $widget_enabled      '1' | '0'.
 *   @var string $arc_sync_enabled    '1' | '0'.
 *   @var string $broadcast_enabled   '1' | '0'.
 *   @var string $tag_book            '1' | '0'.
 *   @var string $tag_series          '1' | '0'.
 *   @var string $tag_genre           '1' | '0'.
 *   @var bool   $has_key             Whether an API key is already stored.
 *   @var string $key_placeholder     Input placeholder text for the API key field.
 *   @var bool   $has_url             Whether an Account URL is already stored.
 *   @var string $url_placeholder     Input placeholder text for the Account URL field.
 *   @var string $mbs_sub_url         Subscriber list URL for the active platform (may be empty).
 *   @var string $mbs_auto_url        Automation URL for the active platform (may be empty).
 *
 * @package    My_Book_Showroom
 * @subpackage Templates/Admin
 * @since      1.1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment -- all printf() calls in this template have translator comments on the immediately preceding line; PHPCS cannot associate comments with esc_html__() calls inside multi-line printf()
?>
<div class="mbs-tab-email">
	<?php wp_nonce_field( 'mbs_email_tab_save', 'mbs_email_tab_nonce' ); ?>
	<input type="hidden" name="mbs_email_tab_submit" value="1">

	<?php /* Section A: Platform */ ?>
	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'Platform', 'my-book-showroom' ); ?></h2>
	<table class="form-table">
		<tr>
			<th scope="row">
				<label for="mbs_email_platform"><?php esc_html_e( 'Email Platform', 'my-book-showroom' ); ?></label>
			</th>
			<td>
				<select id="mbs_email_platform" name="mbs_email_platform" class="regular-text mbs-esp-platform-select">
					<?php foreach ( $platforms as $slug => $label ) : // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global?>
						<option value="<?php echo esc_attr( $slug ); ?>"<?php selected( $platform, $slug ); ?>>
							<?php echo esc_html( $label ); ?>
						</option>
					<?php endforeach; ?>
				</select>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_email_api_key"><?php esc_html_e( 'API Key', 'my-book-showroom' ); ?></label></th>
			<td>
				<input type="password" id="mbs_email_api_key" name="mbs_email_api_key"
					class="regular-text"
					placeholder="<?php echo esc_attr( $key_placeholder ); ?>"
					autocomplete="off"
				       autocomplete="new-password">
				<p class="description"><?php esc_html_e( 'Leave blank to keep the existing key.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<tr class="mbs-esp-url-row" <?php echo $is_url_platform ? '' : 'style="display:none;"'; ?>>
			<th scope="row"><label for="mbs_email_account_url"><?php esc_html_e( 'Account URL', 'my-book-showroom' ); ?></label></th>
			<td>
				<input type="text" id="mbs_email_account_url" name="mbs_email_account_url"
					class="regular-text"
					placeholder="<?php echo esc_attr( $url_placeholder ); ?>"
				       autocomplete="off">
				<p class="description"><?php esc_html_e( 'Required for Sendy and ActiveCampaign. Leave blank to keep the existing URL.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Connection', 'my-book-showroom' ); ?></th>
			<td>
				<button type="button" class="button mbs-esp-test-btn" id="mbs-esp-test-btn">
					<?php esc_html_e( 'Test Connection', 'my-book-showroom' ); ?>
				</button>
				<span class="mbs-esp-test-status" id="mbs-esp-test-status"></span>
				<p class="description mbs-esp-sendy-no-tag-notice" <?php echo 'sendy' === $platform ? '' : 'style="display:none;"'; ?>>
					<?php esc_html_e( 'Sendy does not support tags -- the auto-tagging settings below will have no effect.', 'my-book-showroom' ); ?>
				</p>
			</td>
		</tr>
	</table>

	<?php /* Section B: Lists */ ?>
	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'Lists', 'my-book-showroom' ); ?></h2>
	<table class="form-table">
		<tr>
			<th scope="row"><label for="mbs_email_default_list_id"><?php esc_html_e( 'Default List', 'my-book-showroom' ); ?></label></th>
			<td>
				<select id="mbs_email_default_list_id" name="mbs_email_default_list_id" class="regular-text mbs-esp-list-select">
					<?php if ( '' !== $default_list_id ) : ?>
						<option value="<?php echo esc_attr( $default_list_id ); ?>" selected>
							<?php echo esc_html( $default_list_id ); ?>
						</option>
					<?php else : ?>
						<option value=""><?php esc_html_e( '-- connect first --', 'my-book-showroom' ); ?></option>
					<?php endif; ?>
				</select>
				<p class="description"><?php esc_html_e( 'Used by the reader signup widget and new release broadcasts.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_email_arc_list_id"><?php esc_html_e( 'ARC Reviewer List', 'my-book-showroom' ); ?></label></th>
			<td>
				<select id="mbs_email_arc_list_id" name="mbs_email_arc_list_id" class="regular-text mbs-esp-list-select">
					<?php if ( '' !== $arc_list_id ) : ?>
						<option value="<?php echo esc_attr( $arc_list_id ); ?>" selected>
							<?php echo esc_html( $arc_list_id ); ?>
						</option>
					<?php else : ?>
						<option value=""><?php esc_html_e( '-- connect first --', 'my-book-showroom' ); ?></option>
					<?php endif; ?>
				</select>
				<p class="description"><?php esc_html_e( 'Reviewers added in the ARC metabox are synced to this list.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_email_broadcast_list_id"><?php esc_html_e( 'Broadcast List', 'my-book-showroom' ); ?></label></th>
			<td>
				<select id="mbs_email_broadcast_list_id" name="mbs_email_broadcast_list_id" class="regular-text mbs-esp-list-select">
					<?php if ( '' !== $broadcast_list_id ) : ?>
						<option value="<?php echo esc_attr( $broadcast_list_id ); ?>" selected>
							<?php echo esc_html( $broadcast_list_id ); ?>
						</option>
					<?php else : ?>
						<option value=""><?php esc_html_e( '-- connect first --', 'my-book-showroom' ); ?></option>
					<?php endif; ?>
				</select>
				<p class="description"><?php esc_html_e( 'Used for new release broadcast emails.', 'my-book-showroom' ); ?></p>
				<?php if ( '' !== $mbs_sub_url ) : ?>
				<p class="description">
					<a href="<?php echo esc_url( $mbs_sub_url ); ?>" target="_blank" rel="noopener">
						<?php
						/* translators: %s: platform display name */
						printf( esc_html__( 'View subscribers in %s', 'my-book-showroom' ),
							esc_html( MBS_ESP_Registry::get_platform_label( MBS_ESP_Registry::get_platform() ) ) );
						?>
					</a>
				</p>
				<?php endif; ?>
			</td>
		</tr>
	</table>

	<?php /* Section C: Compliance */ ?>
	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'Compliance', 'my-book-showroom' ); ?></h2>
	<table class="form-table">
		<tr>
			<th scope="row"><?php esc_html_e( 'Double Opt-in', 'my-book-showroom' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="mbs_email_double_optin" value="1"
						<?php checked( '1', $double_optin ); ?>>
					<?php esc_html_e( 'Use double opt-in (recommended for GDPR / CASL compliance)', 'my-book-showroom' ); ?>
				</label>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_email_consent_text"><?php esc_html_e( 'Consent Text', 'my-book-showroom' ); ?></label></th>
			<td>
				<textarea id="mbs_email_consent_text" name="mbs_email_consent_text" class="large-text" rows="3"><?php echo esc_textarea( $consent_text ); ?></textarea>
				<p class="description"><?php esc_html_e( 'Label shown next to the consent checkbox on the front-end signup form. One link tag allowed.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
	</table>

	<?php /* Section D: Features */ ?>
	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'Features', 'my-book-showroom' ); ?></h2>
	<table class="form-table">
		<tr>
			<th scope="row"><?php esc_html_e( 'Show subscriber signup widget on book and author pages', 'my-book-showroom' ); ?></th>
			<td><label>
				<input type="checkbox" name="mbs_email_widget_enabled" value="1"
					<?php checked( '1', $widget_enabled ); ?>>
				<?php esc_html_e( 'Show subscriber signup widget on book and author pages', 'my-book-showroom' ); ?>
			</label></td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Automatically sync ARC reviewers to email list', 'my-book-showroom' ); ?></th>
			<td><label>
				<input type="checkbox" name="mbs_email_arc_sync_enabled" value="1"
					<?php checked( '1', $arc_sync_enabled ); ?>>
				<?php esc_html_e( 'Automatically sync ARC reviewers to email list', 'my-book-showroom' ); ?>
			</label></td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Enable new release broadcast queue', 'my-book-showroom' ); ?></th>
			<td><label>
				<input type="checkbox" name="mbs_email_broadcast_enabled" value="1"
					<?php checked( '1', $broadcast_enabled ); ?>>
				<?php esc_html_e( 'Enable new release broadcast queue', 'my-book-showroom' ); ?>
			</label></td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Drip &amp; automation sequences', 'my-book-showroom' ); ?></th>
			<td>
				<?php if ( '' !== $mbs_auto_url ) : ?>
				<p class="description">
					<?php
					/* translators: 1: platform name, 2: anchor open, 3: anchor close */
					printf( // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment -- comment on preceding line
						esc_html__( 'Welcome sequences and drip automations are managed directly in %1$s. %2$sSet up automations%3$s.', 'my-book-showroom' ),
						esc_html( MBS_ESP_Registry::get_platform_label( MBS_ESP_Registry::get_platform() ) ),
						'<a href="' . esc_url( $mbs_auto_url ) . '" target="_blank" rel="noopener">',
						'</a>'
					);
					?>
				</p>
				<?php else : ?>
				<p class="description">
					<?php esc_html_e( 'Welcome sequences and drip automations are configured directly in your email platform. Connect your platform above to see the link.', 'my-book-showroom' ); ?>
				</p>
				<?php endif; ?>
			</td>
		</tr>
	</table>

	<h3><?php esc_html_e( 'Tags', 'my-book-showroom' ); ?></h3>
	<table class="form-table">
		<tr>
			<th scope="row"><?php esc_html_e( 'Auto-tag Strategy', 'my-book-showroom' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="mbs_email_tag_book" value="1"
						<?php checked( '1', $tag_book ); ?>>
					<?php esc_html_e( 'Apply book title as tag', 'my-book-showroom' ); ?>
				</label><br>
				<label>
					<input type="checkbox" name="mbs_email_tag_series" value="1"
						<?php checked( '1', $tag_series ); ?>>
					<?php esc_html_e( 'Apply series name as tag', 'my-book-showroom' ); ?>
				</label><br>
				<label>
					<input type="checkbox" name="mbs_email_tag_genre" value="1"
						<?php checked( '1', $tag_genre ); ?>>
					<?php esc_html_e( 'Apply primary genre as tag', 'my-book-showroom' ); ?>
				</label><br>
			</td>
		</tr>
	</table>

	<?php /* Section E: ARC Signup CAPTCHA */ ?>
	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'ARC Signup CAPTCHA', 'my-book-showroom' ); ?></h2>
	<p class="description"><?php esc_html_e( 'Protect the ARC reviewer self-signup form from bots. Leave set to None to disable CAPTCHA.', 'my-book-showroom' ); ?></p>
	<table class="form-table">
		<tr>
			<th scope="row"><label for="mbs_email_captcha_provider"><?php esc_html_e( 'CAPTCHA Provider', 'my-book-showroom' ); ?></label></th>
			<td>
				<select id="mbs_email_captcha_provider" name="mbs_email_captcha_provider" class="mbs-captcha-provider-select">
					<option value="" <?php selected( '', $captcha_provider ); ?>><?php esc_html_e( 'None', 'my-book-showroom' ); ?></option>
					<option value="recaptcha_v3" <?php selected( 'recaptcha_v3', $captcha_provider ); ?>><?php esc_html_e( 'reCAPTCHA v3 (Google)', 'my-book-showroom' ); ?></option>
					<option value="hcaptcha" <?php selected( 'hcaptcha', $captcha_provider ); ?>><?php esc_html_e( 'hCaptcha', 'my-book-showroom' ); ?></option>
					<option value="turnstile" <?php selected( 'turnstile', $captcha_provider ); ?>><?php esc_html_e( 'Cloudflare Turnstile', 'my-book-showroom' ); ?></option>
				</select>
			</td>
		</tr>
	</table>

	<?php /* reCAPTCHA v3 key pair -- shown/hidden by admin-email-tab.js */ ?>
	<div class="mbs-captcha-keys-section" data-provider="recaptcha_v3"<?php echo 'recaptcha_v3' !== $captcha_provider ? ' style="display:none"' : ''; ?>>
		<h3><?php esc_html_e( 'reCAPTCHA v3 Keys', 'my-book-showroom' ); ?></h3>
		<p class="description">
			<?php
			/* translators: link to Google reCAPTCHA admin */
			printf( // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment -- comment on preceding line
				esc_html__( 'Get your keys from the %1$sGoogle reCAPTCHA admin console%2$s.', 'my-book-showroom' ),
				'<a href="https://www.google.com/recaptcha/admin" target="_blank" rel="noopener noreferrer">',
				'</a>'
			);
			?>
		</p>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="mbs_email_captcha_recaptcha_v3_site_key"><?php esc_html_e( 'Site Key', 'my-book-showroom' ); ?></label></th>
				<td><input type="text" id="mbs_email_captcha_recaptcha_v3_site_key" name="mbs_email_captcha_recaptcha_v3_site_key" class="regular-text"
						placeholder="<?php echo esc_attr( ! empty( $captcha_key_hints['recaptcha_v3_site'] ) ? __( '(saved -- enter new key to replace)', 'my-book-showroom' ) : '' ); ?>"
				       autocomplete="off"></td>
			</tr>
			<tr>
				<th scope="row"><label for="mbs_email_captcha_recaptcha_v3_secret_key"><?php esc_html_e( 'Secret Key', 'my-book-showroom' ); ?></label></th>
				<td><input type="password" id="mbs_email_captcha_recaptcha_v3_secret_key" name="mbs_email_captcha_recaptcha_v3_secret_key" class="regular-text"
						placeholder="<?php echo esc_attr( ! empty( $captcha_key_hints['recaptcha_v3_secret'] ) ? __( '(saved -- enter new key to replace)', 'my-book-showroom' ) : '' ); ?>"
				       autocomplete="new-password"></td>
			</tr>
		</table>
	</div>

	<?php /* hCaptcha key pair */ ?>
	<div class="mbs-captcha-keys-section" data-provider="hcaptcha"<?php echo 'hcaptcha' !== $captcha_provider ? ' style="display:none"' : ''; ?>>
		<h3><?php esc_html_e( 'hCaptcha Keys', 'my-book-showroom' ); ?></h3>
		<p class="description">
			<?php
			/* translators: 1: anchor tag opening for hCaptcha dashboard link, 2: anchor closing tag */
			printf(
				esc_html__( 'Get your keys from the %1$shCaptcha dashboard%2$s.', 'my-book-showroom' ),
				'<a href="https://dashboard.hcaptcha.com/sites" target="_blank" rel="noopener noreferrer">',
				'</a>'
			);
			?>
		</p>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="mbs_email_captcha_hcaptcha_site_key"><?php esc_html_e( 'Site Key', 'my-book-showroom' ); ?></label></th>
				<td><input type="text" id="mbs_email_captcha_hcaptcha_site_key" name="mbs_email_captcha_hcaptcha_site_key" class="regular-text"
						placeholder="<?php echo esc_attr( ! empty( $captcha_key_hints['hcaptcha_site'] ) ? __( '(saved -- enter new key to replace)', 'my-book-showroom' ) : '' ); ?>"
				       autocomplete="off"></td>
			</tr>
			<tr>
				<th scope="row"><label for="mbs_email_captcha_hcaptcha_secret_key"><?php esc_html_e( 'Secret Key', 'my-book-showroom' ); ?></label></th>
				<td><input type="password" id="mbs_email_captcha_hcaptcha_secret_key" name="mbs_email_captcha_hcaptcha_secret_key" class="regular-text"
						placeholder="<?php echo esc_attr( ! empty( $captcha_key_hints['hcaptcha_secret'] ) ? __( '(saved -- enter new key to replace)', 'my-book-showroom' ) : '' ); ?>"
				       autocomplete="new-password"></td>
			</tr>
		</table>
	</div>

	<?php /* Cloudflare Turnstile key pair */ ?>
	<div class="mbs-captcha-keys-section" data-provider="turnstile"<?php echo 'turnstile' !== $captcha_provider ? ' style="display:none"' : ''; ?>>
		<h3><?php esc_html_e( 'Cloudflare Turnstile Keys', 'my-book-showroom' ); ?></h3>
		<p class="description">
			<?php
			/* translators: 1: anchor tag opening for Cloudflare Turnstile dashboard link, 2: anchor closing tag */
			printf(
				esc_html__( 'Get your keys from the %1$sCloudflare Turnstile dashboard%2$s.', 'my-book-showroom' ),
				'<a href="https://dash.cloudflare.com/?to=/:account/turnstile" target="_blank" rel="noopener noreferrer">',
				'</a>'
			);
			?>
		</p>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="mbs_email_captcha_turnstile_site_key"><?php esc_html_e( 'Site Key', 'my-book-showroom' ); ?></label></th>
				<td><input type="text" id="mbs_email_captcha_turnstile_site_key" name="mbs_email_captcha_turnstile_site_key" class="regular-text"
						placeholder="<?php echo esc_attr( ! empty( $captcha_key_hints['turnstile_site'] ) ? __( '(saved -- enter new key to replace)', 'my-book-showroom' ) : '' ); ?>"
				       autocomplete="off"></td>
			</tr>
			<tr>
				<th scope="row"><label for="mbs_email_captcha_turnstile_secret_key"><?php esc_html_e( 'Secret Key', 'my-book-showroom' ); ?></label></th>
				<td><input type="password" id="mbs_email_captcha_turnstile_secret_key" name="mbs_email_captcha_turnstile_secret_key" class="regular-text"
						placeholder="<?php echo esc_attr( ! empty( $captcha_key_hints['turnstile_secret'] ) ? __( '(saved -- enter new key to replace)', 'my-book-showroom' ) : '' ); ?>"
				       autocomplete="new-password"></td>
			</tr>
		</table>
	</div>

	<?php /* Section F: ARC Signup Notifications */ ?>
	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'ARC Signup Notifications', 'my-book-showroom' ); ?></h2>
	<table class="form-table">
		<tr>
			<th scope="row"><?php esc_html_e( 'Email notification on new application', 'my-book-showroom' ); ?></th>
			<td><label>
				<input type="checkbox" name="mbs_arc_notify_enabled" value="1"
					<?php checked( '1', $arc_notify_enabled ); ?>>
				<?php esc_html_e( 'Send me an email when a new ARC signup application is submitted', 'my-book-showroom' ); ?>
			</label></td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_arc_notify_email"><?php esc_html_e( 'Notification email address', 'my-book-showroom' ); ?></label></th>
			<td>
				<input type="email" id="mbs_arc_notify_email" name="mbs_arc_notify_email" class="regular-text"
					value="<?php echo esc_attr( $arc_notify_email ); ?>"
					placeholder="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>"
				       autocomplete="off">
				<p class="description"><?php esc_html_e( 'Leave blank to use the site admin email address.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
	</table>

	<p class="submit">
		<?php submit_button( null, 'primary', 'mbs_email_save_submit', false ); ?>
	</p>
</div>
