<?php
/**
 * Template: Accessibility Scan
 *
 * Renders the accessibility scan page with a "Start Accessibility Scan" button,
 * results grouped by severity (critical, serious, moderate, minor), and
 * WCAG criterion references with element selectors and recommendations.
 *
 * @var array $data JSON data from Cloud API (get-accessibility-results response).
 * @package Biscotti_CMP
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template is included inside Biscotti_Admin::render_dynamic_page(); these are function-scoped locals, not globals.

$page_title = __( 'Accessibility Scan', 'biscotti-cmp' );
include __DIR__ . '/partials/page-header.php';

// Extract results with safe defaults.
$results    = isset( $data['results'] ) && is_array( $data['results'] ) ? $data['results'] : array();
$scan       = isset( $data['scan'] ) && is_array( $data['scan'] ) ? $data['scan'] : array();
$scan_date  = isset( $scan['completedAt'] ) ? $scan['completedAt'] : '';
$scan_url   = isset( $scan['url'] ) ? $scan['url'] : '';

// Severity configuration: labels, colors, and icons.
$severity_config = array(
	'critical' => array(
		'label' => __( 'Critical', 'biscotti-cmp' ),
		'color' => '#dc2626',
		'icon'  => 'dashicons-warning',
	),
	'serious'  => array(
		'label' => __( 'Serious', 'biscotti-cmp' ),
		'color' => '#ea580c',
		'icon'  => 'dashicons-flag',
	),
	'moderate' => array(
		'label' => __( 'Moderate', 'biscotti-cmp' ),
		'color' => '#ca8a04',
		'icon'  => 'dashicons-info',
	),
	'minor'    => array(
		'label' => __( 'Minor', 'biscotti-cmp' ),
		'color' => '#2563eb',
		'icon'  => 'dashicons-editor-help',
	),
);
?>

<div class="biscotti-page biscotti-page-accessibility-scan">

	<!-- Start Scan Button -->
	<div class="biscotti-scan-actions">
		<p class="description">
			<?php esc_html_e( 'Scan your website to identify WCAG accessibility issues. The scan will check for compliance with WCAG 2.1 guidelines and provide actionable recommendations.', 'biscotti-cmp' ); ?>
		</p>
		<p>
			<button
				type="button"
				id="biscotti-start-accessibility-scan"
				class="button button-primary"
				data-label="<?php esc_attr_e( 'Start Accessibility Scan', 'biscotti-cmp' ); ?>"
				data-biscotti-save="trigger-accessibility-scan"
				data-biscotti-form=""
			>
				<span class="dashicons dashicons-universal-access" style="margin-top: 4px; margin-right: 4px;"></span>
				<?php esc_html_e( 'Start Accessibility Scan', 'biscotti-cmp' ); ?>
			</button>
		</p>
	</div>

	<?php if ( ! empty( $scan_date ) ) : ?>
		<p class="biscotti-scan-meta">
			<?php
			printf(
				/* translators: %s: date of last scan */
				esc_html__( 'Last scan: %s', 'biscotti-cmp' ),
				esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $scan_date ) ) )
			);
			?>
			<?php if ( ! empty( $scan_url ) ) : ?>
				&mdash; <?php echo esc_html( $scan_url ); ?>
			<?php endif; ?>
		</p>
	<?php endif; ?>

	<!-- Results Container -->
	<div id="biscotti-accessibility-results">

		<?php if ( empty( $results ) ) : ?>

			<!-- No Results State -->
			<div class="biscotti-empty-state">
				<span class="dashicons dashicons-universal-access biscotti-empty-state-icon"></span>
				<h2><?php esc_html_e( 'No Accessibility Scan Results', 'biscotti-cmp' ); ?></h2>
				<p><?php esc_html_e( 'Run your first accessibility scan to identify WCAG compliance issues on your website.', 'biscotti-cmp' ); ?></p>
			</div>

		<?php else : ?>

			<?php
			// Group results by severity.
			$grouped = array(
				'critical' => array(),
				'serious'  => array(),
				'moderate' => array(),
				'minor'    => array(),
			);

			foreach ( $results as $issue ) {
				$severity = isset( $issue['severity'] ) ? strtolower( $issue['severity'] ) : 'minor';
				if ( ! isset( $grouped[ $severity ] ) ) {
					$severity = 'minor';
				}
				$grouped[ $severity ][] = $issue;
			}

			// Display summary counts.
			?>
			<div class="biscotti-stats-grid">
				<?php foreach ( $severity_config as $sev_key => $sev_conf ) : ?>
					<?php $count = count( $grouped[ $sev_key ] ); ?>
					<div class="biscotti-stat-card">
						<span class="biscotti-stat-label" style="color: <?php echo esc_attr( $sev_conf['color'] ); ?>;">
							<span class="dashicons <?php echo esc_attr( $sev_conf['icon'] ); ?>"></span>
							<?php echo esc_html( $sev_conf['label'] ); ?>
						</span>
						<span class="biscotti-stat-value"><?php echo esc_html( number_format_i18n( $count ) ); ?></span>
					</div>
				<?php endforeach; ?>
			</div>

			<?php
			// Display issues grouped by severity.
			foreach ( $severity_config as $sev_key => $sev_conf ) :
				$issues = $grouped[ $sev_key ];
				if ( empty( $issues ) ) {
					continue;
				}
				?>

				<h2 style="color: <?php echo esc_attr( $sev_conf['color'] ); ?>;">
					<span class="dashicons <?php echo esc_attr( $sev_conf['icon'] ); ?>"></span>
					<?php echo esc_html( $sev_conf['label'] ); ?>
					<span class="biscotti-severity-badge" style="background-color: <?php echo esc_attr( $sev_conf['color'] ); ?>;">
						<?php echo esc_html( number_format_i18n( count( $issues ) ) ); ?>
					</span>
				</h2>

				<table class="biscotti-data-table widefat striped">
					<thead>
						<tr>
							<th><?php esc_html_e( 'WCAG Criterion', 'biscotti-cmp' ); ?></th>
							<th><?php esc_html_e( 'Element', 'biscotti-cmp' ); ?></th>
							<th><?php esc_html_e( 'Recommendation', 'biscotti-cmp' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ( $issues as $issue ) : ?>
							<tr>
								<td>
									<code><?php echo esc_html( $issue['wcagCriterion'] ?? '' ); ?></code>
								</td>
								<td>
									<code class="biscotti-element-selector"><?php echo esc_html( $issue['selector'] ?? '' ); ?></code>
								</td>
								<td>
									<?php echo esc_html( $issue['recommendation'] ?? '' ); ?>
								</td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>

			<?php endforeach; ?>

		<?php endif; ?>

	</div><!-- #biscotti-accessibility-results -->

</div><!-- .biscotti-page-accessibility-scan -->
