<?php
/**
 * Readiness Report screen.
 *
 * Available: $report (array), $state (scan state array).
 *
 * @package Upgrade_Pilot
 */

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

$upilot_verdict  = isset( $report['verdict'] ) ? $report['verdict'] : 'none';
$upilot_running  = isset( $state['status'] ) && 'running' === $state['status'];
$upilot_has_scan = ! empty( $state['scan_id'] );
$upilot_settings = UPilot_Store::get_option( 'upilot_settings', array() );
?>
<div class="wrap upilot-wrap">
	<h1><?php esc_html_e( 'Upgrade Pilot — Readiness Report', 'upgrade-pilot' ); ?></h1>

	<div class="upilot-hero upilot-hero--<?php echo esc_attr( $upilot_verdict ); ?>">
		<div class="upilot-hero-light" aria-hidden="true"></div>
		<div class="upilot-hero-body">
			<h2 class="upilot-headline"><?php echo esc_html( isset( $report['headline'] ) ? $report['headline'] : __( 'Run your first scan', 'upgrade-pilot' ) ); ?></h2>
			<p class="upilot-sub">
				<?php
				printf(
					/* translators: 1: target PHP version, 2: current PHP version. */
					esc_html__( 'Readiness target: PHP %1$s (this server currently runs PHP %2$s). Deterministic checks are labelled FACT; static-analysis results are ADVISORY and never block your verdict.', 'upgrade-pilot' ),
					esc_html( isset( $report['target_php'] ) ? $report['target_php'] : '8.4' ),
					esc_html( PHP_VERSION )
				);
				?>
			</p>
			<p>
				<button type="button" class="button button-primary button-hero" id="upilot-start-scan" <?php disabled( $upilot_running ); ?>>
					<?php $upilot_running ? esc_html_e( 'Scan in progress…', 'upgrade-pilot' ) : esc_html_e( 'Run full readiness scan', 'upgrade-pilot' ); ?>
				</button>
				<span class="upilot-progress" id="upilot-progress" role="status" aria-live="polite" <?php echo $upilot_running ? '' : 'hidden'; ?>>
					<span class="spinner is-active" style="float:none;margin:0 8px 0 0;"></span>
					<span id="upilot-progress-text">
						<?php
						if ( $upilot_running ) {
							printf(
								/* translators: 1: done count, 2: total count. */
								esc_html__( '%1$d / %2$d files', 'upgrade-pilot' ),
								(int) ( isset( $state['done'] ) ? $state['done'] : 0 ),
								(int) ( isset( $state['total'] ) ? $state['total'] : 0 )
							);
						}
						?>
					</span>
				</span>
			</p>
			<?php if ( isset( $report['generated_at'] ) ) : ?>
				<p class="upilot-meta">
					<?php
					printf(
						/* translators: %s: human time diff. */
						esc_html__( 'Report assembled %s ago.', 'upgrade-pilot' ),
						esc_html( human_time_diff( (int) $report['generated_at'] ) )
					);
					?>
				</p>
			<?php endif; ?>
		</div>
	</div>

	<h2><?php esc_html_e( 'Server environment', 'upgrade-pilot' ); ?> <span class="upilot-tier upilot-tier--fact"><?php echo esc_html_x( 'FACT', 'evidence tier badge', 'upgrade-pilot' ); ?></span></h2>
	<table class="widefat striped upilot-table">
		<thead>
			<tr>
				<th style="width:30px;"></th>
				<th style="width:220px;"><?php esc_html_e( 'Check', 'upgrade-pilot' ); ?></th>
				<th><?php esc_html_e( 'Result', 'upgrade-pilot' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ( (array) ( isset( $report['server_items'] ) ? $report['server_items'] : array() ) as $upilot_item ) : ?>
				<tr>
					<td><span class="upilot-dot upilot-dot--<?php echo esc_attr( $upilot_item['status'] ); ?>" title="<?php echo esc_attr( $upilot_item['status'] ); ?>"></span></td>
					<td><strong><?php echo esc_html( $upilot_item['label'] ); ?></strong></td>
					<td><?php echo esc_html( $upilot_item['detail'] ); ?></td>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>

	<h2><?php esc_html_e( 'Installed plugins vs WordPress.org', 'upgrade-pilot' ); ?> <span class="upilot-tier upilot-tier--fact"><?php echo esc_html_x( 'FACT', 'evidence tier badge', 'upgrade-pilot' ); ?></span></h2>
	<?php
	$upilot_meta_items = isset( $report['metadata']['items'] ) ? (array) $report['metadata']['items'] : array();
	$upilot_attention  = array_filter(
		$upilot_meta_items,
		function ( $i ) {
			return 'pass' !== $i['status'] || ! in_array( $i['kind'], array( 'ok', 'external', 'unknown' ), true );
		}
	);
	// Honest counting: only wp.org-verified plugins are "checked"; external and
	// unverifiable plugins are reported as their own groups, never as "healthy".
	$upilot_checked_ok = 0;
	$upilot_external   = 0;
	$upilot_unknown    = 0;
	foreach ( $upilot_meta_items as $upilot_mi ) {
		if ( 'external' === $upilot_mi['kind'] ) {
			$upilot_external++;
		} elseif ( 'unknown' === $upilot_mi['kind'] ) {
			$upilot_unknown++;
		} elseif ( 'pass' === $upilot_mi['status'] ) {
			$upilot_checked_ok++;
		}
	}
	?>
	<?php if ( empty( $upilot_meta_items ) ) : ?>
		<p><?php esc_html_e( 'No plugin data yet — the cross-reference runs with the readiness scan.', 'upgrade-pilot' ); ?></p>
	<?php else : ?>
		<?php if ( empty( $upilot_attention ) ) : ?>
			<p class="upilot-allgood">
				<?php
				printf(
					/* translators: %d: plugin count. */
					esc_html( _n( '%d WordPress.org plugin looks healthy.', '%d WordPress.org plugins look healthy.', $upilot_checked_ok, 'upgrade-pilot' ) ),
					(int) $upilot_checked_ok
				);
				if ( $upilot_external > 0 ) {
					echo ' ';
					printf(
						/* translators: %d: plugin count. */
						esc_html( _n( '%d external/premium plugin is covered by the code scan only.', '%d external/premium plugins are covered by the code scan only.', $upilot_external, 'upgrade-pilot' ) ),
						(int) $upilot_external
					);
				}
				if ( $upilot_unknown > 0 ) {
					echo ' ';
					printf(
						/* translators: %d: plugin count. */
						esc_html( _n( '%d plugin could not be verified right now (WordPress.org unreachable).', '%d plugins could not be verified right now (WordPress.org unreachable).', $upilot_unknown, 'upgrade-pilot' ) ),
						(int) $upilot_unknown
					);
				}
				?>
			</p>
		<?php else : ?>
			<table class="widefat striped upilot-table">
				<thead>
					<tr>
						<th style="width:30px;"></th>
						<th style="width:260px;"><?php esc_html_e( 'Plugin', 'upgrade-pilot' ); ?></th>
						<th><?php esc_html_e( 'Finding', 'upgrade-pilot' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $upilot_attention as $upilot_item ) : ?>
						<tr>
							<td><span class="upilot-dot upilot-dot--<?php echo esc_attr( $upilot_item['status'] ); ?>"></span></td>
							<td>
								<strong><?php echo esc_html( $upilot_item['name'] ); ?></strong>
								<?php if ( ! empty( $upilot_item['version'] ) ) : ?>
									<span class="upilot-muted"><?php echo esc_html( $upilot_item['version'] ); ?></span>
								<?php endif; ?>
								<?php if ( empty( $upilot_item['active'] ) ) : ?>
									<span class="upilot-badge"><?php esc_html_e( 'inactive', 'upgrade-pilot' ); ?></span>
								<?php endif; ?>
							</td>
							<td><?php echo esc_html( $upilot_item['detail'] ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<p class="upilot-muted">
				<?php
				$upilot_fine_count = count( $upilot_meta_items ) - count( $upilot_attention );
				printf(
					/* translators: %d: count of healthy plugins. */
					esc_html( _n( '%d more plugin checked out clean and is not listed.', '%d more plugins checked out clean and are not listed.', $upilot_fine_count, 'upgrade-pilot' ) ),
					(int) $upilot_fine_count
				);
				?>
			</p>
		<?php endif; ?>
	<?php endif; ?>

	<h2><?php esc_html_e( 'Static PHP compatibility scan', 'upgrade-pilot' ); ?> <span class="upilot-tier upilot-tier--advisory"><?php echo esc_html_x( 'ADVISORY', 'evidence tier badge', 'upgrade-pilot' ); ?></span></h2>
	<?php if ( ! $upilot_has_scan ) : ?>
		<p><?php esc_html_e( 'Not run yet. The scan reads every plugin\'s PHP files locally (nothing leaves your server) and flags code that modern PHP removed or deprecated.', 'upgrade-pilot' ); ?></p>
	<?php else : ?>
		<?php $upilot_counts = isset( $report['scan_counts'] ) ? $report['scan_counts'] : array(); ?>
		<p>
			<?php
			printf(
				/* translators: 1: error count, 2: warning count, 3: suppressed count. */
				esc_html__( '%1$d possible errors and %2$d warnings found (%3$d suppressed by you). Advisory findings are honest hints, not verdicts: version-guarded or dead code paths commonly trigger them.', 'upgrade-pilot' ),
				(int) ( isset( $upilot_counts['error'] ) ? $upilot_counts['error'] : 0 ),
				(int) ( isset( $upilot_counts['warning'] ) ? $upilot_counts['warning'] : 0 ),
				(int) ( isset( $upilot_counts['suppressed'] ) ? $upilot_counts['suppressed'] : 0 )
			);
			?>
			<a href="<?php echo esc_url( UPilot_Store::admin_url( 'admin.php?page=upgrade-pilot-scan' ) ); ?>"><?php esc_html_e( 'Review findings →', 'upgrade-pilot' ); ?></a>
		</p>
		<?php if ( ! empty( $state['partial'] ) ) : ?>
			<p class="upilot-muted">
				<?php
				printf(
					/* translators: %s: plugin slugs. */
					esc_html__( 'Partially scanned (size/file-count budgets): %s. Oversized or vendored files were skipped and are disclosed here rather than silently ignored.', 'upgrade-pilot' ),
					esc_html( implode( ', ', array_keys( (array) $state['partial'] ) ) )
				);
				?>
			</p>
		<?php endif; ?>
	<?php endif; ?>

	<h2><?php esc_html_e( 'Update trust', 'upgrade-pilot' ); ?></h2>
	<?php $upilot_trust = isset( $report['trust'] ) ? $report['trust'] : array(); ?>
	<p>
		<?php
		$upilot_crit = isset( $upilot_trust['unacked_critical'] ) ? (int) $upilot_trust['unacked_critical'] : 0;
		$upilot_warn = isset( $upilot_trust['unacked_warning'] ) ? (int) $upilot_trust['unacked_warning'] : 0;
		if ( $upilot_crit > 0 ) {
			printf(
				'<span class="upilot-dot upilot-dot--fail"></span> ' .
				/* translators: %d: critical events. */
				esc_html( _n( '%d critical plugin event needs attention.', '%d critical plugin events need attention.', $upilot_crit, 'upgrade-pilot' ) ),
				(int) $upilot_crit
			);
		} elseif ( $upilot_warn > 0 ) {
			printf(
				'<span class="upilot-dot upilot-dot--warn"></span> ' .
				/* translators: %d: warning events. */
				esc_html( _n( '%d plugin change is worth a look.', '%d plugin changes are worth a look.', $upilot_warn, 'upgrade-pilot' ) ),
				(int) $upilot_warn
			);
		} else {
			echo '<span class="upilot-dot upilot-dot--pass"></span> ';
			esc_html_e( 'No unusual ownership or closure activity among your installed plugins.', 'upgrade-pilot' );
		}
		?>
		<a href="<?php echo esc_url( UPilot_Store::admin_url( 'admin.php?page=upgrade-pilot-trust' ) ); ?>"><?php esc_html_e( 'Open Update Trust →', 'upgrade-pilot' ); ?></a>
	</p>

	<p class="upilot-footer-note">
		<?php esc_html_e( 'Upgrade Pilot is read-only: it never modifies your site, your plugins, or your database content. Only plugin/theme slugs are sent to the WordPress.org API.', 'upgrade-pilot' ); ?>
	</p>
</div>
