<?php
/**
 * Scan Results screen: advisory findings grouped by plugin, with suppression.
 *
 * Available: $state (scan state), $results (['by_slug'=>..,'counts'=>..]).
 *
 * @package Upgrade_Pilot
 */

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

$upilot_by_slug = isset( $results['by_slug'] ) ? $results['by_slug'] : array();
$upilot_counts  = isset( $results['counts'] ) ? $results['counts'] : array();
?>
<div class="wrap upilot-wrap">
	<h1><?php esc_html_e( 'Upgrade Pilot — Scan Results', 'upgrade-pilot' ); ?> <span class="upilot-tier upilot-tier--advisory"><?php echo esc_html_x( 'ADVISORY', 'evidence tier badge', 'upgrade-pilot' ); ?></span></h1>

	<?php if ( empty( $state['scan_id'] ) ) : ?>
		<p><?php esc_html_e( 'No scan has been run yet.', 'upgrade-pilot' ); ?>
			<a class="button button-primary" href="<?php echo esc_url( UPilot_Store::admin_url( 'admin.php?page=upgrade-pilot' ) ); ?>"><?php esc_html_e( 'Go to the Readiness Report to start one', 'upgrade-pilot' ); ?></a>
		</p>
	<?php else : ?>
		<p class="upilot-explainer">
			<?php esc_html_e( 'These findings come from reading your plugins\' PHP code locally. They are honest hints, not verdicts: code behind version checks, dead code paths, and bundled polyfills commonly trigger them. A finding you have verified as harmless can be suppressed — suppressions persist across scans.', 'upgrade-pilot' ); ?>
		</p>

		<?php if ( isset( $state['status'] ) && 'running' === $state['status'] ) : ?>
			<div class="notice notice-info inline"><p>
				<?php
				printf(
					/* translators: 1: done count, 2: total count. */
					esc_html__( 'Scan in progress (%1$d / %2$d files) — results below are partial.', 'upgrade-pilot' ),
					(int) ( isset( $state['done'] ) ? $state['done'] : 0 ),
					(int) ( isset( $state['total'] ) ? $state['total'] : 0 )
				);
				?>
			</p></div>
		<?php endif; ?>

		<?php if ( empty( $upilot_by_slug ) ) : ?>
			<?php if ( ! isset( $state['status'] ) || 'running' !== $state['status'] ) : ?>
				<div class="upilot-hero upilot-hero--green"><div class="upilot-hero-body">
					<h2 class="upilot-headline"><?php esc_html_e( 'Clean scan — no compatibility flags in any plugin file', 'upgrade-pilot' ); ?></h2>
				</div></div>
			<?php endif; ?>
		<?php else : ?>
			<?php foreach ( $upilot_by_slug as $upilot_slug => $upilot_rows ) : ?>
				<h2 class="upilot-plugin-heading">
					<?php echo esc_html( $upilot_slug ); ?>
					<?php if ( ! empty( $upilot_rows[0]['item_version'] ) ) : ?>
						<span class="upilot-muted"><?php echo esc_html( $upilot_rows[0]['item_version'] ); ?></span>
					<?php endif; ?>
				</h2>
				<table class="widefat striped upilot-table">
					<thead>
						<tr>
							<th style="width:90px;"><?php esc_html_e( 'Severity', 'upgrade-pilot' ); ?></th>
							<th style="width:70px;"><?php esc_html_e( 'PHP', 'upgrade-pilot' ); ?></th>
							<th><?php esc_html_e( 'Finding', 'upgrade-pilot' ); ?></th>
							<th style="width:280px;"><?php esc_html_e( 'Location', 'upgrade-pilot' ); ?></th>
							<th style="width:110px;"><?php esc_html_e( 'Action', 'upgrade-pilot' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ( $upilot_rows as $upilot_row ) : ?>
							<tr class="<?php echo $upilot_row['suppressed'] ? 'upilot-suppressed' : ''; ?>">
								<td>
									<span class="upilot-dot upilot-dot--<?php echo 'error' === $upilot_row['severity'] ? 'fail' : 'warn'; ?>"></span>
									<?php echo esc_html( 'error' === $upilot_row['severity'] ? __( 'Possible error', 'upgrade-pilot' ) : __( 'Warning', 'upgrade-pilot' ) ); ?>
									<?php if ( ! empty( $upilot_row['above_target'] ) ) : ?>
										<br /><span class="upilot-badge" title="<?php esc_attr_e( 'Only relevant beyond your current target PHP version — not counted in your verdict.', 'upgrade-pilot' ); ?>"><?php esc_html_e( 'beyond target', 'upgrade-pilot' ); ?></span>
									<?php endif; ?>
								</td>
								<td><?php echo esc_html( $upilot_row['php_version'] ); ?></td>
								<td><?php echo esc_html( $upilot_row['message'] ); ?></td>
								<td><code><?php echo esc_html( $upilot_row['file'] . ':' . $upilot_row['line'] ); ?></code></td>
								<td>
									<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
										<?php wp_nonce_field( 'upilot_suppress' ); ?>
										<input type="hidden" name="action" value="upilot_suppress" />
										<input type="hidden" name="slug" value="<?php echo esc_attr( $upilot_row['slug'] ); ?>" />
										<input type="hidden" name="rule_id" value="<?php echo esc_attr( $upilot_row['rule_id'] ); ?>" />
										<?php if ( $upilot_row['suppressed'] ) : ?>
											<input type="hidden" name="undo" value="1" />
											<button type="submit" class="button-link"><?php esc_html_e( 'Unsuppress', 'upgrade-pilot' ); ?></button>
										<?php else : ?>
											<button type="submit" class="button-link"><?php esc_html_e( 'Suppress', 'upgrade-pilot' ); ?></button>
										<?php endif; ?>
									</form>
								</td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			<?php endforeach; ?>

			<p class="upilot-muted">
				<?php
				printf(
					/* translators: 1: error count, 2: warning count, 3: suppressed count. */
					esc_html__( 'Totals: %1$d possible errors · %2$d warnings · %3$d suppressed. Suppressing a rule hides it for that plugin only.', '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 )
				);
				?>
			</p>
		<?php endif; ?>
	<?php endif; ?>
</div>
