<?php
/**
 * Diagnostics — pourquoi le bouton Réinstaller n'apparaît pas, blocages, lock.
 *
 * @package CVS
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class CVS_Diagnostics {

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

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

	/**
	 * @return array{ok:bool,checks:array<int,array<string,mixed>>}
	 */
	public function run() {
		$checks = array();
		$target = CVS_Settings::sanitize_version( $this->plugin->settings->get( 'target_version' ) );

		$checks[] = $this->check(
			'capability',
			__( 'Permission update_core', 'core-version-switcher-by-volade' ),
			current_user_can( 'update_core' ) ? 'pass' : 'fail',
			current_user_can( 'update_core' )
				? __( 'Your account can update WordPress core.', 'core-version-switcher-by-volade' )
				: __( 'You need update_core capability.', 'core-version-switcher-by-volade' )
		);

		$checks[] = $this->check(
			'target',
			__( 'Target version', 'core-version-switcher-by-volade' ),
			$target ? 'pass' : 'warn',
			$target
				? sprintf(
					/* translators: %s: version */
					__( 'Pinned to %s.', 'core-version-switcher-by-volade' ),
					$target
				)
				: __( 'No target set — WordPress shows normal updates only.', 'core-version-switcher-by-volade' )
		);

		if ( $target ) {
			$zip      = $this->plugin->settings->build_download_url( $target );
			$ok       = $this->plugin->settings->zip_exists( $zip );
			$checks[] = $this->check(
				'package',
				__( 'Download package', 'core-version-switcher-by-volade' ),
				$ok ? 'pass' : 'fail',
				$ok
					? __( 'ZIP exists on wordpress.org.', 'core-version-switcher-by-volade' )
					: __( 'ZIP not found — pick another version or locale.', 'core-version-switcher-by-volade' )
			);

			$status   = CVS_Releases::get_security_status( $target );
			$checks[] = $this->check(
				'security',
				__( 'Security status', 'core-version-switcher-by-volade' ),
				( 'insecure' === $status ) ? 'warn' : 'pass',
				self::security_label( $status )
			);

			$php_ok   = CVS_Releases::php_compatible_with_target( $target );
			$checks[] = $this->check(
				'php',
				__( 'PHP compatibility', 'core-version-switcher-by-volade' ),
				$php_ok ? 'pass' : 'fail',
				$php_ok
					? sprintf(
						/* translators: 1: PHP version, 2: estimated minimum */
						__( 'PHP %1$s meets the estimated minimum (%2$s) for this release.', 'core-version-switcher-by-volade' ),
						PHP_VERSION,
						CVS_Releases::estimated_min_php( $target )
					)
					: sprintf(
						/* translators: 1: PHP version, 2: estimated minimum */
						__( 'PHP %1$s may be too new for WordPress %2$s (estimated min %3$s).', 'core-version-switcher-by-volade' ),
						PHP_VERSION,
						$target,
						CVS_Releases::estimated_min_php( $target )
					)
			);
		}

		$lock     = get_option( 'core_updater.lock' );
		$checks[] = $this->check(
			'lock',
			__( 'Updater lock', 'core-version-switcher-by-volade' ),
			$lock ? 'fail' : 'pass',
			$lock
				? __( 'core_updater.lock is set — another update may be stuck.', 'core-version-switcher-by-volade' )
				: __( 'No updater lock.', 'core-version-switcher-by-volade' )
		);

		if ( defined( 'WP_AUTO_UPDATE_CORE' ) && false === WP_AUTO_UPDATE_CORE ) {
			$checks[] = $this->check(
				'auto_constant',
				'WP_AUTO_UPDATE_CORE',
				'warn',
				__( 'Defined as false in wp-config.php — automatic core updates disabled globally.', 'core-version-switcher-by-volade' )
			);
		}

		if ( file_exists( WPMU_PLUGIN_DIR . '/gd-system-plugin.php' ) ) {
			$checks[] = $this->check(
				'hosting_mu',
				__( 'Hosting MU-plugin', 'core-version-switcher-by-volade' ),
				'warn',
				__( 'GoDaddy gd-system-plugin detected — may restrict core updates on managed hosting.', 'core-version-switcher-by-volade' )
			);
		}

		$conflicts = CVS_Settings::detect_conflicts();
		if ( ! empty( $conflicts ) ) {
			$checks[] = $this->check(
				'conflict',
				__( 'Conflicting plugins', 'core-version-switcher-by-volade' ),
				'fail',
				implode( ', ', wp_list_pluck( $conflicts, 'name' ) )
			);
		}

		$failed = array_filter(
			$checks,
			static function ( $c ) {
				return 'fail' === $c['status'];
			}
		);

		return array(
			'ok'     => empty( $failed ),
			'checks' => $checks,
		);
	}

	private static function security_label( $status ) {
		switch ( $status ) {
			case 'latest':
				return __( 'Latest stable release.', 'core-version-switcher-by-volade' );
			case 'outdated':
				return __( 'Outdated but still listed — security patches may be missing.', 'core-version-switcher-by-volade' );
			case 'insecure':
				return __( 'Marked insecure by wordpress.org — avoid except for short diagnostics.', 'core-version-switcher-by-volade' );
			default:
				return __( 'Unknown security status.', 'core-version-switcher-by-volade' );
		}
	}

	private function check( $key, $label, $status, $message ) {
		return array(
			'key'     => $key,
			'label'   => $label,
			'status'  => $status,
			'message' => (string) $message,
		);
	}
}
