<?php
/**
 * Dashboard page view.
 *
 * Renders the EAA scope questionnaire and the scan form.
 * All output is escaped. No business logic here.
 *
 * @package AccessLens
 */

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

$accesslens_last_scan = AccessLens_Options::get( 'accesslens_last_scan', null );
$accesslens_is_pro    = function_exists( 'accesslens_fs' ) && accesslens_fs()->is_paying();
$accesslens_stats     = $accesslens_last_scan['stats'] ?? array(
	'critical' => 0,
	'serious'  => 0,
	'moderate' => 0,
	'minor'    => 0,
);
?>
<div class="wrap accesslens-dashboard">
	<h1><?php esc_html_e( 'AccessLens - Web Accessibility Audit', 'accesslens' ); ?></h1>

	<div class="accesslens-notice-disclaimer notice notice-info inline">
		<p>
			<strong><?php esc_html_e( 'Important notice:', 'accesslens' ); ?></strong>
			<?php esc_html_e( 'This scanner detects approximately 25-40% of accessibility issues. Full compliance with the EAA requires additional manual review by an expert. Results are indicative and do not constitute a declaration of conformity.', 'accesslens' ); ?>
		</p>
	</div>

	<?php if ( $accesslens_last_scan ) : ?>
	<!-- Last scan (persisted) -->
	<div class="accesslens-card accesslens-last-scan">
		<h2>
			<?php
			printf(
				/* translators: 1: date, 2: scanned URL */
				esc_html__( 'Last scan: %1$s - %2$s', 'accesslens' ),
				esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) $accesslens_last_scan['timestamp'] ) ),
				esc_html( $accesslens_last_scan['url'] )
			);
			?>
		</h2>
		<button type="button" id="accesslens-rescan-button" class="button button-secondary" data-scan-url="<?php echo esc_attr( $accesslens_last_scan['url'] ); ?>">
			<?php esc_html_e( 'Scan again', 'accesslens' ); ?>
		</button>
	</div>
	<?php endif; ?>

	<!-- Severity panel: totales por severidad, nunca un score global -->
	<div id="accesslens-severity-panel" class="accesslens-severity-panel<?php echo $accesslens_last_scan ? '' : ' hidden'; ?>">
		<div class="accesslens-severity-box accesslens-severity-critical">
			<span class="accesslens-severity-count" id="accesslens-severity-count-critical"><?php echo esc_html( (string) $accesslens_stats['critical'] ); ?></span>
			<span class="accesslens-severity-label"><?php esc_html_e( 'Critical', 'accesslens' ); ?></span>
		</div>
		<div class="accesslens-severity-box accesslens-severity-serious">
			<span class="accesslens-severity-count" id="accesslens-severity-count-serious"><?php echo esc_html( (string) $accesslens_stats['serious'] ); ?></span>
			<span class="accesslens-severity-label"><?php esc_html_e( 'Serious', 'accesslens' ); ?></span>
		</div>
		<div class="accesslens-severity-box accesslens-severity-moderate">
			<span class="accesslens-severity-count" id="accesslens-severity-count-moderate"><?php echo esc_html( (string) $accesslens_stats['moderate'] ); ?></span>
			<span class="accesslens-severity-label"><?php esc_html_e( 'Moderate', 'accesslens' ); ?></span>
		</div>
		<div class="accesslens-severity-box accesslens-severity-minor">
			<span class="accesslens-severity-count" id="accesslens-severity-count-minor"><?php echo esc_html( (string) $accesslens_stats['minor'] ); ?></span>
			<span class="accesslens-severity-label"><?php esc_html_e( 'Minor', 'accesslens' ); ?></span>
		</div>
	</div>
	<p class="accesslens-severity-honesty description<?php echo $accesslens_last_scan ? '' : ' hidden'; ?>" id="accesslens-severity-honesty">
		<?php esc_html_e( 'Automated detection: covers approx. 25-40% of WCAG criteria. Manual review is still necessary.', 'accesslens' ); ?>
	</p>

	<?php $accesslens_history_count = count( (array) AccessLens_Options::get( 'accesslens_scan_history', array() ) ); ?>
	<!-- Progress over time: solo counts historicos, nunca detalle de violaciones ni score/compliance. -->
	<div id="accesslens-history-wrap" class="accesslens-card accesslens-no-print accesslens-history-card<?php echo $accesslens_history_count >= 2 ? '' : ' hidden'; ?>">
		<h2><?php esc_html_e( 'Progress over time', 'accesslens' ); ?></h2>
		<p id="accesslens-history-trend" class="accesslens-history-trend description"></p>

		<div id="accesslens-history-chart-wrap" class="accesslens-history-chart-wrap"></div>

		<ul class="accesslens-history-legend">
			<li><span class="accesslens-legend-swatch accesslens-legend-critical" aria-hidden="true"></span><?php esc_html_e( 'Critical', 'accesslens' ); ?></li>
			<li><span class="accesslens-legend-swatch accesslens-legend-serious" aria-hidden="true"></span><?php esc_html_e( 'Serious', 'accesslens' ); ?></li>
			<li><span class="accesslens-legend-swatch accesslens-legend-moderate" aria-hidden="true"></span><?php esc_html_e( 'Moderate', 'accesslens' ); ?></li>
			<li><span class="accesslens-legend-swatch accesslens-legend-minor" aria-hidden="true"></span><?php esc_html_e( 'Minor', 'accesslens' ); ?></li>
		</ul>

		<details class="accesslens-history-details">
			<summary><?php esc_html_e( 'Show recent scans', 'accesslens' ); ?></summary>
			<table class="widefat striped accesslens-history-table">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Date', 'accesslens' ); ?></th>
						<th><?php esc_html_e( 'URL', 'accesslens' ); ?></th>
						<th><?php esc_html_e( 'Mode', 'accesslens' ); ?></th>
						<th><?php esc_html_e( 'Pages', 'accesslens' ); ?></th>
						<th class="accesslens-col-critical"><?php esc_html_e( 'Critical', 'accesslens' ); ?></th>
						<th class="accesslens-col-serious"><?php esc_html_e( 'Serious', 'accesslens' ); ?></th>
						<th class="accesslens-col-moderate"><?php esc_html_e( 'Moderate', 'accesslens' ); ?></th>
						<th class="accesslens-col-minor"><?php esc_html_e( 'Minor', 'accesslens' ); ?></th>
					</tr>
				</thead>
				<tbody id="accesslens-history-table-body"></tbody>
			</table>
		</details>
	</div>

	<!-- Accessibility Statement preview (visible en Free y Pro) -->
	<div class="accesslens-card accesslens-declaration-preview">
		<h2><?php esc_html_e( 'Accessibility Statement', 'accesslens' ); ?></h2>
		<p class="description">
			<?php esc_html_e( 'This is the public document the European Accessibility Act (EAA) expects you to publish on your site: it explains the accessibility status of your service and how to get in touch if someone finds a barrier. AccessLens does not certify compliance; it helps you prepare and maintain this documentation honestly.', 'accesslens' ); ?>
		</p>
		<ul class="accesslens-declaration-fields">
			<li><?php esc_html_e( '1. Identification of the service or site (name, URL, date)', 'accesslens' ); ?></li>
			<li><?php esc_html_e( '2. Reference standard (EN 301549 / WCAG 2.1 AA)', 'accesslens' ); ?></li>
			<li><?php esc_html_e( '3. Accessibility status, based on your scan results', 'accesslens' ); ?></li>
			<li class="accesslens-field-locked"><span class="dashicons dashicons-lock" aria-hidden="true"></span> <?php esc_html_e( '4. Non-conformities per page and WCAG criterion (full-site crawl and AI prioritization with Pro)', 'accesslens' ); ?></li>
			<li><?php esc_html_e( '5. Evaluation method used (automated scan; manual review recommended)', 'accesslens' ); ?></li>
			<li><?php esc_html_e( '6. Contact / feedback channel', 'accesslens' ); ?></li>
			<li><?php esc_html_e( '7. Complaint procedure', 'accesslens' ); ?></li>
			<li><?php esc_html_e( '8. Preparation and last review date', 'accesslens' ); ?></li>
			<li><?php esc_html_e( '9. Document language', 'accesslens' ); ?></li>
		</ul>
		<?php if ( $accesslens_is_pro ) : ?>
			<p class="description">
				<?php esc_html_e( 'Generate your full Statement from the results section, after completing a scan.', 'accesslens' ); ?>
			</p>
		<?php else : ?>
			<p class="description">
				<?php esc_html_e( 'A basic Statement is included with your free scan. Pro enhances it with a full-site crawl (up to 25 pages) and AI-prioritized analysis.', 'accesslens' ); ?>
			</p>
			<p>
				<a href="<?php echo esc_url( function_exists( 'accesslens_fs' ) ? (string) accesslens_fs()->get_upgrade_url() : '#' ); ?>" class="button button-primary">
					<?php esc_html_e( 'Upgrade to Pro', 'accesslens' ); ?>
				</a>
			</p>
		<?php endif; ?>
	</div>

	<!-- EAA Scope Questionnaire -->
	<div class="accesslens-card accesslens-scope-checker">
		<h2><?php esc_html_e( 'Check if your company falls within the scope of the EAA', 'accesslens' ); ?></h2>
		<p class="description">
			<?php esc_html_e( 'Answer these questions for an initial orientation. This diagnosis has no legal validity.', 'accesslens' ); ?>
		</p>

		<table class="form-table accesslens-scope-table" role="presentation">
			<tr>
				<th scope="row">
					<label for="accesslens_employees"><?php esc_html_e( 'Number of employees', 'accesslens' ); ?></label>
				</th>
				<td>
					<select id="accesslens_employees" class="accesslens-scope-input">
						<option value=""><?php esc_html_e( '-- Select --', 'accesslens' ); ?></option>
						<option value="micro"><?php esc_html_e( 'Fewer than 10', 'accesslens' ); ?></option>
						<option value="small"><?php esc_html_e( '10 to 49', 'accesslens' ); ?></option>
						<option value="medium"><?php esc_html_e( '50 to 249', 'accesslens' ); ?></option>
						<option value="large"><?php esc_html_e( '250 or more', 'accesslens' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<label for="accesslens_revenue"><?php esc_html_e( 'Annual revenue', 'accesslens' ); ?></label>
				</th>
				<td>
					<select id="accesslens_revenue" class="accesslens-scope-input">
						<option value=""><?php esc_html_e( '-- Select --', 'accesslens' ); ?></option>
						<option value="micro"><?php esc_html_e( 'Less than EUR 2 million', 'accesslens' ); ?></option>
						<option value="small"><?php esc_html_e( 'EUR 2 to 10 million', 'accesslens' ); ?></option>
						<option value="medium"><?php esc_html_e( 'EUR 10 to 50 million', 'accesslens' ); ?></option>
						<option value="large"><?php esc_html_e( 'More than EUR 50 million', 'accesslens' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<label for="accesslens_service_type"><?php esc_html_e( 'Type of service or product', 'accesslens' ); ?></label>
				</th>
				<td>
					<select id="accesslens_service_type" class="accesslens-scope-input">
						<option value=""><?php esc_html_e( '-- Select --', 'accesslens' ); ?></option>
						<option value="ecommerce"><?php esc_html_e( 'E-commerce (online store)', 'accesslens' ); ?></option>
						<option value="banking"><?php esc_html_e( 'Banking or financial services', 'accesslens' ); ?></option>
						<option value="transport"><?php esc_html_e( 'Passenger transport', 'accesslens' ); ?></option>
						<option value="media"><?php esc_html_e( 'Audiovisual media services', 'accesslens' ); ?></option>
						<option value="telecom"><?php esc_html_e( 'Telecommunications services', 'accesslens' ); ?></option>
						<option value="ereader"><?php esc_html_e( 'E-books / e-readers', 'accesslens' ); ?></option>
						<option value="other_digital"><?php esc_html_e( 'Other digital service', 'accesslens' ); ?></option>
						<option value="not_digital"><?php esc_html_e( 'I do not sell digital products/services', 'accesslens' ); ?></option>
					</select>
				</td>
			</tr>
		</table>

		<div id="accesslens-scope-verdict" class="accesslens-scope-verdict hidden" role="alert" aria-live="polite"></div>

		<button type="button" id="accesslens-check-scope" class="button button-secondary">
			<?php esc_html_e( 'View orientative diagnosis', 'accesslens' ); ?>
		</button>
	</div>

	<!-- Scan Form -->
	<div class="accesslens-card accesslens-scan-form">
		<h2><?php esc_html_e( 'New audit', 'accesslens' ); ?></h2>

		<table class="form-table" role="presentation">
			<tr>
				<th scope="row">
					<label for="accesslens_scan_url"><?php esc_html_e( 'URL to audit', 'accesslens' ); ?></label>
				</th>
				<td>
					<input
						type="url"
						id="accesslens_scan_url"
						name="accesslens_scan_url"
						class="regular-text"
						value="<?php echo esc_attr( home_url() ); ?>"
						required
					/>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<label for="accesslens_scan_mode"><?php esc_html_e( 'Scan mode', 'accesslens' ); ?></label>
				</th>
				<td>
					<select id="accesslens_scan_mode" name="accesslens_scan_mode">
						<option value="single"><?php esc_html_e( 'Single page', 'accesslens' ); ?></option>
						<option value="sitemap"><?php esc_html_e( 'Sitemap', 'accesslens' ); ?></option>
						<option value="crawl"><?php esc_html_e( 'Full crawl', 'accesslens' ); ?></option>
					</select>
				</td>
			</tr>
		</table>

		<div class="accesslens-scan-actions">
			<button type="button" id="accesslens-start-scan" class="button button-primary">
				<?php esc_html_e( 'Start audit', 'accesslens' ); ?>
			</button>
		</div>
	</div>

	<!-- Progress & Status -->
	<div id="accesslens-scan-status" class="accesslens-card hidden">
		<h2><?php esc_html_e( 'Scan status', 'accesslens' ); ?></h2>
		<div id="accesslens-status-text" class="accesslens-status-badge" role="status" aria-live="polite"></div>
		<div class="accesslens-progress-wrap">
			<progress id="accesslens-progress-bar" value="0" max="100"></progress>
			<span id="accesslens-progress-label"></span>
		</div>
		<div id="accesslens-quota-info" class="accesslens-quota-info hidden" aria-live="polite"></div>
	</div>

	<!-- Results Table -->
	<div id="accesslens-results-wrap" class="accesslens-card hidden">
		<h2><?php esc_html_e( 'Results by page', 'accesslens' ); ?></h2>

		<div class="accesslens-no-print">
			<button type="button" id="accesslens-print-results" class="button button-secondary">
				<?php esc_html_e( 'Print results', 'accesslens' ); ?>
			</button>
		</div>

		<div id="accesslens-results-summary" class="accesslens-results-summary"></div>

		<table id="accesslens-results-table" class="widefat striped accesslens-results-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Page', 'accesslens' ); ?></th>
					<th><?php esc_html_e( 'Title', 'accesslens' ); ?></th>
					<th class="accesslens-col-critical"><?php esc_html_e( 'Critical', 'accesslens' ); ?></th>
					<th class="accesslens-col-serious"><?php esc_html_e( 'Serious', 'accesslens' ); ?></th>
					<th class="accesslens-col-moderate"><?php esc_html_e( 'Moderate', 'accesslens' ); ?></th>
					<th class="accesslens-col-minor"><?php esc_html_e( 'Minor', 'accesslens' ); ?></th>
					<th><?php esc_html_e( 'Total', 'accesslens' ); ?></th>
					<th><?php esc_html_e( 'Top rules', 'accesslens' ); ?></th>
				</tr>
			</thead>
			<tbody id="accesslens-results-body">
			</tbody>
		</table>

		<p class="accesslens-print-only accesslens-print-footer">
			<?php esc_html_e( 'Generated with AccessLens - partial automated detection (25-40% of WCAG criteria).', 'accesslens' ); ?>
		</p>

		<!-- Upsell contextual post-scan (solo Free, solo si hay violaciones) -->
		<div id="accesslens-upsell-box" class="accesslens-upsell-box accesslens-no-print hidden"></div>

		<!-- Report Download -->
		<div class="accesslens-report-section">
			<h3><?php esc_html_e( 'Download accessibility report', 'accesslens' ); ?></h3>
			<p class="description">
				<?php esc_html_e( 'The report is generated by the AccessLens service. Advanced AI analysis (alt text and suggestions) requires a Pro subscription to the service; without it, the basic version is generated.', 'accesslens' ); ?>
			</p>
			<table class="form-table accesslens-report-meta" role="presentation">
				<tr>
					<th><label for="accesslens_report_site_name"><?php esc_html_e( 'Site name', 'accesslens' ); ?></label></th>
					<td><input type="text" id="accesslens_report_site_name" class="regular-text" value="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="accesslens_report_entity_type"><?php esc_html_e( 'Entity type', 'accesslens' ); ?></label></th>
					<td>
						<select id="accesslens_report_entity_type">
							<option value="private"><?php esc_html_e( 'Private', 'accesslens' ); ?></option>
							<option value="public"><?php esc_html_e( 'Public', 'accesslens' ); ?></option>
						</select>
					</td>
				</tr>
				<tr>
					<th><label for="accesslens_report_contact_email"><?php esc_html_e( 'Accessibility contact email', 'accesslens' ); ?></label></th>
					<td><input type="email" id="accesslens_report_contact_email" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="accesslens_report_contact_url"><?php esc_html_e( 'Contact form URL', 'accesslens' ); ?></label></th>
					<td><input type="url" id="accesslens_report_contact_url" class="regular-text" value="<?php echo esc_attr( home_url( '/contacto' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="accesslens_report_prepared_by"><?php esc_html_e( 'Prepared by', 'accesslens' ); ?></label></th>
					<td><input type="text" id="accesslens_report_prepared_by" class="regular-text" /></td>
				</tr>
			</table>
			<button type="button" id="accesslens-download-report" class="button button-primary">
				<?php esc_html_e( 'Download PDF report', 'accesslens' ); ?>
			</button>
			<div id="accesslens-report-status" class="accesslens-report-status" role="status" aria-live="polite"></div>
		</div>
	</div>
</div>
