<?php
/**
 * Dashboard widget template (F4).
 *
 * @var array   $breakdown     Score + component counts from HealthScorer.
 * @var string  $badge_class   Badge colour class for the score.
 * @var ?object $last_event    Most recent update event, or null.
 * @var string  $dashboard_url URL to the CR dashboard page.
 *
 * @package ConflictRadar\Admin
 */

if ( ! defined( 'ABSPATH' ) ) { exit; }
?>
<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>
	<strong><?php esc_html_e( 'Site Health Score', 'conflict-radar' ); ?></strong>
</div>

<ul class="cr-widget-stats">
	<li>
		<span><?php esc_html_e( 'Updates (last 7 days)', 'conflict-radar' ); ?></span>
		<span><?php echo esc_html( (string) (int) $breakdown['recent_update_count'] ); ?></span>
	</li>
	<li>
		<span><?php esc_html_e( 'Fatal errors (last 24h)', 'conflict-radar' ); ?></span>
		<span class="cr-type-fatal"><?php echo esc_html( (string) (int) $breakdown['fatal_count'] ); ?></span>
	</li>
	<li>
		<span><?php esc_html_e( 'Warnings (last 24h)', 'conflict-radar' ); ?></span>
		<span class="cr-type-warning"><?php echo esc_html( (string) (int) $breakdown['warning_count'] ); ?></span>
	</li>
	<li>
		<span><?php esc_html_e( 'Last update', 'conflict-radar' ); ?></span>
		<span>
			<?php
			if ( $last_event ) {
				$cr_name = '' !== (string) $last_event->plugin_name ? (string) $last_event->plugin_name : (string) $last_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) $last_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.
				echo esc_html( $cr_name );
				if ( $cr_ts ) {
					echo ' — ' . esc_html( wp_date( 'Y-m-d H:i', $cr_ts ) );
				}
			} else {
				esc_html_e( 'None yet', 'conflict-radar' );
			}
			?>
		</span>
	</li>
</ul>

<p>
	<a href="<?php echo esc_url( $dashboard_url ); ?>"><?php esc_html_e( 'View Conflict Radar dashboard', 'conflict-radar' ); ?> &rarr;</a>
</p>
