<?php
/**
 * Task-first CertaCart Home screen.
 *
 * @package CertaCart_EU_Product_Readiness
 */

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

$ccpr_current_page = 'home';
$ccpr_status       = $latest ? (string) $latest['status'] : '';
$ccpr_is_active    = in_array( $ccpr_status, array( 'queued', 'running' ), true );
$ccpr_is_complete  = 'completed' === $ccpr_status;
$ccpr_has_products = $ccpr_is_complete && 0 < (int) ( $latest['total_products'] ?? 0 );
$ccpr_has_scope    = $ccpr_has_products && 0 < (int) ( $latest['in_scope_products'] ?? 0 );
$ccpr_score_available = $latest && \CertaCart\EU_Product_Readiness\Scanner::is_score_available( $latest );
$ccpr_needs_scope_confirmation = $latest && \CertaCart\EU_Product_Readiness\Scanner::needs_scope_confirmation( $latest );
$ccpr_has_issues   = $ccpr_has_scope && ! empty( $tasks );
$ccpr_score        = $ccpr_is_complete ? (float) $latest['overall_score'] : 0;
$ccpr_home_title   = __( 'No supported information gaps found', 'certacart-eu-product-readiness' );
$ccpr_home_subtitle = __( 'All supported product information checks passed in the latest scan.', 'certacart-eu-product-readiness' );
if ( $ccpr_is_complete && ! $ccpr_has_products ) {
	$ccpr_home_title    = __( 'No published products found in this scan.', 'certacart-eu-product-readiness' );
	$ccpr_home_subtitle = __( 'Add or publish a product to build your product information action plan.', 'certacart-eu-product-readiness' );
} elseif ( $ccpr_is_complete && $ccpr_needs_scope_confirmation ) {
	$ccpr_home_title    = __( 'Your product information action plan is ready.', 'certacart-eu-product-readiness' );
	$ccpr_home_subtitle = __( 'Review the ranked tasks now. Confirm EU sales scope when you want a readiness score.', 'certacart-eu-product-readiness' );
} elseif ( $ccpr_is_complete && ! $ccpr_has_scope ) {
	$ccpr_home_title    = __( 'Choose which products you sell to the EU.', 'certacart-eu-product-readiness' );
	$ccpr_home_subtitle = __( 'No products were scored because EU sales scope is not set.', 'certacart-eu-product-readiness' );
} elseif ( $ccpr_has_issues ) {
	/* translators: %s: product information readiness percentage. */
	$ccpr_home_title    = sprintf( __( 'EU product information readiness: %s%%', 'certacart-eu-product-readiness' ), number_format_i18n( $ccpr_score, 1 ) );
	$ccpr_home_subtitle = __( 'Start with the task that moves the most products forward.', 'certacart-eu-product-readiness' );
}
$ccpr_is_cancelled = 'cancelled' === $ccpr_status;
?>
<div class="wrap ccpr-wrap" id="ccpr-dashboard">
	<?php require CCPR_PATH . 'templates/admin-navigation.php'; ?>

	<main class="ccpr-main">
		<?php if ( $needs_quick_start ) : ?>
			<section class="ccpr-hero ccpr-hero--onboarding">
				<img class="ccpr-hero-illustration" src="<?php echo esc_url( CCPR_URL . 'assets/images/readiness-scan.svg' ); ?>" alt="" width="96" height="96">
				<h1><?php esc_html_e( 'Check your EU product information', 'certacart-eu-product-readiness' ); ?></h1>
				<form class="ccpr-quick-start-form" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post">
					<input type="hidden" name="action" value="ccpr_quick_start">
					<?php wp_nonce_field( 'ccpr_quick_start' ); ?>
					<fieldset class="ccpr-scope-picker">
						<legend><strong><?php esc_html_e( 'Which products can EU customers buy?', 'certacart-eu-product-readiness' ); ?></strong></legend>
						<label><input type="radio" name="ccpr_scan_scope" value="yes" required> <span><?php esc_html_e( 'All published products for this scan', 'certacart-eu-product-readiness' ); ?></span></label>
						<label><input type="radio" name="ccpr_scan_scope" value="review" required> <span><?php esc_html_e( 'Not sure — scan without a score', 'certacart-eu-product-readiness' ); ?></span></label>
					</fieldset>
					<button type="submit" class="button button-primary button-hero"><?php esc_html_e( 'Scan my store', 'certacart-eu-product-readiness' ); ?></button>
				</form>
				<p class="ccpr-trust-note"><span class="dashicons dashicons-lock" aria-hidden="true"></span><?php esc_html_e( 'Read-only catalog check. Product fields stay unchanged until you apply a fix.', 'certacart-eu-product-readiness' ); ?></p>
			</section>
		<?php else : ?>
			<section class="ccpr-hero ccpr-home-state" id="ccpr-home-empty" <?php echo $latest ? 'hidden' : ''; ?>>
				<img class="ccpr-hero-illustration" src="<?php echo esc_url( CCPR_URL . 'assets/images/readiness-scan.svg' ); ?>" alt="" width="96" height="96">
				<h1><?php esc_html_e( 'Check your EU product information', 'certacart-eu-product-readiness' ); ?></h1>
				<p class="ccpr-lead"><?php esc_html_e( 'Scan your catalog and get a ranked action plan.', 'certacart-eu-product-readiness' ); ?></p>
				<button type="button" class="button button-primary button-hero" id="ccpr-start-scan"><?php esc_html_e( 'Scan my store', 'certacart-eu-product-readiness' ); ?></button>
				<p class="ccpr-trust-note"><span class="dashicons dashicons-lock" aria-hidden="true"></span><?php esc_html_e( 'Read-only catalog check. Product fields stay unchanged until you apply a fix.', 'certacart-eu-product-readiness' ); ?></p>
			</section>

			<section class="ccpr-home-state ccpr-scan-state" id="ccpr-home-scanning" <?php echo $ccpr_is_active ? '' : 'hidden'; ?> aria-live="polite">
				<div class="ccpr-page-heading">
					<div><p class="ccpr-eyebrow"><?php esc_html_e( 'Building your action plan', 'certacart-eu-product-readiness' ); ?></p><h1><?php esc_html_e( 'Scanning your store…', 'certacart-eu-product-readiness' ); ?></h1><p><?php esc_html_e( 'We’re checking your catalog and finding the fastest path forward.', 'certacart-eu-product-readiness' ); ?></p></div>
					<button type="button" class="button" id="ccpr-cancel-scan"><?php esc_html_e( 'Stop scan', 'certacart-eu-product-readiness' ); ?></button>
				</div>
				<div class="ccpr-progress-layout">
					<div class="ccpr-progress-ring" id="ccpr-progress-ring" style="--ccpr-progress:0"><strong id="ccpr-progress-number">0%</strong></div>
					<div class="ccpr-progress-copy">
						<strong id="ccpr-status-label"><?php esc_html_e( 'Scanning your store…', 'certacart-eu-product-readiness' ); ?></strong>
						<span id="ccpr-status-detail"></span>
						<div class="ccpr-progress-track" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" id="ccpr-progress-track"><span id="ccpr-progress-bar"></span></div>
						<p class="ccpr-keep-open"><?php esc_html_e( 'Keep this page open while CertaCart finishes.', 'certacart-eu-product-readiness' ); ?></p>
					</div>
				</div>
			</section>

			<section class="ccpr-home-state" id="ccpr-home-complete" <?php echo $ccpr_is_complete ? '' : 'hidden'; ?>>
				<div class="ccpr-page-heading">
					<div>
						<p class="ccpr-eyebrow"><?php esc_html_e( 'Latest scan complete', 'certacart-eu-product-readiness' ); ?></p>
						<h1 id="ccpr-home-title"><?php echo esc_html( $ccpr_home_title ); ?></h1>
						<p id="ccpr-home-subtitle"><?php echo esc_html( $ccpr_home_subtitle ); ?></p>
					</div>
					<a class="button button-primary" id="ccpr-add-product" href="<?php echo esc_url( admin_url( 'post-new.php?post_type=product' ) ); ?>" <?php echo $ccpr_has_products ? 'hidden' : ''; ?>><?php esc_html_e( 'Add product', 'certacart-eu-product-readiness' ); ?></a>
					<a class="button button-primary" id="ccpr-set-scope" href="<?php echo esc_url( admin_url( 'admin.php?page=ccpr-settings' ) ); ?>" <?php echo $ccpr_has_products && $ccpr_needs_scope_confirmation ? '' : 'hidden'; ?>><?php esc_html_e( 'Confirm EU sales scope', 'certacart-eu-product-readiness' ); ?></a>
					<button type="button" class="button" id="ccpr-start-scan-complete" <?php echo $ccpr_has_scope ? '' : 'hidden'; ?>><?php esc_html_e( 'Scan again', 'certacart-eu-product-readiness' ); ?></button>
				</div>

				<div class="ccpr-home-overview">
					<div class="ccpr-score-card">
						<div class="ccpr-score-ring" id="ccpr-score-ring" style="--ccpr-score:<?php echo esc_attr( $ccpr_score_available ? (string) $ccpr_score : '0' ); ?>"><strong id="ccpr-score"><?php echo $ccpr_score_available ? esc_html( number_format_i18n( $ccpr_score, 1 ) ) . '%' : esc_html__( 'N/A', 'certacart-eu-product-readiness' ); ?></strong></div>
						<div><span><?php esc_html_e( 'EU product information readiness', 'certacart-eu-product-readiness' ); ?></span><p><?php esc_html_e( 'Based on CertaCart’s supported checks', 'certacart-eu-product-readiness' ); ?></p></div>
					</div>

					<article class="ccpr-best-action" id="ccpr-best-action" <?php echo $ccpr_has_issues ? '' : 'hidden'; ?>>
						<p class="ccpr-eyebrow"><?php esc_html_e( 'Best next action', 'certacart-eu-product-readiness' ); ?></p>
						<h2 id="ccpr-best-task-title"><?php echo $ccpr_has_issues ? esc_html( $tasks[0]['title'] ) : ''; ?></h2>
						<p id="ccpr-best-task-reason"><?php echo $ccpr_has_issues ? esc_html( $tasks[0]['reason'] ) : ''; ?></p>
						<div class="ccpr-task-meta"><span><strong id="ccpr-best-task-impact"><?php echo $ccpr_has_issues ? esc_html( number_format_i18n( (int) $tasks[0]['affected_products'] ) ) : '0'; ?></strong> <?php esc_html_e( 'products affected', 'certacart-eu-product-readiness' ); ?></span><span id="ccpr-best-task-confidence"><?php echo $ccpr_has_issues && ! empty( $tasks[0]['confidence'] ) ? ( 'high' === $tasks[0]['confidence'] ? esc_html__( 'High detection confidence', 'certacart-eu-product-readiness' ) : esc_html__( 'Merchant review needed', 'certacart-eu-product-readiness' ) ) : ''; ?></span></div>
						<a class="button button-primary" id="ccpr-best-task-link" href="<?php echo $ccpr_has_issues ? esc_url( admin_url( 'admin.php?page=ccpr-tasks&task=' . rawurlencode( $tasks[0]['rule'] ) ) ) : '#'; ?>"><?php esc_html_e( 'Start this task', 'certacart-eu-product-readiness' ); ?></a>
					</article>
				</div>

				<div class="ccpr-metrics" id="ccpr-summary-grid">
					<div><span><?php esc_html_e( 'Products scanned', 'certacart-eu-product-readiness' ); ?></span><strong id="ccpr-processed"><?php echo esc_html( number_format_i18n( (int) ( $latest['processed_products'] ?? 0 ) ) ); ?></strong></div>
					<div><span><?php esc_html_e( 'Unique products affected', 'certacart-eu-product-readiness' ); ?></span><strong id="ccpr-affected"><?php echo esc_html( number_format_i18n( (int) $affected_products ) ); ?></strong></div>
					<div><span><?php esc_html_e( 'Action tasks', 'certacart-eu-product-readiness' ); ?></span><strong id="ccpr-task-count"><?php echo esc_html( number_format_i18n( $task_count ) ); ?></strong></div>
					<div><span><?php esc_html_e( 'High-confidence tasks', 'certacart-eu-product-readiness' ); ?></span><strong id="ccpr-high-confidence"><?php echo esc_html( number_format_i18n( $high_confidence_task_count ) ); ?></strong></div>
				</div>

				<div class="ccpr-home-footer-actions"><a href="<?php echo esc_url( admin_url( 'admin.php?page=ccpr-tasks' ) ); ?>"><?php esc_html_e( 'View all tasks', 'certacart-eu-product-readiness' ); ?> →</a><a href="<?php echo esc_url( admin_url( 'admin.php?page=ccpr-history' ) ); ?>"><?php esc_html_e( 'View scan history', 'certacart-eu-product-readiness' ); ?></a></div>
			</section>

			<section class="ccpr-home-state ccpr-error-state" id="ccpr-home-error" <?php echo $latest && in_array( $ccpr_status, array( 'failed', 'cancelled' ), true ) ? '' : 'hidden'; ?>>
				<span class="dashicons dashicons-warning" aria-hidden="true"></span><h1 id="ccpr-error-title"><?php echo $ccpr_is_cancelled ? esc_html__( 'Scan stopped.', 'certacart-eu-product-readiness' ) : esc_html__( 'The scan was interrupted.', 'certacart-eu-product-readiness' ); ?></h1><p id="ccpr-error-detail"><?php esc_html_e( 'Your product data was not changed.', 'certacart-eu-product-readiness' ); ?></p><details id="ccpr-error-technical" <?php echo ! $ccpr_is_cancelled && ! empty( $latest['error_message'] ) ? '' : 'hidden'; ?>><summary><?php esc_html_e( 'Show technical details', 'certacart-eu-product-readiness' ); ?></summary><code id="ccpr-error-message"><?php echo esc_html( (string) ( $latest['error_message'] ?? '' ) ); ?></code></details><button type="button" class="button button-primary" id="ccpr-start-scan-error"><?php esc_html_e( 'Try again', 'certacart-eu-product-readiness' ); ?></button>
			</section>
		<?php endif; ?>

		<?php do_action( 'ccpr_after_dashboard' ); ?>
	</main>
</div>
