<?php
/**
 * Dashboard page template (F3/F4 overview).
 *
 * @var array  $breakdown   Score + component counts from HealthScorer.
 * @var string $badge_class Badge colour class for the score.
 * @var array  $events      Recent update events (objects).
 *
 * @package ConflictRadar\Admin
 */

if ( ! defined( 'ABSPATH' ) ) { exit; }
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Conflict Radar', 'conflict-radar' ); ?></h1>

	<div class="cr-score-panel">
		<span class="cr-badge cr-score-large <?php echo esc_attr( $badge_class ); ?>">
			<?php echo esc_html( (string) $breakdown['score'] ); ?>
		</span>
		<div>
			<strong><?php esc_html_e( 'Site Health Score', 'conflict-radar' ); ?></strong>
			<ul class="cr-breakdown">
				<li>
					<?php
					/* translators: %d: number of fatal errors in the last 24 hours. */
					echo esc_html( sprintf( _n( '%d fatal error (last 24h)', '%d fatal errors (last 24h)', (int) $breakdown['fatal_count'], 'conflict-radar' ), (int) $breakdown['fatal_count'] ) );
					?>
				</li>
				<li>
					<?php
					/* translators: %d: number of warnings in the last 24 hours. */
					echo esc_html( sprintf( _n( '%d warning (last 24h)', '%d warnings (last 24h)', (int) $breakdown['warning_count'], 'conflict-radar' ), (int) $breakdown['warning_count'] ) );
					?>
				</li>
				<li>
					<?php
					/* translators: %d: number of update events in the last 7 days. */
					echo esc_html( sprintf( _n( '%d update (last 7 days)', '%d updates (last 7 days)', (int) $breakdown['recent_update_count'], 'conflict-radar' ), (int) $breakdown['recent_update_count'] ) );
					?>
				</li>
				<li>
					<?php
					/* translators: %d: total errors logged in the last 7 days. */
					echo esc_html( sprintf( _n( '%d error logged (last 7 days)', '%d errors logged (last 7 days)', (int) $breakdown['errors_last_7_days'], 'conflict-radar' ), (int) $breakdown['errors_last_7_days'] ) );
					?>
				</li>
			</ul>
		</div>
	</div>

	<h2><?php esc_html_e( 'Recent Events', 'conflict-radar' ); ?></h2>
	<table class="widefat striped">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Type', 'conflict-radar' ); ?></th>
				<th><?php esc_html_e( 'Plugin / Theme', 'conflict-radar' ); ?></th>
				<th><?php esc_html_e( 'From', 'conflict-radar' ); ?></th>
				<th><?php esc_html_e( 'To', 'conflict-radar' ); ?></th>
				<th><?php esc_html_e( 'Date', 'conflict-radar' ); ?></th>
			</tr>
		</thead>
		<tbody>
		<?php if ( empty( $events ) ) : ?>
			<tr><td colspan="5"><?php esc_html_e( 'No update events recorded yet.', 'conflict-radar' ); ?></td></tr>
		<?php else : ?>
			<?php foreach ( $events as $cr_event ) : // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Function-local: template is require'd inside a render() method; method-scoped local, not a global. Sniff can't see the include scope. ?>
				<?php
				$cr_name     = '' !== (string) $cr_event->plugin_name ? (string) $cr_event->plugin_name : (string) $cr_event->plugin_slug; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Function-local: template is require'd inside a render() method; method-scoped local, not a global. Sniff can't see the include scope.
				$cr_ts       = strtotime( (string) $cr_event->created_at . ' UTC' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Function-local: template is require'd inside a render() method; method-scoped local, not a global. Sniff can't see the include scope.
				$cr_type_lbl = ucwords( str_replace( '_', ' ', (string) $cr_event->event_type ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Function-local: template is require'd inside a render() method; method-scoped local, not a global. Sniff can't see the include scope.
				?>
				<tr>
					<td><?php echo esc_html( $cr_type_lbl ); ?></td>
					<td><?php echo esc_html( $cr_name ); ?></td>
					<td><?php echo esc_html( (string) $cr_event->version_from ); ?></td>
					<td><?php echo esc_html( (string) $cr_event->version_to ); ?></td>
					<td><?php echo $cr_ts ? esc_html( wp_date( 'Y-m-d H:i', $cr_ts ) ) : ''; ?></td>
				</tr>
			<?php endforeach; ?>
		<?php endif; ?>
		</tbody>
	</table>
</div>
