<?php
/**
 * Scan and action history.
 *
 * @package CertaCart_EU_Product_Readiness
 */

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

$ccpr_current_page = 'history';
?>
<div class="wrap ccpr-wrap" id="ccpr-history-page">
	<?php require CCPR_PATH . 'templates/admin-navigation.php'; ?>
	<main class="ccpr-main">
		<div class="ccpr-page-heading"><div><p class="ccpr-eyebrow"><?php esc_html_e( 'Your store over time', 'certacart-eu-product-readiness' ); ?></p><h1><?php esc_html_e( 'History', 'certacart-eu-product-readiness' ); ?></h1><p><?php esc_html_e( 'Review previous scans and return to any saved report.', 'certacart-eu-product-readiness' ); ?></p></div><a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=ccpr-readiness' ) ); ?>"><?php esc_html_e( 'Go to Home', 'certacart-eu-product-readiness' ); ?></a></div>

		<section class="ccpr-history-section">
			<?php /* translators: %s: number of saved scans. */ ?>
			<div class="ccpr-section-heading"><h2><?php esc_html_e( 'Scan history', 'certacart-eu-product-readiness' ); ?></h2><span><?php echo esc_html( sprintf( _n( '%s saved scan', '%s saved scans', count( $scans ), 'certacart-eu-product-readiness' ), number_format_i18n( count( $scans ) ) ) ); ?></span></div>
			<?php if ( ! $scans ) : ?>
				<div class="ccpr-empty-state"><span class="dashicons dashicons-backup" aria-hidden="true"></span><h2><?php esc_html_e( 'Your scan history will appear here.', 'certacart-eu-product-readiness' ); ?></h2><p><?php esc_html_e( 'Run your first scan to create a baseline.', 'certacart-eu-product-readiness' ); ?></p></div>
			<?php else : ?>
				<div class="ccpr-history-list">
					<?php foreach ( $scans as $ccpr_scan ) : ?>
						<?php
						$ccpr_scan_status = sanitize_key( $ccpr_scan['status'] ?? '' );
						$ccpr_scan_time   = (string) ( $ccpr_scan['completed_at'] ?: $ccpr_scan['created_at'] );
						$ccpr_report_url  = add_query_arg( array( 'page' => 'ccpr-tasks', 'scan_id' => absint( $ccpr_scan['id'] ) ), admin_url( 'admin.php' ) );
						$ccpr_scan_title  = __( 'Scan queued', 'certacart-eu-product-readiness' );
						$ccpr_scan_icon   = 'dashicons-update';
						if ( 'completed' === $ccpr_scan_status ) {
							$ccpr_scan_title = __( 'Store scan completed', 'certacart-eu-product-readiness' );
							$ccpr_scan_icon  = 'dashicons-yes-alt';
						} elseif ( 'failed' === $ccpr_scan_status ) {
							$ccpr_scan_title = __( 'The scan was interrupted', 'certacart-eu-product-readiness' );
							$ccpr_scan_icon  = 'dashicons-warning';
						} elseif ( 'cancelled' === $ccpr_scan_status ) {
							$ccpr_scan_title = __( 'Scan stopped', 'certacart-eu-product-readiness' );
							$ccpr_scan_icon  = 'dashicons-dismiss';
						} elseif ( 'running' === $ccpr_scan_status ) {
							$ccpr_scan_title = __( 'Scan in progress', 'certacart-eu-product-readiness' );
						}
						?>
						<article class="ccpr-history-row">
							<div class="ccpr-history-icon"><span class="dashicons <?php echo esc_attr( $ccpr_scan_icon ); ?>" aria-hidden="true"></span></div>
							<div class="ccpr-history-main"><h3><?php echo esc_html( $ccpr_scan_title ); ?></h3><p><?php echo esc_html( wp_date( get_option( 'date_format' ) . ' · ' . get_option( 'time_format' ), strtotime( $ccpr_scan_time . ' UTC' ) ) ); ?></p></div>
							<div class="ccpr-history-metrics"><span><strong><?php echo esc_html( number_format_i18n( (int) $ccpr_scan['processed_products'] ) ); ?></strong> <?php esc_html_e( 'products', 'certacart-eu-product-readiness' ); ?></span><?php if ( 'completed' === $ccpr_scan_status ) : ?><?php if ( 0 === (int) ( $ccpr_scan['total_products'] ?? 0 ) ) : ?><span><strong><?php esc_html_e( 'Not scored', 'certacart-eu-product-readiness' ); ?></strong> <?php esc_html_e( 'No products in scan', 'certacart-eu-product-readiness' ); ?></span><?php elseif ( \CertaCart\EU_Product_Readiness\Scanner::is_score_available( $ccpr_scan ) ) : ?><span><strong><?php echo esc_html( number_format_i18n( (float) $ccpr_scan['overall_score'], 1 ) ); ?>%</strong> <?php esc_html_e( 'product information readiness', 'certacart-eu-product-readiness' ); ?></span><?php else : ?><span><strong><?php esc_html_e( 'Not scored', 'certacart-eu-product-readiness' ); ?></strong> <?php esc_html_e( 'EU scope needs confirmation', 'certacart-eu-product-readiness' ); ?></span><?php endif; ?><span><strong><?php echo esc_html( number_format_i18n( (int) $ccpr_scan['critical_count'] + (int) $ccpr_scan['warning_count'] ) ); ?></strong> <?php esc_html_e( 'field gaps', 'certacart-eu-product-readiness' ); ?></span><?php endif; ?></div>
							<?php if ( 'completed' === $ccpr_scan_status ) : ?><a class="button" href="<?php echo esc_url( $ccpr_report_url ); ?>"><?php esc_html_e( 'Open report', 'certacart-eu-product-readiness' ); ?></a><?php endif; ?>
						</article>
					<?php endforeach; ?>
				</div>
			<?php endif; ?>
		</section>

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