<?php
/**
 * Page principale — scan de compatibilité PHP.
 *
 * @package PCC
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

$plugin       = PCC_Plugin::instance();
$can_all      = $plugin->has_feature( 'scan_all_plugins' );
$can_parent   = $plugin->has_feature( 'scan_parent_theme' );
$can_fp       = $plugin->has_feature( 'false_positive_mark' );
$can_csv      = $plugin->has_feature( 'export_csv' );
$can_history  = $plugin->has_feature( 'scan_history' );
$verdict      = $last_scan['verdict'] ?? 'unknown';
$scan_status  = $scan_state['status'] ?? '';
$is_running   = 'running' === $scan_status;
$is_paused    = 'paused' === $scan_status;
$is_active    = $is_running || $is_paused;
$can_schedule = $plugin->has_feature( 'scheduled_scan' );
$pcc_doc_url = function_exists( 'volade_extension_doc_url' ) ? volade_extension_doc_url( PCC_SLUG ) : 'https://volade.com/fr/cms/wordpress/extensions/' . PCC_SLUG;

if ( empty( $settings['target_php'] ) && ! empty( $targets ) ) {
	$settings['target_php'] = $targets[0];
}

$delta_checked = ! empty( $settings['delta_mode'] ) || ! empty( $settings['default_delta_mode'] );
$custom_excludes_text = is_array( $settings['custom_excludes'] ?? null )
	? implode( "\n", $settings['custom_excludes'] )
	: (string) ( $settings['custom_excludes'] ?? '' );

$last_errors    = (int) ( $last_scan['summary']['errors'] ?? 0 );
$last_warnings  = (int) ( $last_scan['summary']['warnings'] ?? 0 );
$last_files     = (int) ( $last_scan['summary']['files'] ?? 0 );
$last_skipped   = (int) ( $last_scan['summary']['files_skipped'] ?? 0 );
$target_display = $last_scan['target_php'] ?? ( $settings['target_php'] ?? ( $targets[0] ?? '' ) );
$has_results    = ! empty( $last_scan['completed_at'] );
$runtime_count  = count( $runtime_log );
$ext_loaded     = $env_check['details']['extensions']['loaded'] ?? array();
$deep_analysis  = $env_check['details']['phpcs_bundled'] ?? '';
$memory_limit   = $env_check['details']['memory_limit'] ?? '';
$batch_display  = $env_check['details']['batch_recommended'] ?? (string) $batch_recommended;
$has_mu_plugins = PCC_Scanner::has_mu_plugins();
$has_dropins    = PCC_Scanner::has_dropins();
?>
<div class="pcc-wrap-inner">
	<div class="pcc-page-header">
		<div>
			<h1><?php esc_html_e( 'PHP Compatibility Checker by Volade', 'php-compatibility-checker-by-volade' ); ?></h1>
			<p class="description pcc-page-subtitle">
				<?php
				printf(
					/* translators: %s: current PHP version */
					esc_html__( 'Server PHP: %s — scan before you upgrade.', 'php-compatibility-checker-by-volade' ),
					esc_html( $runtime_php )
				);
				?>
			</p>
		</div>
		<div class="pcc-header-actions">
			<span class="pcc-header-badge" title="<?php esc_attr_e( 'Current server PHP version', 'php-compatibility-checker-by-volade' ); ?>">
				PHP <?php echo esc_html( $runtime_php ); ?>
			</span>
			<a class="button button-secondary" href="<?php echo esc_url( $pcc_doc_url ); ?>" target="_blank" rel="noopener">
				<?php esc_html_e( 'Documentation', 'php-compatibility-checker-by-volade' ); ?>
			</a>
		</div>
	</div>

	<?php if ( isset( $_GET['preset_applied'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Preset applied.', 'php-compatibility-checker-by-volade' ); ?></p></div>
	<?php endif; ?>
	<?php if ( isset( $_GET['schedule_saved'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Scheduled scan settings saved.', 'php-compatibility-checker-by-volade' ); ?></p></div>
	<?php endif; ?>
	<?php if ( isset( $_GET['whitelist_imported'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'False-positive whitelist imported.', 'php-compatibility-checker-by-volade' ); ?></p></div>
	<?php endif; ?>
	<?php if ( isset( $_GET['whitelist_error'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="notice notice-error is-dismissible"><p><?php esc_html_e( 'Could not import whitelist file.', 'php-compatibility-checker-by-volade' ); ?></p></div>
	<?php endif; ?>
	<?php if ( isset( $_GET['options_saved'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved.', 'php-compatibility-checker-by-volade' ); ?></p></div>
	<?php endif; ?>
	<?php if ( isset( $_GET['runtime_cleared'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Runtime log cleared.', 'php-compatibility-checker-by-volade' ); ?></p></div>
	<?php endif; ?>
	<?php if ( isset( $_GET['pcc_scan_done'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Single-plugin scan complete.', 'php-compatibility-checker-by-volade' ); ?></p></div>
	<?php endif; ?>

	<?php if ( ! empty( $conflicts ) ) : ?>
		<div class="notice notice-warning">
			<p>
				<strong><?php esc_html_e( 'Similar plugin detected', 'php-compatibility-checker-by-volade' ); ?>:</strong>
				<?php
				$names = wp_list_pluck( $conflicts, 'name' );
				echo esc_html( implode( ', ', $names ) );
				?>
				— <?php esc_html_e( 'Deactivate the other checker to avoid duplicate scans.', 'php-compatibility-checker-by-volade' ); ?>
			</p>
		</div>
	<?php endif; ?>

	<?php include PCC_DIR . 'admin/views/partials/intro-php-compat.php'; ?>
	<?php include PCC_DIR . 'admin/views/partials/access-summary.php'; ?>

	<div class="pcc-stats-bar">
		<div class="pcc-stats-row">
			<div class="pcc-stat-pill">
				<strong><?php echo esc_html( $runtime_php ); ?></strong>
				<span><?php esc_html_e( 'Server PHP', 'php-compatibility-checker-by-volade' ); ?></span>
			</div>
			<div class="pcc-stat-pill">
				<strong><?php echo esc_html( $target_display ); ?></strong>
				<span><?php esc_html_e( 'Scan target', 'php-compatibility-checker-by-volade' ); ?></span>
			</div>
			<?php if ( $has_results ) : ?>
				<div class="pcc-stat-pill pcc-stat-pill-error">
					<strong><?php echo esc_html( number_format_i18n( $last_errors ) ); ?></strong>
					<span><?php esc_html_e( 'Errors', 'php-compatibility-checker-by-volade' ); ?></span>
				</div>
				<div class="pcc-stat-pill pcc-stat-pill-warning">
					<strong><?php echo esc_html( number_format_i18n( $last_warnings ) ); ?></strong>
					<span><?php esc_html_e( 'Warnings', 'php-compatibility-checker-by-volade' ); ?></span>
				</div>
				<div class="pcc-stat-pill pcc-stat-pill-muted">
					<strong><?php echo esc_html( number_format_i18n( $last_files ) ); ?></strong>
					<span><?php esc_html_e( 'Files scanned', 'php-compatibility-checker-by-volade' ); ?></span>
				</div>
			<?php else : ?>
				<div class="pcc-stat-pill pcc-stat-pill-muted">
					<strong>—</strong>
					<span><?php esc_html_e( 'No scan yet', 'php-compatibility-checker-by-volade' ); ?></span>
				</div>
			<?php endif; ?>
			<?php if ( $runtime_count > 0 ) : ?>
				<div class="pcc-stat-pill pcc-stat-pill-warning">
					<strong><?php echo esc_html( number_format_i18n( $runtime_count ) ); ?></strong>
					<span><?php esc_html_e( 'Runtime events', 'php-compatibility-checker-by-volade' ); ?></span>
				</div>
			<?php endif; ?>
		</div>
		<div class="pcc-info-tip">
			<button
				type="button"
				class="pcc-info-tip-trigger"
				aria-label="<?php esc_attr_e( 'About scan results', 'php-compatibility-checker-by-volade' ); ?>"
				aria-describedby="pcc-scan-tip"
				aria-expanded="false"
			>
				<svg class="pcc-info-tip-icon" width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
					<circle cx="8" cy="8" r="7" stroke="currentColor" stroke-width="1.5"/>
					<path d="M8 7.2V11" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
					<circle cx="8" cy="5" r="0.9" fill="currentColor"/>
				</svg>
			</button>
			<div class="pcc-info-tip-bubble" id="pcc-scan-tip" role="tooltip">
				<p><?php esc_html_e( 'Errors block a PHP upgrade — fix them first. Warnings are deprecations that may still run but should be updated.', 'php-compatibility-checker-by-volade' ); ?></p>
				<p><?php esc_html_e( 'The scan runs entirely on your server. Vendor and node_modules folders are skipped by default to focus on your code.', 'php-compatibility-checker-by-volade' ); ?></p>
			</div>
		</div>
		<?php if ( ! $has_results ) : ?>
			<p class="pcc-stats-hint">
				<?php esc_html_e( 'Pick a preset below, then run your first scan to see errors and warnings before switching PHP on your host.', 'php-compatibility-checker-by-volade' ); ?>
			</p>
		<?php endif; ?>
	</div>

	<div class="pcc-env-strip">
		<span class="pcc-env-chip <?php echo empty( $env_check['messages'] ) ? 'pcc-env-chip-ok' : 'pcc-env-chip-warn'; ?>">
			<strong><?php esc_html_e( 'Environment', 'php-compatibility-checker-by-volade' ); ?></strong>
			<?php
			echo esc_html(
				sprintf(
					/* translators: 1: memory limit, 2: deep analysis yes/no, 3: batch size */
					__( '%1$s · Deep analysis: %2$s · Batch: %3$s', 'php-compatibility-checker-by-volade' ),
					$memory_limit,
					$deep_analysis,
					$batch_display
				)
			);
			?>
		</span>
		<?php if ( ! empty( $env_check['messages'] ) ) : ?>
			<span class="pcc-env-chip pcc-env-chip-warn">
				<?php echo esc_html( implode( ' · ', $env_check['messages'] ) ); ?>
			</span>
		<?php endif; ?>
	</div>
	<?php if ( ! empty( $ext_loaded ) ) : ?>
		<div class="pcc-env-strip">
			<div class="pcc-ext-chips" aria-label="<?php esc_attr_e( 'Loaded PHP extensions', 'php-compatibility-checker-by-volade' ); ?>">
				<?php foreach ( $ext_loaded as $ext ) : ?>
					<span class="pcc-ext-chip"><?php echo esc_html( $ext ); ?></span>
				<?php endforeach; ?>
			</div>
		</div>
	<?php endif; ?>

	<div class="pcc-main-layout">
		<div class="pcc-main-column">
			<div class="pcc-panels">

	<section class="pcc-panel pcc-presets">
		<h2><span class="pcc-panel-icon" aria-hidden="true">⚡</span> <?php esc_html_e( 'Quick presets', 'php-compatibility-checker-by-volade' ); ?></h2>
		<p class="pcc-panel-lead"><?php esc_html_e( 'One click to set the target PHP version and recommended scan scope.', 'php-compatibility-checker-by-volade' ); ?></p>
		<div class="pcc-preset-grid">
			<?php foreach ( $presets as $slug => $preset ) : ?>
				<?php
				$icon_class = 'pcc-preset-icon';
				if ( preg_match( '/^[0-9.]+$/', (string) $preset['icon'] ) ) {
					$icon_class .= ' pcc-preset-icon-ver';
				}
				?>
				<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="pcc-preset-card">
					<input type="hidden" name="action" value="pcc_apply_preset" />
					<?php wp_nonce_field( 'pcc_apply_preset' ); ?>
					<input type="hidden" name="pcc_preset" value="<?php echo esc_attr( $slug ); ?>" />
					<div class="<?php echo esc_attr( $icon_class ); ?>"><?php echo esc_html( $preset['icon'] ); ?></div>
					<strong><?php echo esc_html( $preset['label'] ); ?></strong>
					<p class="pcc-preset-desc"><?php echo esc_html( $preset['desc'] ); ?></p>
					<button type="submit" class="button button-secondary pcc-preset-apply"><?php esc_html_e( 'Apply', 'php-compatibility-checker-by-volade' ); ?></button>
				</form>
			<?php endforeach; ?>
		</div>
	</section>

	<section class="pcc-panel pcc-scan-config">
		<h2><span class="pcc-panel-icon" aria-hidden="true">🔍</span> <?php esc_html_e( 'Scan options', 'php-compatibility-checker-by-volade' ); ?></h2>
		<p class="pcc-panel-lead"><?php esc_html_e( 'Fine-tune scope, batch size, and components before launching the analysis.', 'php-compatibility-checker-by-volade' ); ?></p>
		<form id="pcc-scan-form" class="pcc-scan-form">
			<div class="pcc-field-row">
				<label for="pcc-target-php"><?php esc_html_e( 'Target PHP version', 'php-compatibility-checker-by-volade' ); ?></label>
				<select id="pcc-target-php" name="target_php">
					<?php foreach ( $targets as $ver ) : ?>
						<option value="<?php echo esc_attr( $ver ); ?>" <?php selected( $settings['target_php'] ?? '', $ver ); ?>>
							<?php echo esc_html( $ver ); ?>
						</option>
					<?php endforeach; ?>
				</select>
				<?php if ( ! $can_all ) : ?>
					<p class="description"><?php esc_html_e( 'Free mode: next PHP version only. Connect Volade for all targets.', 'php-compatibility-checker-by-volade' ); ?></p>
				<?php endif; ?>
			</div>

			<?php if ( $can_all ) : ?>
				<div class="pcc-field-row">
					<label for="pcc-target-wp"><?php esc_html_e( 'Target WordPress version (optional)', 'php-compatibility-checker-by-volade' ); ?></label>
					<select id="pcc-target-wp" name="target_wp">
						<option value=""><?php esc_html_e( 'None — PHP only', 'php-compatibility-checker-by-volade' ); ?></option>
						<?php foreach ( $wp_targets as $wver ) : ?>
							<option value="<?php echo esc_attr( $wver ); ?>" <?php selected( $settings['target_wp'] ?? '', $wver ); ?>><?php echo esc_html( $wver ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>
				<div class="pcc-field-row pcc-delta-row">
					<label><input type="checkbox" name="delta_mode" value="1" <?php checked( $delta_checked ); ?> /> <?php esc_html_e( 'Delta mode — only show issues introduced after baseline PHP', 'php-compatibility-checker-by-volade' ); ?></label>
					<label for="pcc-baseline-php"><?php esc_html_e( 'Baseline PHP', 'php-compatibility-checker-by-volade' ); ?></label>
					<input type="text" id="pcc-baseline-php" name="baseline_php" value="<?php echo esc_attr( $settings['baseline_php'] ?: $runtime_php ); ?>" class="small-text" />
				</div>
			<?php endif; ?>

			<div class="pcc-field-row pcc-checkboxes">
				<label><input type="checkbox" name="scan_plugins" value="1" <?php checked( ! empty( $settings['scan_plugins'] ) ); ?> /> <?php esc_html_e( 'Scan plugins', 'php-compatibility-checker-by-volade' ); ?></label>
				<label><input type="checkbox" name="scan_themes" value="1" <?php checked( ! empty( $settings['scan_themes'] ) ); ?> /> <?php esc_html_e( 'Scan themes', 'php-compatibility-checker-by-volade' ); ?></label>
				<label><input type="checkbox" name="skip_vendor" value="1" <?php checked( ! isset( $settings['skip_vendor'] ) || ! empty( $settings['skip_vendor'] ) ); ?> /> <?php esc_html_e( 'Skip vendor / node_modules', 'php-compatibility-checker-by-volade' ); ?></label>
				<label><input type="checkbox" name="scan_active_first" value="1" <?php checked( ! isset( $settings['scan_active_first'] ) || ! empty( $settings['scan_active_first'] ) ); ?> /> <?php esc_html_e( 'Scan active components first', 'php-compatibility-checker-by-volade' ); ?></label>
				<?php if ( $can_parent ) : ?>
					<label><input type="checkbox" name="scan_parent" value="1" <?php checked( ! empty( $settings['scan_parent'] ) ); ?> /> <?php esc_html_e( 'Include parent theme', 'php-compatibility-checker-by-volade' ); ?></label>
				<?php endif; ?>
				<?php if ( $can_all && $has_mu_plugins ) : ?>
					<label><input type="checkbox" name="scan_mu_plugins" value="1" <?php checked( ! empty( $settings['scan_mu_plugins'] ) ); ?> /> <?php esc_html_e( 'Scan must-use plugins', 'php-compatibility-checker-by-volade' ); ?></label>
					<p class="description pcc-field-help"><?php esc_html_e( 'Must-use plugins live in wp-content/mu-plugins/ — they do not appear in the Extensions list.', 'php-compatibility-checker-by-volade' ); ?></p>
				<?php endif; ?>
				<?php if ( $can_all && $has_dropins ) : ?>
					<label><input type="checkbox" name="scan_dropins" value="1" <?php checked( ! empty( $settings['scan_dropins'] ) ); ?> /> <?php esc_html_e( 'Scan drop-ins (object-cache.php, etc.)', 'php-compatibility-checker-by-volade' ); ?></label>
				<?php endif; ?>
			</div>

			<div class="pcc-field-row">
				<label for="pcc-batch-size"><?php esc_html_e( 'Batch size', 'php-compatibility-checker-by-volade' ); ?></label>
				<select id="pcc-batch-size" name="batch_size">
					<option value="0" <?php selected( (int) ( $settings['batch_size'] ?? 0 ), 0 ); ?>>
						<?php
						printf(
							/* translators: %d: recommended batch size */
							esc_html__( 'Auto (%d)', 'php-compatibility-checker-by-volade' ),
							(int) $batch_recommended
						);
						?>
					</option>
					<?php foreach ( array( 10, 25, 50, 100 ) as $bs ) : ?>
						<option value="<?php echo esc_attr( (string) $bs ); ?>" <?php selected( (int) ( $settings['batch_size'] ?? 0 ), $bs ); ?>><?php echo esc_html( (string) $bs ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>

			<?php if ( $can_all ) : ?>
				<details class="pcc-target-picker">
					<summary><?php esc_html_e( 'Choose specific plugins / themes', 'php-compatibility-checker-by-volade' ); ?></summary>
					<p class="pcc-target-bulk">
						<button type="button" class="button button-small" id="pcc-select-all-plugins"><?php esc_html_e( 'Select all plugins', 'php-compatibility-checker-by-volade' ); ?></button>
						<button type="button" class="button button-small" id="pcc-select-active-plugins"><?php esc_html_e( 'Active plugins only', 'php-compatibility-checker-by-volade' ); ?></button>
						<button type="button" class="button button-small" id="pcc-select-all-themes"><?php esc_html_e( 'Select all themes', 'php-compatibility-checker-by-volade' ); ?></button>
					</p>
					<div class="pcc-target-columns">
						<div id="pcc-plugin-checkboxes">
							<strong><?php esc_html_e( 'Plugins', 'php-compatibility-checker-by-volade' ); ?></strong>
							<?php
							if ( ! function_exists( 'get_plugins' ) ) {
								require_once ABSPATH . 'wp-admin/includes/plugin.php';
							}
							foreach ( get_plugins() as $file => $data ) {
								$slug = dirname( $file );
								if ( '.' === $slug ) {
									$slug = basename( $file, '.php' );
								}
								$checked = empty( $settings['selected_plugins'] ) || in_array( $slug, $settings['selected_plugins'], true );
								$is_active = in_array( $file, $active_plugins, true );
								echo '<label data-active="' . ( $is_active ? '1' : '0' ) . '"><input type="checkbox" name="selected_plugins[]" value="' . esc_attr( $slug ) . '" ' . checked( $checked, true, false ) . ' /> ' . esc_html( $data['Name'] ?? $slug ) . '</label><br />';
							}
							?>
						</div>
						<div id="pcc-theme-checkboxes">
							<strong><?php esc_html_e( 'Themes', 'php-compatibility-checker-by-volade' ); ?></strong>
							<?php foreach ( $all_themes as $slug => $theme ) : ?>
								<?php
								$checked = empty( $settings['selected_themes'] ) || in_array( $slug, $settings['selected_themes'], true );
								?>
								<label><input type="checkbox" name="selected_themes[]" value="<?php echo esc_attr( $slug ); ?>" <?php checked( $checked ); ?> /> <?php echo esc_html( $theme->get( 'Name' ) ); ?></label><br />
							<?php endforeach; ?>
						</div>
					</div>
				</details>
			<?php else : ?>
				<p class="description">
					<?php
					$plugin_count = 0;
					foreach ( $scan_targets as $t ) {
						if ( 'plugin' === $t['type'] ) {
							$plugin_count++;
						}
					}
					printf(
						/* translators: %d: number of components */
						esc_html__( 'Free scan scope: %d active plugin(s) and active theme.', 'php-compatibility-checker-by-volade' ),
						absint( $plugin_count )
					);
					?>
				</p>
				<?php if ( ! $can_guest_scan ) : ?>
					<p class="pcc-guest-limit"><?php esc_html_e( 'Daily scan limit reached. Connect Volade for unlimited scans.', 'php-compatibility-checker-by-volade' ); ?></p>
				<?php endif; ?>
			<?php endif; ?>

			<div class="pcc-scan-actions">
				<button type="button" class="button button-primary" id="pcc-start-scan" <?php disabled( $is_active || ( ! $can_all && ! $can_guest_scan ) ); ?>>
					<?php esc_html_e( 'Run scan', 'php-compatibility-checker-by-volade' ); ?>
				</button>
				<button type="button" class="button" id="pcc-pause-scan" <?php disabled( ! $is_running ); ?>>
					<?php esc_html_e( 'Pause', 'php-compatibility-checker-by-volade' ); ?>
				</button>
				<button type="button" class="button" id="pcc-resume-scan" <?php disabled( ! $is_paused ); ?>>
					<?php esc_html_e( 'Resume', 'php-compatibility-checker-by-volade' ); ?>
				</button>
				<button type="button" class="button" id="pcc-stop-scan" <?php disabled( ! $is_active ); ?>>
					<?php esc_html_e( 'Stop', 'php-compatibility-checker-by-volade' ); ?>
				</button>
			</div>
		</form>

		<div class="pcc-progress-wrap" id="pcc-progress-wrap" <?php echo $is_active ? '' : 'hidden'; ?>>
			<div class="pcc-progress-bar"><div class="pcc-progress-fill" id="pcc-progress-fill" style="width:0%"></div></div>
			<p class="pcc-progress-label" id="pcc-progress-label"><?php esc_html_e( 'Preparing…', 'php-compatibility-checker-by-volade' ); ?></p>
		</div>
	</section>

	<section class="pcc-panel pcc-advanced-settings">
		<h2><span class="pcc-panel-icon" aria-hidden="true">⚙️</span> <?php esc_html_e( 'Advanced settings', 'php-compatibility-checker-by-volade' ); ?></h2>
		<p class="pcc-panel-lead"><?php esc_html_e( 'Defaults for delta mode, results display, and passive runtime monitoring.', 'php-compatibility-checker-by-volade' ); ?></p>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<input type="hidden" name="action" value="pcc_save_options" />
			<?php wp_nonce_field( 'pcc_save_options' ); ?>
			<div class="pcc-checkboxes">
				<label><input type="checkbox" name="default_delta_mode" value="1" <?php checked( ! empty( $settings['default_delta_mode'] ) ); ?> /> <?php esc_html_e( 'Enable delta mode by default', 'php-compatibility-checker-by-volade' ); ?></label>
				<label><input type="checkbox" name="errors_only" value="1" <?php checked( ! empty( $settings['errors_only'] ) ); ?> /> <?php esc_html_e( 'Display errors only in results table', 'php-compatibility-checker-by-volade' ); ?></label>
				<label><input type="checkbox" name="runtime_monitor" value="1" <?php checked( ! isset( $settings['runtime_monitor'] ) || ! empty( $settings['runtime_monitor'] ) ); ?> /> <?php esc_html_e( 'Capture runtime WordPress deprecations', 'php-compatibility-checker-by-volade' ); ?></label>
				<?php if ( $can_all ) : ?>
					<label><input type="checkbox" name="auto_rescan_update" value="1" <?php checked( ! empty( $settings['auto_rescan_update'] ) ); ?> /> <?php esc_html_e( 'Auto-rescan plugin after update', 'php-compatibility-checker-by-volade' ); ?></label>
				<?php endif; ?>
			</div>
			<p>
				<label for="pcc-max-warnings"><?php esc_html_e( 'Max warnings before caution verdict (0 = any warning)', 'php-compatibility-checker-by-volade' ); ?></label>
				<input type="number" min="0" max="9999" id="pcc-max-warnings" name="max_warnings_verdict" class="small-text" value="<?php echo esc_attr( (string) (int) ( $settings['max_warnings_verdict'] ?? 0 ) ); ?>" />
			</p>
			<?php if ( $can_all ) : ?>
				<p>
					<label for="pcc-custom-excludes"><?php esc_html_e( 'Custom path exclusions (one per line)', 'php-compatibility-checker-by-volade' ); ?></label><br />
					<textarea id="pcc-custom-excludes" name="custom_excludes" rows="3" class="large-text code"><?php echo esc_textarea( $custom_excludes_text ); ?></textarea>
				</p>
			<?php endif; ?>
			<button type="submit" class="button button-secondary"><?php esc_html_e( 'Save settings', 'php-compatibility-checker-by-volade' ); ?></button>
		</form>
	</section>

	<?php if ( ! empty( $runtime_log ) ) : ?>
		<section class="pcc-panel pcc-runtime-log">
			<h2><span class="pcc-panel-icon" aria-hidden="true">📡</span> <?php esc_html_e( 'Runtime deprecations', 'php-compatibility-checker-by-volade' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Captured passively while the site runs — no WP_DEBUG required.', 'php-compatibility-checker-by-volade' ); ?></p>
			<table class="widefat striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Time', 'php-compatibility-checker-by-volade' ); ?></th>
						<th><?php esc_html_e( 'Type', 'php-compatibility-checker-by-volade' ); ?></th>
						<th><?php esc_html_e( 'Source', 'php-compatibility-checker-by-volade' ); ?></th>
						<th><?php esc_html_e( 'Detail', 'php-compatibility-checker-by-volade' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( array_slice( $runtime_log, 0, 20 ) as $row ) : ?>
						<tr>
							<td><?php echo esc_html( $row['time'] ?? '' ); ?></td>
							<td><?php echo esc_html( $row['type'] ?? '' ); ?></td>
							<td><code><?php echo esc_html( $row['source'] ?? '' ); ?></code></td>
							<td><?php echo esc_html( $row['function'] ?? $row['hook'] ?? $row['message'] ?? '' ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-top:12px">
				<input type="hidden" name="action" value="pcc_clear_runtime_log" />
				<?php wp_nonce_field( 'pcc_clear_runtime_log' ); ?>
				<button type="submit" class="button"><?php esc_html_e( 'Clear log', 'php-compatibility-checker-by-volade' ); ?></button>
			</form>
		</section>
	<?php endif; ?>

	<?php if ( ! empty( $last_scan['completed_at'] ) || $is_active ) : ?>
		<section class="pcc-panel pcc-results">
			<h2><span class="pcc-panel-icon" aria-hidden="true">📊</span> <?php esc_html_e( 'Results', 'php-compatibility-checker-by-volade' ); ?></h2>
			<?php if ( ! empty( $last_scan['completed_at'] ) ) : ?>
				<?php
				$verdict_icons = array(
					'ready'   => '✅',
					'caution' => '⚠️',
					'blocked' => '🛑',
				);
				$verdict_icon  = $verdict_icons[ $verdict ] ?? '🔍';
				?>
				<div class="pcc-verdict-hero pcc-verdict-<?php echo esc_attr( $verdict ); ?>">
					<div class="pcc-verdict-icon" aria-hidden="true"><?php echo esc_html( $verdict_icon ); ?></div>
					<div class="pcc-verdict-body">
						<strong><?php echo esc_html( $plugin->scanner->verdict_label( $verdict ) ); ?></strong>
						<span>
							<?php
							printf(
								/* translators: 1: target PHP, 2: error count, 3: warning count */
								esc_html__( 'PHP %1$s — %2$d errors, %3$d warnings', 'php-compatibility-checker-by-volade' ),
								esc_html( $last_scan['target_php'] ?? '' ),
								(int) ( $last_scan['summary']['errors'] ?? 0 ),
								(int) ( $last_scan['summary']['warnings'] ?? 0 )
							);
							?>
						</span>
						<?php if ( ! empty( $last_scan['summary']['files'] ) ) : ?>
							<span class="description">
								<?php
								printf(
									/* translators: 1: files scanned, 2: files skipped */
									esc_html__( '%1$d files scanned · %2$d skipped (vendor/exclusions)', 'php-compatibility-checker-by-volade' ),
									(int) ( $last_scan['summary']['files'] ?? 0 ),
									(int) ( $last_scan['summary']['files_skipped'] ?? 0 )
								);
								?>
							</span>
						<?php endif; ?>
					</div>
					<div class="pcc-verdict-metrics">
						<div class="pcc-verdict-metric pcc-verdict-metric-errors">
							<strong><?php echo (int) ( $last_scan['summary']['errors'] ?? 0 ); ?></strong>
							<span><?php esc_html_e( 'Errors', 'php-compatibility-checker-by-volade' ); ?></span>
						</div>
						<div class="pcc-verdict-metric pcc-verdict-metric-warnings">
							<strong><?php echo (int) ( $last_scan['summary']['warnings'] ?? 0 ); ?></strong>
							<span><?php esc_html_e( 'Warnings', 'php-compatibility-checker-by-volade' ); ?></span>
						</div>
						<div class="pcc-verdict-metric">
							<strong><?php echo (int) ( $last_scan['summary']['files'] ?? 0 ); ?></strong>
							<span><?php esc_html_e( 'Files', 'php-compatibility-checker-by-volade' ); ?></span>
						</div>
					</div>
				</div>

				<?php if ( ! empty( $last_scan['top_issues'] ) ) : ?>
					<div class="pcc-top-issues">
						<h3><?php esc_html_e( 'Top components to fix', 'php-compatibility-checker-by-volade' ); ?></h3>
						<ol>
							<?php foreach ( $last_scan['top_issues'] as $top ) : ?>
								<?php if ( empty( $top['errors'] ) && empty( $top['warnings'] ) ) { continue; } ?>
								<li>
									<strong><?php echo esc_html( $top['label'] ?? '' ); ?></strong>
									— <?php echo (int) ( $top['errors'] ?? 0 ); ?> <?php esc_html_e( 'errors', 'php-compatibility-checker-by-volade' ); ?>,
									<?php echo (int) ( $top['warnings'] ?? 0 ); ?> <?php esc_html_e( 'warnings', 'php-compatibility-checker-by-volade' ); ?>
								</li>
							<?php endforeach; ?>
						</ol>
					</div>
				<?php endif; ?>

				<?php if ( ! empty( $last_scan['by_component'] ) ) : ?>
					<table class="widefat striped pcc-component-table">
						<thead>
							<tr>
								<th><?php esc_html_e( 'Component', 'php-compatibility-checker-by-volade' ); ?></th>
								<th><?php esc_html_e( 'Score', 'php-compatibility-checker-by-volade' ); ?></th>
								<th><?php esc_html_e( 'Errors', 'php-compatibility-checker-by-volade' ); ?></th>
								<th><?php esc_html_e( 'Warnings', 'php-compatibility-checker-by-volade' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $last_scan['by_component'] as $cid => $row ) : ?>
								<tr>
									<td><?php echo esc_html( $row['label'] ?? $cid ); ?></td>
									<td><?php echo (int) ( $row['score'] ?? $plugin->scanner->component_score( $row ) ); ?>%</td>
									<td><?php echo (int) ( $row['errors'] ?? 0 ); ?></td>
									<td><?php echo (int) ( $row['warnings'] ?? 0 ); ?></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				<?php endif; ?>

				<?php if ( ! empty( $last_scan['findings'] ) ) : ?>
					<details class="pcc-findings-details" open>
						<summary><?php esc_html_e( 'All findings', 'php-compatibility-checker-by-volade' ); ?></summary>
						<div class="pcc-findings-toolbar">
							<input type="search" id="pcc-findings-search" placeholder="<?php esc_attr_e( 'Filter findings…', 'php-compatibility-checker-by-volade' ); ?>" class="regular-text" />
							<select id="pcc-findings-severity">
								<option value=""><?php esc_html_e( 'All severities', 'php-compatibility-checker-by-volade' ); ?></option>
								<option value="error"><?php esc_html_e( 'Errors only', 'php-compatibility-checker-by-volade' ); ?></option>
								<option value="warning"><?php esc_html_e( 'Warnings only', 'php-compatibility-checker-by-volade' ); ?></option>
							</select>
						</div>
						<table class="widefat striped pcc-findings-table" id="pcc-findings-table">
							<thead>
								<tr>
									<th><?php esc_html_e( 'Severity', 'php-compatibility-checker-by-volade' ); ?></th>
									<th><?php esc_html_e( 'Component', 'php-compatibility-checker-by-volade' ); ?></th>
									<th><?php esc_html_e( 'File', 'php-compatibility-checker-by-volade' ); ?></th>
									<th><?php esc_html_e( 'Line', 'php-compatibility-checker-by-volade' ); ?></th>
									<th><?php esc_html_e( 'Message', 'php-compatibility-checker-by-volade' ); ?></th>
									<th><?php esc_html_e( 'Actions', 'php-compatibility-checker-by-volade' ); ?></th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ( array_slice( $last_scan['findings'], 0, 500 ) as $finding ) : ?>
									<?php
									$rel_file   = str_replace( ABSPATH, '', $finding['file'] ?? '' );
									$show_error = empty( $settings['errors_only'] ) || 'error' === ( $finding['severity'] ?? '' );
									if ( ! $show_error ) {
										continue;
									}
									$code_hint = (string) ( $finding['code'] ?? '' );
									$hint_url  = '';
									foreach ( PCC_Static_Analyzer::error_hints() as $prefix => $url ) {
										if ( 0 === strpos( $code_hint, $prefix ) ) {
											$hint_url = $url;
											break;
										}
									}
									?>
									<tr class="pcc-sev-<?php echo esc_attr( $finding['severity'] ?? 'warning' ); ?>" data-severity="<?php echo esc_attr( $finding['severity'] ?? '' ); ?>" data-search="<?php echo esc_attr( strtolower( ( $finding['component'] ?? '' ) . ' ' . $rel_file . ' ' . ( $finding['message'] ?? '' ) ) ); ?>">
										<td><?php echo esc_html( $finding['severity'] ?? '' ); ?></td>
										<td><?php echo esc_html( $finding['component'] ?? '' ); ?></td>
										<td><code class="pcc-file-path" data-full="<?php echo esc_attr( $finding['file'] ?? '' ); ?>"><?php echo esc_html( $rel_file ); ?></code></td>
										<td><?php echo (int) ( $finding['line'] ?? 0 ); ?></td>
										<td>
											<?php echo esc_html( $finding['message'] ?? '' ); ?>
											<?php if ( $hint_url ) : ?>
												<a href="<?php echo esc_url( $hint_url ); ?>" target="_blank" rel="noopener" class="pcc-hint-link" title="<?php esc_attr_e( 'Learn more', 'php-compatibility-checker-by-volade' ); ?>">?</a>
											<?php endif; ?>
										</td>
										<td class="pcc-row-actions">
											<button type="button" class="button-link pcc-copy-path"><?php esc_html_e( 'Copy', 'php-compatibility-checker-by-volade' ); ?></button>
											<?php if ( $can_fp ) : ?>
												<button type="button" class="button-link pcc-fp-btn" data-fingerprint="<?php echo esc_attr( $finding['fingerprint'] ?? '' ); ?>">
													<?php esc_html_e( 'False +', 'php-compatibility-checker-by-volade' ); ?>
												</button>
											<?php endif; ?>
										</td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
						<p id="pcc-findings-empty" class="description" hidden><?php esc_html_e( 'No findings match your filter.', 'php-compatibility-checker-by-volade' ); ?></p>
					</details>
				<?php endif; ?>

				<div class="pcc-export-actions">
					<button type="button" class="button" id="pcc-print-report"><?php esc_html_e( 'Print report', 'php-compatibility-checker-by-volade' ); ?></button>
					<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
						<input type="hidden" name="action" value="pcc_export_report" />
						<?php wp_nonce_field( 'pcc_export_report' ); ?>
						<button type="submit" class="button"><?php esc_html_e( 'Export JSON', 'php-compatibility-checker-by-volade' ); ?></button>
					</form>
					<?php if ( $can_csv ) : ?>
						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
							<input type="hidden" name="action" value="pcc_export_csv" />
							<?php wp_nonce_field( 'pcc_export_csv' ); ?>
							<button type="submit" class="button"><?php esc_html_e( 'Export CSV', 'php-compatibility-checker-by-volade' ); ?></button>
						</form>
					<?php else : ?>
						<?php $plugin->render_gate( 'export_csv', __( 'CSV export is available with V+ Premium.', 'php-compatibility-checker-by-volade' ) ); ?>
					<?php endif; ?>
					<?php if ( $can_fp ) : ?>
						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
							<input type="hidden" name="action" value="pcc_export_whitelist" />
							<?php wp_nonce_field( 'pcc_export_whitelist' ); ?>
							<button type="submit" class="button"><?php esc_html_e( 'Export whitelist', 'php-compatibility-checker-by-volade' ); ?></button>
						</form>
						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" enctype="multipart/form-data" class="pcc-import-whitelist">
							<input type="hidden" name="action" value="pcc_import_whitelist" />
							<?php wp_nonce_field( 'pcc_import_whitelist' ); ?>
							<input type="file" name="pcc_whitelist_file" accept="application/json,.json" />
							<button type="submit" class="button"><?php esc_html_e( 'Import whitelist', 'php-compatibility-checker-by-volade' ); ?></button>
						</form>
					<?php endif; ?>
				</div>

				<?php if ( ! empty( $last_scan['audit']['items'] ) ) : ?>
					<details class="pcc-audit-details">
						<summary><?php esc_html_e( 'Update suggestions & wp.org audit', 'php-compatibility-checker-by-volade' ); ?></summary>
						<table class="widefat striped">
							<thead>
								<tr>
									<th><?php esc_html_e( 'Component', 'php-compatibility-checker-by-volade' ); ?></th>
									<th><?php esc_html_e( 'Version', 'php-compatibility-checker-by-volade' ); ?></th>
									<th><?php esc_html_e( 'Notes', 'php-compatibility-checker-by-volade' ); ?></th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ( $last_scan['audit']['items'] as $audit_row ) : ?>
									<tr>
										<td><?php echo esc_html( $audit_row['name'] ?? '' ); ?></td>
										<td>
											<?php
											echo esc_html( $audit_row['local_version'] ?? '' );
											if ( ! empty( $audit_row['update_available'] ) ) {
												echo ' → ' . esc_html( $audit_row['latest_version'] ?? '' );
											}
											?>
										</td>
										<td><?php echo esc_html( implode( ' ', $audit_row['issues'] ?? array() ) ); ?></td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</details>
				<?php endif; ?>
			<?php endif; ?>
		</section>
	<?php endif; ?>

	<?php if ( $can_history && count( $history ) >= 2 ) : ?>
		<section class="pcc-panel pcc-compare">
			<h2><?php esc_html_e( 'Compare scans', 'php-compatibility-checker-by-volade' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Pick two history entries to see error/warning deltas (regressions).', 'php-compatibility-checker-by-volade' ); ?></p>
			<div class="pcc-compare-form">
				<select id="pcc-compare-a">
					<?php foreach ( $history as $row ) : ?>
						<option value="<?php echo esc_attr( $row['id'] ?? '' ); ?>"><?php echo esc_html( ( $row['completed_at'] ?? '' ) . ' — PHP ' . ( $row['target_php'] ?? '' ) ); ?></option>
					<?php endforeach; ?>
				</select>
				<select id="pcc-compare-b">
					<?php foreach ( array_reverse( $history ) as $row ) : ?>
						<option value="<?php echo esc_attr( $row['id'] ?? '' ); ?>"><?php echo esc_html( ( $row['completed_at'] ?? '' ) . ' — PHP ' . ( $row['target_php'] ?? '' ) ); ?></option>
					<?php endforeach; ?>
				</select>
				<button type="button" class="button" id="pcc-compare-btn"><?php esc_html_e( 'Compare', 'php-compatibility-checker-by-volade' ); ?></button>
			</div>
			<div id="pcc-compare-result" hidden></div>
		</section>
	<?php endif; ?>

	<?php if ( $can_history && ! empty( $history ) ) : ?>
		<section class="pcc-panel pcc-history">
			<h2><?php esc_html_e( 'Scan history', 'php-compatibility-checker-by-volade' ); ?></h2>
			<table class="widefat striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Date', 'php-compatibility-checker-by-volade' ); ?></th>
						<th><?php esc_html_e( 'Target', 'php-compatibility-checker-by-volade' ); ?></th>
						<th><?php esc_html_e( 'Verdict', 'php-compatibility-checker-by-volade' ); ?></th>
						<th><?php esc_html_e( 'Errors', 'php-compatibility-checker-by-volade' ); ?></th>
						<th><?php esc_html_e( 'Warnings', 'php-compatibility-checker-by-volade' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( array_reverse( $history ) as $row ) : ?>
						<tr>
							<td><?php echo esc_html( $row['completed_at'] ?? '' ); ?></td>
							<td><?php echo esc_html( $row['target_php'] ?? '' ); ?></td>
							<td><?php echo esc_html( $plugin->scanner->verdict_label( $row['verdict'] ?? 'unknown' ) ); ?></td>
							<td><?php echo (int) ( $row['errors'] ?? 0 ); ?></td>
							<td><?php echo (int) ( $row['warnings'] ?? 0 ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</section>
	<?php elseif ( ! $can_history ) : ?>
		<section class="pcc-panel pcc-gated">
			<h2><span class="pcc-panel-icon" aria-hidden="true">🕐</span> <?php esc_html_e( 'Scan history', 'php-compatibility-checker-by-volade' ); ?> <span class="pcc-tier pcc-tier-premium">V+</span></h2>
			<?php $plugin->render_gate( 'scan_history', __( 'Scan history is available with V+ Premium.', 'php-compatibility-checker-by-volade' ) ); ?>
		</section>
	<?php endif; ?>

	<?php if ( $can_schedule ) : ?>
		<section class="pcc-panel pcc-schedule">
			<h2><?php esc_html_e( 'Scheduled scan (V+)', 'php-compatibility-checker-by-volade' ); ?></h2>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
				<input type="hidden" name="action" value="pcc_save_schedule" />
				<?php wp_nonce_field( 'pcc_save_schedule' ); ?>
				<label><input type="checkbox" name="schedule_enabled" value="1" <?php checked( ! empty( $schedule['enabled'] ) ); ?> /> <?php esc_html_e( 'Enable scheduled scan', 'php-compatibility-checker-by-volade' ); ?></label>
				<p>
					<label for="pcc-schedule-frequency"><?php esc_html_e( 'Frequency', 'php-compatibility-checker-by-volade' ); ?></label>
					<select name="schedule_frequency" id="pcc-schedule-frequency">
						<option value="weekly" <?php selected( $schedule['frequency'] ?? '', 'weekly' ); ?>><?php esc_html_e( 'Weekly', 'php-compatibility-checker-by-volade' ); ?></option>
						<option value="monthly" <?php selected( $schedule['frequency'] ?? '', 'monthly' ); ?>><?php esc_html_e( 'Monthly', 'php-compatibility-checker-by-volade' ); ?></option>
					</select>
				</p>
				<p>
					<label for="pcc-schedule-email"><?php esc_html_e( 'Email report to', 'php-compatibility-checker-by-volade' ); ?></label>
					<input type="email" name="schedule_email" id="pcc-schedule-email" class="regular-text" value="<?php echo esc_attr( $schedule['email'] ?? get_option( 'admin_email' ) ); ?>" />
				</p>
				<button type="submit" class="button button-primary"><?php esc_html_e( 'Save schedule', 'php-compatibility-checker-by-volade' ); ?></button>
			</form>
		</section>
	<?php else : ?>
		<section class="pcc-panel pcc-gated">
			<h2><span class="pcc-panel-icon" aria-hidden="true">📅</span> <?php esc_html_e( 'Scheduled scan', 'php-compatibility-checker-by-volade' ); ?> <span class="pcc-tier pcc-tier-premium">V+</span></h2>
			<?php $plugin->render_gate( 'scheduled_scan', __( 'Scheduled scans are available with V+ Premium.', 'php-compatibility-checker-by-volade' ) ); ?>
		</section>
	<?php endif; ?>

			</div><!-- .pcc-panels -->
		</div><!-- .pcc-main-column -->

		<?php include PCC_DIR . 'admin/views/partials/migration-guide.php'; ?>
	</div><!-- .pcc-main-layout -->
</div>
