<?php
/**
 * Widget tableau de bord + test Site Health.
 *
 * @package PCC
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class PCC_Health {

	/** @var PCC_Plugin */
	private $plugin;

	public function __construct( PCC_Plugin $plugin ) {
		$this->plugin = $plugin;

		add_action( 'wp_dashboard_setup', array( $this, 'register_dashboard_widget' ) );
		add_filter( 'site_status_tests', array( $this, 'register_site_health_test' ) );
	}

	public function register_dashboard_widget() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		wp_add_dashboard_widget(
			'pcc_dashboard_widget',
			__( 'PHP Compatibility', 'php-compatibility-checker-by-volade' ),
			array( $this, 'render_dashboard_widget' )
		);
	}

	public function render_dashboard_widget() {
		$scan   = $this->plugin->settings->get_last_scan();
		$url    = admin_url( PCC_Admin::instance_settings_path( $this->plugin ) );
		$stats  = $this->readiness_stats( $scan );
		$verdict = $scan['verdict'] ?? 'unknown';

		if ( empty( $scan['completed_at'] ) ) {
			echo '<p>' . esc_html__( 'No scan yet — check PHP compatibility before upgrading.', 'php-compatibility-checker-by-volade' ) . '</p>';
			echo '<p><a class="button button-primary" href="' . esc_url( $url ) . '">' . esc_html__( 'Run scan', 'php-compatibility-checker-by-volade' ) . '</a></p>';
			return;
		}

		echo '<p><strong>' . esc_html( $this->plugin->scanner->verdict_label( $verdict ) ) . '</strong></p>';
		printf(
			'<p>%s</p>',
			esc_html(
				sprintf(
					/* translators: 1: ready count, 2: total, 3: target PHP */
					__( '%1$d / %2$d components ready for PHP %3$s', 'php-compatibility-checker-by-volade' ),
					$stats['ready'],
					$stats['total'],
					$scan['target_php'] ?? ''
				)
			)
		);
		printf(
			'<p class="description">%s</p>',
			esc_html(
				sprintf(
					/* translators: 1: error count, 2: warning count */
					__( '%1$d errors · %2$d warnings — last scan %3$s', 'php-compatibility-checker-by-volade' ),
					(int) ( $scan['summary']['errors'] ?? 0 ),
					(int) ( $scan['summary']['warnings'] ?? 0 ),
					$scan['completed_at'] ?? ''
				)
			)
		);
		echo '<p><a class="button button-secondary" href="' . esc_url( $url ) . '">' . esc_html__( 'View report', 'php-compatibility-checker-by-volade' ) . '</a></p>';
	}

	/**
	 * @param array<string, mixed> $scan
	 * @return array{ready:int,total:int}
	 */
	public function readiness_stats( $scan ) {
		$by    = $scan['by_component'] ?? array();
		$total = count( $by );
		$ready = 0;
		foreach ( $by as $row ) {
			if ( empty( $row['errors'] ) && empty( $row['warnings'] ) ) {
				$ready++;
			}
		}
		return array(
			'ready' => $ready,
			'total' => $total,
		);
	}

	/**
	 * @param array<string, mixed> $tests
	 * @return array<string, mixed>
	 */
	public function register_site_health_test( $tests ) {
		$tests['direct']['pcc_php_upgrade'] = array(
			'label' => __( 'PHP upgrade readiness', 'php-compatibility-checker-by-volade' ),
			'test'  => array( $this, 'run_site_health_test' ),
		);
		return $tests;
	}

	/**
	 * @return array{label:string,status:string,badge:array,actions:string,test:string,description:string}
	 */
	public function run_site_health_test() {
		$scan    = $this->plugin->settings->get_last_scan();
		$url     = admin_url( PCC_Admin::instance_settings_path( $this->plugin ) );
		$runtime = $this->plugin->runtime->summary();

		if ( empty( $scan['completed_at'] ) ) {
			return array(
				'label'       => __( 'PHP upgrade readiness', 'php-compatibility-checker-by-volade' ),
				'status'      => 'recommended',
				'badge'       => array(
					'label' => __( 'Security', 'php-compatibility-checker-by-volade' ),
					'color' => 'blue',
				),
				'actions'     => sprintf(
					'<a href="%s">%s</a>',
					esc_url( $url ),
					esc_html__( 'Run PHP compatibility scan', 'php-compatibility-checker-by-volade' )
				),
				'test'        => 'pcc_php_upgrade',
				'description' => sprintf(
					'<p>%s</p>',
					esc_html__( 'No compatibility scan has been run yet.', 'php-compatibility-checker-by-volade' )
				),
			);
		}

		$verdict = $scan['verdict'] ?? 'unknown';
		$status  = 'good';
		if ( 'blocked' === $verdict ) {
			$status = 'critical';
		} elseif ( 'caution' === $verdict ) {
			$status = 'recommended';
		}

		$desc = sprintf(
			/* translators: 1: target PHP, 2: errors, 3: warnings */
			__( 'Last scan for PHP %1$s: %2$d errors, %3$d warnings.', 'php-compatibility-checker-by-volade' ),
			$scan['target_php'] ?? '',
			(int) ( $scan['summary']['errors'] ?? 0 ),
			(int) ( $scan['summary']['warnings'] ?? 0 )
		);

		if ( $runtime['count'] > 0 ) {
			$desc .= ' ' . sprintf(
				/* translators: %d: number of runtime deprecation events */
				__( '%d runtime deprecation event(s) captured since last clear.', 'php-compatibility-checker-by-volade' ),
				$runtime['count']
			);
		}

		return array(
			'label'       => __( 'PHP upgrade readiness', 'php-compatibility-checker-by-volade' ),
			'status'      => $status,
			'badge'       => array(
				'label' => __( 'Security', 'php-compatibility-checker-by-volade' ),
				'color' => 'blue',
			),
			'actions'     => sprintf(
				'<a href="%s">%s</a>',
				esc_url( $url ),
				esc_html__( 'View compatibility report', 'php-compatibility-checker-by-volade' )
			),
			'test'        => 'pcc_php_upgrade',
			'description' => sprintf( '<p>%s</p>', esc_html( $desc ) ),
		);
	}
}
