<?php
/**
 * Reports page.
 *
 * @package WPLens
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
// phpcs:disable WordPress.Security.NonceVerification.Recommended

$settings    = \WPLens\Utils\Options::all();
$recipients  = (array) ( $settings['reporting']['recipients'] ?? array() );
$preview_url = wp_nonce_url( admin_url( 'admin-post.php?action=wplens_report_preview' ), 'wplens_report_preview' );
?>
<div class="wrap lens-wrap">
	<h1>
		<img class="lens-icon" src="<?php echo esc_url( WPLENS_PLUGIN_URL . 'assets/icon.svg' ); ?>" alt="" />
		<?php esc_html_e( 'Weekly Reports', 'lens-ai-visibility-monitor' ); ?>
	</h1>
	<?php settings_errors( 'lens-ai-visibility-monitor' ); ?>

	<div class="lens-card">
		<header style="margin-bottom:12px;"><h2 style="margin:0;font-size:18px;"><?php esc_html_e( 'Weekly AI Visibility Report', 'lens-ai-visibility-monitor' ); ?></h2></header>
		<p style="color:#50575e;">
			<?php esc_html_e( 'A printable HTML summary of AI bot activity, top crawled pages, blind spots, and citation status. Sent every Monday morning if enabled.', 'lens-ai-visibility-monitor' ); ?>
		</p>

		<p>
			<strong><?php esc_html_e( 'Recipients', 'lens-ai-visibility-monitor' ); ?>:</strong>
			<?php if ( empty( $recipients ) ) : ?>
				<em><?php esc_html_e( 'No recipients configured.', 'lens-ai-visibility-monitor' ); ?></em>
				&nbsp;<a class="lens-btn ghost" href="<?php echo esc_url( admin_url( 'admin.php?page=lens-settings' ) ); ?>"><?php esc_html_e( 'Add one →', 'lens-ai-visibility-monitor' ); ?></a>
			<?php else : ?>
				<?php echo esc_html( implode( ', ', array_map( 'sanitize_email', $recipients ) ) ); ?>
			<?php endif; ?>
		</p>

		<p style="display:flex;gap:8px;margin-top:16px;">
			<a class="lens-btn" href="<?php echo esc_url( $preview_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Preview the report', 'lens-ai-visibility-monitor' ); ?></a>
			<form method="post" style="display:inline">
				<?php wp_nonce_field( 'wplens_send_test' ); ?>
				<button type="submit" name="wplens_send_test" class="lens-btn primary" <?php disabled( empty( $recipients ) ); ?>><?php esc_html_e( 'Send test email now', 'lens-ai-visibility-monitor' ); ?></button>
			</form>
		</p>

		<p class="description" style="font-size:12px;margin-top:16px;">
			<?php esc_html_e( 'Reports are generated as print-friendly HTML. Use your browser\'s "Print to PDF" command to save a PDF copy of any preview. Native PDF export is on the v1.1 roadmap.', 'lens-ai-visibility-monitor' ); ?>
		</p>
	</div>
</div>
