<?php
/**
 * Update Trust screen: provenance events, plugin custody table, freeze controls.
 *
 * Available: $events, $states, $frozen, $map.
 *
 * @package Upgrade_Pilot
 */

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

$upilot_last_run = (int) UPilot_Store::get_option( 'upilot_last_snapshot_run', 0 );

$upilot_event_labels = array(
	'author_change'       => __( 'Author changed', 'upgrade-pilot' ),
	'contributor_added'   => __( 'Contributor(s) added', 'upgrade-pilot' ),
	'contributor_removed' => __( 'Contributor(s) removed', 'upgrade-pilot' ),
	'closed'              => __( 'Closed on WordPress.org', 'upgrade-pilot' ),
	'reopened'            => __( 'Reopened on WordPress.org', 'upgrade-pilot' ),
	'compromise'          => __( 'Known-compromise advisory', 'upgrade-pilot' ),
	'abandonment'         => __( 'Abandonment threshold crossed', 'upgrade-pilot' ),
);
?>
<div class="wrap upilot-wrap">
	<h1><?php esc_html_e( 'Upgrade Pilot — Update Trust', 'upgrade-pilot' ); ?></h1>

	<p class="upilot-explainer">
		<?php esc_html_e( 'Upgrade Pilot takes a daily snapshot of every installed plugin\'s public WordPress.org record and tells you when something changes: the author or contributors change (plugins that change hands deserve a closer look before their next update), the plugin is closed, or it crosses your abandonment threshold. This is change detection and closure alerting — an early-warning custody log, not malware scanning.', 'upgrade-pilot' ); ?>
	</p>

	<p>
		<button type="button" class="button button-primary" id="upilot-run-snapshot"><?php esc_html_e( 'Check now (fresh data)', 'upgrade-pilot' ); ?></button>
		<span class="upilot-progress" id="upilot-snap-progress" hidden>
			<span class="spinner is-active" style="float:none;margin:0 8px 0 0;"></span>
			<span><?php esc_html_e( 'Fetching fresh WordPress.org records…', 'upgrade-pilot' ); ?></span>
		</span>
		<?php if ( $upilot_last_run ) : ?>
			<span class="upilot-muted">
				<?php
				printf(
					/* translators: %s: human time diff. */
					esc_html__( 'Last check: %s ago.', 'upgrade-pilot' ),
					esc_html( human_time_diff( $upilot_last_run ) )
				);
				?>
			</span>
		<?php endif; ?>
	</p>

	<h2><?php esc_html_e( 'Recent events', 'upgrade-pilot' ); ?></h2>
	<?php if ( empty( $events ) ) : ?>
		<p><?php esc_html_e( 'No events recorded yet. After the first snapshot, changes will appear here.', 'upgrade-pilot' ); ?></p>
	<?php else : ?>
		<table class="widefat striped upilot-table">
			<thead>
				<tr>
					<th style="width:30px;"></th>
					<th style="width:150px;"><?php esc_html_e( 'When', 'upgrade-pilot' ); ?></th>
					<th style="width:220px;"><?php esc_html_e( 'Plugin', 'upgrade-pilot' ); ?></th>
					<th><?php esc_html_e( 'What happened', 'upgrade-pilot' ); ?></th>
					<th style="width:110px;"><?php esc_html_e( 'Action', 'upgrade-pilot' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $events as $upilot_event ) : ?>
					<?php
					$upilot_details = json_decode( (string) $upilot_event['details'], true );
					if ( ! is_array( $upilot_details ) ) {
						$upilot_details = array();
					}
					$upilot_dot = 'critical' === $upilot_event['severity'] ? 'fail' : ( 'warning' === $upilot_event['severity'] ? 'warn' : 'pass' );
					$upilot_extra = '';
					if ( 'author_change' === $upilot_event['event_type'] && isset( $upilot_details['from'], $upilot_details['to'] ) ) {
						$upilot_extra = sprintf( ' %s → %s', $upilot_details['from'], $upilot_details['to'] );
					} elseif ( 'contributor_added' === $upilot_event['event_type'] && ! empty( $upilot_details['added'] ) ) {
						$upilot_extra = ' ' . implode( ', ', (array) $upilot_details['added'] );
					} elseif ( 'closed' === $upilot_event['event_type'] && ! empty( $upilot_details['reason'] ) ) {
						/* translators: %s: closure reason. */
						$upilot_extra = ' ' . sprintf( __( 'Reason: %s', 'upgrade-pilot' ), $upilot_details['reason'] );
					} elseif ( 'compromise' === $upilot_event['event_type'] && ! empty( $upilot_details['detail'] ) ) {
						$upilot_extra = ' ' . $upilot_details['detail'];
					}
					?>
					<tr class="<?php echo $upilot_event['acknowledged'] ? 'upilot-suppressed' : ''; ?>">
						<td><span class="upilot-dot upilot-dot--<?php echo esc_attr( $upilot_dot ); ?>"></span></td>
						<td><?php echo esc_html( get_date_from_gmt( $upilot_event['created_at'], get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) ); ?></td>
						<td><strong><?php echo esc_html( isset( $upilot_details['name'] ) ? $upilot_details['name'] : $upilot_event['slug'] ); ?></strong></td>
						<td>
							<?php echo esc_html( isset( $upilot_event_labels[ $upilot_event['event_type'] ] ) ? $upilot_event_labels[ $upilot_event['event_type'] ] : $upilot_event['event_type'] ); ?><?php echo esc_html( $upilot_extra ); ?>
						</td>
						<td>
							<?php if ( ! $upilot_event['acknowledged'] ) : ?>
								<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
									<?php wp_nonce_field( 'upilot_ack' ); ?>
									<input type="hidden" name="action" value="upilot_ack_event" />
									<input type="hidden" name="event_id" value="<?php echo esc_attr( $upilot_event['id'] ); ?>" />
									<button type="submit" class="button-link"><?php esc_html_e( 'Acknowledge', 'upgrade-pilot' ); ?></button>
								</form>
							<?php endif; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	<?php endif; ?>

	<h2><?php esc_html_e( 'Plugin custody & update freeze', 'upgrade-pilot' ); ?></h2>
	<p class="upilot-explainer">
		<?php esc_html_e( 'Freezing a plugin blocks its AUTOMATIC updates only — manual updates stay visible and possible, and Upgrade Pilot warns loudly if a frozen plugin ever appears on a known-compromise advisory (never hold back a security fix).', 'upgrade-pilot' ); ?>
	</p>
	<table class="widefat striped upilot-table">
		<thead>
			<tr>
				<th style="width:260px;"><?php esc_html_e( 'Plugin', 'upgrade-pilot' ); ?></th>
				<th style="width:110px;"><?php esc_html_e( 'Source', 'upgrade-pilot' ); ?></th>
				<th><?php esc_html_e( 'WordPress.org record', 'upgrade-pilot' ); ?></th>
				<th style="width:160px;"><?php esc_html_e( 'Auto-updates', 'upgrade-pilot' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ( (array) $map as $upilot_file => $upilot_entry ) : ?>
				<?php
				$upilot_is_frozen = isset( $frozen[ $upilot_file ] );
				$upilot_snap      = isset( $states[ $upilot_entry['slug'] ] ) ? $states[ $upilot_entry['slug'] ] : null;
				$upilot_is_self   = ( UPILOT_BASENAME === $upilot_file );
				?>
				<tr>
					<td>
						<strong><?php echo esc_html( $upilot_entry['name'] ); ?></strong>
						<span class="upilot-muted"><?php echo esc_html( $upilot_entry['version'] ); ?></span>
						<?php if ( empty( $upilot_entry['active'] ) ) : ?>
							<span class="upilot-badge"><?php esc_html_e( 'inactive', 'upgrade-pilot' ); ?></span>
						<?php endif; ?>
					</td>
					<td>
						<?php
						if ( 'wporg' === $upilot_entry['class'] ) {
							esc_html_e( 'WordPress.org', 'upgrade-pilot' );
						} elseif ( 'external' === $upilot_entry['class'] ) {
							esc_html_e( 'External', 'upgrade-pilot' );
						} else {
							esc_html_e( 'Unverified', 'upgrade-pilot' );
						}
						?>
					</td>
					<td>
						<?php
						if ( 'wporg' !== $upilot_entry['class'] ) {
							echo '<span class="upilot-muted">' . esc_html__( 'Not monitored (no public wp.org record).', 'upgrade-pilot' ) . '</span>';
						} elseif ( $upilot_snap ) {
							if ( 'closed' === $upilot_snap['state'] ) {
								echo '<span class="upilot-dot upilot-dot--fail"></span> ' . esc_html__( 'CLOSED on WordPress.org', 'upgrade-pilot' );
							} else {
								$upilot_author = isset( $upilot_snap['data']['author'] ) ? $upilot_snap['data']['author'] : '';
								$upilot_contrib_count = isset( $upilot_snap['data']['contributors'] ) ? count( (array) $upilot_snap['data']['contributors'] ) : 0;
								printf(
									/* translators: 1: author, 2: contributor count, 3: last-updated date. */
									esc_html__( 'Author: %1$s · %2$d contributor(s) · last plugin update %3$s', 'upgrade-pilot' ),
									esc_html( $upilot_author ? $upilot_author : '—' ),
									(int) $upilot_contrib_count,
									esc_html( isset( $upilot_snap['data']['last_updated'] ) && $upilot_snap['data']['last_updated'] ? mysql2date( get_option( 'date_format' ), $upilot_snap['data']['last_updated'] ) : '—' )
								);
							}
						} else {
							echo '<span class="upilot-muted">' . esc_html__( 'No snapshot yet — run "Check now".', 'upgrade-pilot' ) . '</span>';
						}
						?>
					</td>
					<td>
						<?php if ( $upilot_is_self ) : ?>
							<span class="upilot-muted"><?php esc_html_e( 'n/a (this plugin)', 'upgrade-pilot' ); ?></span>
						<?php else : ?>
							<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
								<?php wp_nonce_field( 'upilot_freeze' ); ?>
								<input type="hidden" name="action" value="upilot_freeze" />
								<input type="hidden" name="plugin_file" value="<?php echo esc_attr( $upilot_file ); ?>" />
								<?php if ( $upilot_is_frozen ) : ?>
									<input type="hidden" name="undo" value="1" />
									<span class="upilot-badge upilot-badge--frozen"><?php esc_html_e( 'held', 'upgrade-pilot' ); ?></span>
									<button type="submit" class="button-link"><?php esc_html_e( 'Unfreeze', 'upgrade-pilot' ); ?></button>
								<?php else : ?>
									<button type="submit" class="button-link"><?php esc_html_e( 'Freeze', 'upgrade-pilot' ); ?></button>
								<?php endif; ?>
							</form>
						<?php endif; ?>
					</td>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
</div>
