<?php
/**
 * Ranked tasks and affected-product report.
 *
 * @package CertaCart_EU_Product_Readiness
 */

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

$ccpr_current_page = 'tasks';
$ccpr_scan_id      = $scan ? (int) $scan['id'] : 0;
$ccpr_scan_status  = $scan ? sanitize_key( $scan['status'] ?? '' ) : '';
$ccpr_needs_scope_confirmation = $scan && \CertaCart\EU_Product_Readiness\Scanner::needs_scope_confirmation( $scan );
?>
<div class="wrap ccpr-wrap" id="ccpr-tasks-page">
	<?php require CCPR_PATH . 'templates/admin-navigation.php'; ?>
	<main class="ccpr-main">
		<div id="ccpr-task-context" data-task="<?php echo esc_attr( $selected_task ? $selected_task['rule'] : ( 'all' === $selected_task_key ? 'all' : '' ) ); ?>" data-rules="<?php echo esc_attr( wp_json_encode( $selected_task ? $selected_task['rules'] : array() ) ); ?>" hidden></div>
		<div class="ccpr-page-heading">
			<div><p class="ccpr-eyebrow"><?php esc_html_e( 'Your action plan', 'certacart-eu-product-readiness' ); ?></p><h1><?php esc_html_e( 'Your tasks', 'certacart-eu-product-readiness' ); ?></h1><p><?php esc_html_e( 'Work through the highest-impact gaps first.', 'certacart-eu-product-readiness' ); ?></p></div>
			<?php if ( $scan ) : ?><a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=ccpr-readiness' ) ); ?>"><?php esc_html_e( 'Go to Home', 'certacart-eu-product-readiness' ); ?></a><?php endif; ?>
		</div>
		<?php if ( $ccpr_needs_scope_confirmation && 'completed' === $ccpr_scan_status ) : ?>
			<div class="notice notice-info inline"><p><strong><?php esc_html_e( 'Your action plan is ready; the readiness score is intentionally N/A.', 'certacart-eu-product-readiness' ); ?></strong> <?php esc_html_e( 'Confirm the store’s EU sales scope when you want CertaCart to grade the catalog.', 'certacart-eu-product-readiness' ); ?> <a href="<?php echo esc_url( admin_url( 'admin.php?page=ccpr-settings' ) ); ?>"><?php esc_html_e( 'Confirm EU sales scope', 'certacart-eu-product-readiness' ); ?></a></p></div>
		<?php endif; ?>

		<?php if ( ! $scan ) : ?>
			<section class="ccpr-empty-state"><span class="dashicons dashicons-yes-alt" aria-hidden="true"></span><h2><?php esc_html_e( 'Scan your store to build your task list.', 'certacart-eu-product-readiness' ); ?></h2><p><?php esc_html_e( 'CertaCart turns catalog findings into a short, ranked action plan.', 'certacart-eu-product-readiness' ); ?></p><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></section>
		<?php elseif ( in_array( $ccpr_scan_status, array( 'queued', 'running' ), true ) ) : ?>
			<section class="ccpr-empty-state"><span class="dashicons dashicons-update" aria-hidden="true"></span><h2><?php esc_html_e( 'Your task list is being built.', 'certacart-eu-product-readiness' ); ?></h2><p><?php esc_html_e( 'CertaCart is finishing the active scan.', 'certacart-eu-product-readiness' ); ?></p><a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=ccpr-readiness' ) ); ?>"><?php esc_html_e( 'View scan progress', 'certacart-eu-product-readiness' ); ?></a></section>
		<?php elseif ( in_array( $ccpr_scan_status, array( 'failed', 'cancelled' ), true ) ) : ?>
			<section class="ccpr-empty-state"><span class="dashicons dashicons-warning" aria-hidden="true"></span><h2><?php echo 'cancelled' === $ccpr_scan_status ? esc_html__( 'Scan stopped.', 'certacart-eu-product-readiness' ) : esc_html__( 'The scan was interrupted.', 'certacart-eu-product-readiness' ); ?></h2><p><?php esc_html_e( 'Your product data was not changed.', 'certacart-eu-product-readiness' ); ?></p><a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=ccpr-readiness' ) ); ?>"><?php esc_html_e( 'Try again', 'certacart-eu-product-readiness' ); ?></a></section>
		<?php elseif ( 0 === (int) ( $scan['total_products'] ?? 0 ) ) : ?>
			<section class="ccpr-empty-state"><span class="dashicons dashicons-products" aria-hidden="true"></span><h2><?php esc_html_e( 'No published products found in this scan.', 'certacart-eu-product-readiness' ); ?></h2><p><?php esc_html_e( 'Add or publish a product to build your product information action plan.', 'certacart-eu-product-readiness' ); ?></p><a class="button button-primary" href="<?php echo esc_url( admin_url( 'post-new.php?post_type=product' ) ); ?>"><?php esc_html_e( 'Add product', 'certacart-eu-product-readiness' ); ?></a></section>
		<?php elseif ( 0 === (int) ( $scan['in_scope_products'] ?? 0 ) ) : ?>
			<section class="ccpr-empty-state"><span class="dashicons dashicons-admin-site-alt3" aria-hidden="true"></span><h2><?php esc_html_e( 'Choose which products you sell to the EU.', 'certacart-eu-product-readiness' ); ?></h2><p><?php esc_html_e( 'No products were scored because EU sales scope is not set.', 'certacart-eu-product-readiness' ); ?></p><a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=ccpr-settings' ) ); ?>"><?php esc_html_e( 'Set EU sales scope', 'certacart-eu-product-readiness' ); ?></a></section>
		<?php elseif ( ! $tasks ) : ?>
			<section class="ccpr-empty-state ccpr-empty-state--success"><span class="dashicons dashicons-yes-alt" aria-hidden="true"></span><h2><?php esc_html_e( 'All supported checks are complete.', 'certacart-eu-product-readiness' ); ?></h2><p><?php esc_html_e( 'No supported product information tasks were found in this scan.', 'certacart-eu-product-readiness' ); ?></p><a class="button" href="<?php echo esc_url( $export_url ); ?>"><?php esc_html_e( 'Export report', 'certacart-eu-product-readiness' ); ?></a></section>
		<?php else : ?>
			<div class="ccpr-task-list" aria-label="<?php esc_attr_e( 'Ranked product information tasks', 'certacart-eu-product-readiness' ); ?>">
				<?php foreach ( $tasks as $ccpr_task ) : ?>
					<?php
					$ccpr_task_url = add_query_arg(
						array( 'page' => 'ccpr-tasks', 'scan_id' => $ccpr_scan_id, 'task' => $ccpr_task['rule'] ),
						admin_url( 'admin.php' )
					);
					$ccpr_is_selected = $selected_task && $selected_task['rule'] === $ccpr_task['rule'];
					?>
					<article class="ccpr-task-card <?php echo $ccpr_is_selected ? 'is-selected' : ''; ?>">
						<div class="ccpr-task-rank" aria-hidden="true"><?php echo esc_html( (string) $ccpr_task['rank'] ); ?></div>
						<div class="ccpr-task-content"><div class="ccpr-task-title-row"><h2><?php echo esc_html( $ccpr_task['title'] ); ?></h2><span class="ccpr-status-chip ccpr-status-chip--<?php echo esc_attr( $ccpr_task['severity'] ); ?>"><?php echo 'critical' === $ccpr_task['severity'] ? esc_html__( 'Must fix', 'certacart-eu-product-readiness' ) : esc_html__( 'Needs review', 'certacart-eu-product-readiness' ); ?></span></div><p><?php echo esc_html( $ccpr_task['reason'] ); ?></p><div class="ccpr-task-meta"><span><strong><?php echo esc_html( number_format_i18n( (int) $ccpr_task['affected_products'] ) ); ?></strong> <?php esc_html_e( 'products affected', 'certacart-eu-product-readiness' ); ?></span><?php if ( ! empty( $ccpr_task['confidence'] ) ) : ?><span class="ccpr-confidence-chip"><?php echo 'high' === $ccpr_task['confidence'] ? esc_html__( 'High detection confidence', 'certacart-eu-product-readiness' ) : esc_html__( 'Merchant review needed', 'certacart-eu-product-readiness' ); ?></span><?php endif; ?></div></div>
						<a class="button <?php echo 1 === (int) $ccpr_task['rank'] ? 'button-primary' : ''; ?>" href="<?php echo esc_url( $ccpr_task_url ); ?>#affected-products"><?php esc_html_e( 'Start this task', 'certacart-eu-product-readiness' ); ?></a>
					</article>
				<?php endforeach; ?>
			</div>

			<section class="ccpr-results-panel" id="affected-products">
				<div class="ccpr-results-toolbar">
					<div><p class="ccpr-eyebrow"><?php esc_html_e( 'Product report', 'certacart-eu-product-readiness' ); ?></p><h2><?php echo $selected_task ? esc_html( $selected_task['title'] ) : esc_html__( 'All scanned products', 'certacart-eu-product-readiness' ); ?></h2><?php if ( $selected_task ) : ?><p><?php echo esc_html( $selected_task['best_next_action'] ); ?></p><?php endif; ?></div>
					<div class="ccpr-results-actions">
						<label class="screen-reader-text" for="ccpr-filter-severity"><?php esc_html_e( 'Filter by status', 'certacart-eu-product-readiness' ); ?></label>
						<select id="ccpr-filter-severity"><option value=""><?php esc_html_e( 'All statuses', 'certacart-eu-product-readiness' ); ?></option><option value="critical"><?php esc_html_e( 'Must fix', 'certacart-eu-product-readiness' ); ?></option><option value="warning"><?php esc_html_e( 'Needs review', 'certacart-eu-product-readiness' ); ?></option><?php if ( ! $selected_task ) : ?><option value="ready"><?php esc_html_e( 'Ready', 'certacart-eu-product-readiness' ); ?></option><option value="out_of_scope"><?php esc_html_e( 'Outside EU scope', 'certacart-eu-product-readiness' ); ?></option><?php endif; ?></select>
						<label class="screen-reader-text" for="ccpr-search-products"><?php esc_html_e( 'Search products', 'certacart-eu-product-readiness' ); ?></label><input type="search" id="ccpr-search-products" placeholder="<?php esc_attr_e( 'Search products…', 'certacart-eu-product-readiness' ); ?>"><button class="button" type="button" id="ccpr-apply-filters"><?php esc_html_e( 'Search', 'certacart-eu-product-readiness' ); ?></button><a class="button" id="ccpr-export" href="<?php echo esc_url( $export_url ); ?>"><?php esc_html_e( 'Export CSV', 'certacart-eu-product-readiness' ); ?></a>
					</div>
				</div>
				<?php if ( $selected_task ) : ?><p><a href="<?php echo esc_url( add_query_arg( array( 'page' => 'ccpr-tasks', 'scan_id' => $ccpr_scan_id, 'task' => 'all' ), admin_url( 'admin.php' ) ) ); ?>#affected-products"><?php esc_html_e( 'View full product report', 'certacart-eu-product-readiness' ); ?></a></p><?php endif; ?>
				<div class="ccpr-table-scroll"><table class="widefat striped ccpr-results-table"><thead><tr><th><?php esc_html_e( 'Product', 'certacart-eu-product-readiness' ); ?></th><th><?php esc_html_e( 'Score', 'certacart-eu-product-readiness' ); ?></th><th><?php esc_html_e( 'What needs attention', 'certacart-eu-product-readiness' ); ?></th><th><?php esc_html_e( 'Actions', 'certacart-eu-product-readiness' ); ?></th></tr></thead><tbody id="ccpr-results-body"><tr><td colspan="4"><?php esc_html_e( 'Loading affected products…', 'certacart-eu-product-readiness' ); ?></td></tr></tbody></table></div>
				<div class="ccpr-pagination" id="ccpr-pagination" aria-label="<?php esc_attr_e( 'Results pagination', 'certacart-eu-product-readiness' ); ?>"></div>
			</section>
		<?php endif; ?>
	</main>
</div>
