<?php
/**
 * Settings tab - Email Reports.
 *
 * Variable: $options (Options instance)
 *
 * @package Relatedly
 * @since   2.0.0
 * @since   2.3.0 Rebuilt with native form rows.
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
defined( 'ABSPATH' ) || exit;

use Relatedly\Premium\FeatureGate;
use Relatedly\Premium\LicenseManager;

if ( ! FeatureGate::can_use_email_reports() ) :
	$upgrade_url = LicenseManager::checkout_url();
	?>
	<div class="relatedly-settings-panel">
		<div class="relatedly-settings-panel-header">
			<h2 class="relatedly-settings-panel-title"><?php esc_html_e( 'Email Reports', 'relatedly' ); ?></h2>
			<p class="relatedly-settings-panel-desc"><?php esc_html_e( 'Scheduled performance reports delivered to your inbox.', 'relatedly' ); ?></p>
		</div>
		<div class="relatedly-settings-panel-body">
			<div class="relatedly-upgrade-panel">
				<h3><?php esc_html_e( 'Email Reports are available in Pro', 'relatedly' ); ?></h3>
				<p><?php esc_html_e( 'Send weekly or monthly summaries with clicks, impressions, and top posts.', 'relatedly' ); ?></p>
				<a class="button button-primary" href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener noreferrer">
					<?php esc_html_e( 'View Plans', 'relatedly' ); ?>
				</a>
			</div>
		</div>
	</div>
	<?php
	return;
endif;

$recipients = (array) $options->get( 'email_reports_recipients', [] );
?>
<div class="relatedly-settings-panel">
	<div class="relatedly-settings-panel-header">
		<h2 class="relatedly-settings-panel-title"><?php esc_html_e( 'Email Reports', 'relatedly' ); ?></h2>
		<p class="relatedly-settings-panel-desc"><?php esc_html_e( 'Choose when Relatedly sends performance emails and who receives them.', 'relatedly' ); ?></p>
	</div>

	<div class="relatedly-settings-panel-body">
		<table class="form-table relatedly-settings-table">
			<tbody>
				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-email-enabled"><?php esc_html_e( 'Report Delivery', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Send scheduled performance reports by email.', 'relatedly' ); ?></p>
					</th>
					<td>
						<div class="relatedly-toggle-row">
							<div class="relatedly-toggle-switch">
								<input type="checkbox" name="email_reports_enabled" id="relatedly-email-enabled" value="1" <?php checked( $options->get( 'email_reports_enabled', false ) ); ?>>
								<label for="relatedly-email-enabled"></label>
							</div>
						</div>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-email-frequency"><?php esc_html_e( 'Frequency', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Choose how often reports are delivered.', 'relatedly' ); ?></p>
					</th>
					<td>
						<select name="email_reports_frequency" id="relatedly-email-frequency" class="relatedly-control">
							<option value="weekly" <?php selected( $options->get( 'email_reports_frequency', 'weekly' ), 'weekly' ); ?>><?php esc_html_e( 'Weekly', 'relatedly' ); ?></option>
							<option value="monthly" <?php selected( $options->get( 'email_reports_frequency', 'weekly' ), 'monthly' ); ?>><?php esc_html_e( 'Monthly', 'relatedly' ); ?></option>
						</select>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-email-recipients"><?php esc_html_e( 'Recipients', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Enter one email address per line.', 'relatedly' ); ?></p>
					</th>
					<td>
						<textarea name="email_reports_recipients" id="relatedly-email-recipients" class="relatedly-control relatedly-textarea" rows="5"><?php echo esc_textarea( implode( "\n", $recipients ) ); ?></textarea>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<span class="relatedly-field-label"><?php esc_html_e( 'Report Content', 'relatedly' ); ?></span>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Choose what appears in each email.', 'relatedly' ); ?></p>
					</th>
					<td>
						<div class="relatedly-checkbox-list relatedly-checkbox-list-auto">
							<label class="relatedly-checkbox-item">
								<input type="checkbox" name="email_reports_include_summary" value="1" <?php checked( $options->get( 'email_reports_include_summary', true ) ); ?>>
								<?php esc_html_e( 'Include clicks, views, and click-rate summary', 'relatedly' ); ?>
							</label>
							<label class="relatedly-checkbox-item">
								<input type="checkbox" name="email_reports_include_top_posts" value="1" <?php checked( $options->get( 'email_reports_include_top_posts', true ) ); ?>>
								<?php esc_html_e( 'Include top clicked posts table', 'relatedly' ); ?>
							</label>
							<label class="relatedly-checkbox-item">
								<input type="checkbox" name="email_reports_include_comparison" value="1" <?php checked( $options->get( 'email_reports_include_comparison', false ) ); ?>>
								<?php esc_html_e( 'Include period comparison', 'relatedly' ); ?>
							</label>
						</div>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-email-count"><?php esc_html_e( 'Top Posts Count', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Maximum top posts included in each report.', 'relatedly' ); ?></p>
					</th>
					<td>
						<input type="number" name="email_reports_top_posts_count" id="relatedly-email-count" class="relatedly-control" min="1" max="50" value="<?php echo esc_attr( $options->get( 'email_reports_top_posts_count', 10 ) ); ?>">
					</td>
				</tr>
			</tbody>
		</table>
	</div>

	<div class="relatedly-save-bar">
		<?php submit_button( __( 'Save Changes', 'relatedly' ), 'primary', 'submit', false ); ?>
	</div>
</div>
