<?php
/**
 * Runtime requirement checks.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Support;

/**
 * Evaluates platform requirements without mutating site state.
 */
final class Requirements {
	/**
	 * Returns all blocking requirement messages.
	 *
	 * @return list<string>
	 */
	public function errors(): array {
		global $wp_version;

		$errors = array();

		if ( ! VersionPolicy::meets( PHP_VERSION, VersionPolicy::MIN_PHP ) ) {
			$errors[] = sprintf(
				/* translators: 1: required PHP version, 2: current PHP version. */
				__( 'JJ\'s Post Scheduler requires PHP %1$s or newer. This site is running PHP %2$s.', 'jjs-post-scheduler' ),
				VersionPolicy::MIN_PHP,
				PHP_VERSION
			);
		}

		if ( isset( $wp_version ) && ! VersionPolicy::meets( $wp_version, VersionPolicy::MIN_WORDPRESS ) ) {
			$errors[] = sprintf(
				/* translators: 1: required WordPress version, 2: current WordPress version. */
				__( 'JJ\'s Post Scheduler requires WordPress %1$s or newer. This site is running WordPress %2$s.', 'jjs-post-scheduler' ),
				VersionPolicy::MIN_WORDPRESS,
				$wp_version
			);
		}

		$database = VersionPolicy::databaseEnvironment();

		if ( 'unsupported' === $database['status'] ) {
			$engine   = 'mariadb' === $database['engine'] ? 'MariaDB' : 'MySQL';
			$errors[] = sprintf(
				/* translators: 1: database engine, 2: required version, 3: current version. */
				__( 'JJ\'s Post Scheduler requires %1$s %2$s or newer. This site is running %1$s %3$s.', 'jjs-post-scheduler' ),
				$engine,
				$database['minimum'],
				$database['version']
			);
		}

		return $errors;
	}

	/**
	 * Returns supported-but-not-recommended runtime notices.
	 *
	 * @return list<string>
	 */
	public function recommendations(): array {
		global $wp_version;

		$recommended = VersionPolicy::recommended();
		$messages    = array();

		if ( isset( $recommended['php'] ) && VersionPolicy::meets( PHP_VERSION, VersionPolicy::MIN_PHP ) && ! VersionPolicy::meets( PHP_VERSION, $recommended['php'] ) ) {
			$messages[] = sprintf(
				/* translators: 1: recommended PHP version, 2: current PHP version. */
				__( 'JJ\'s Post Scheduler supports this PHP version, but PHP %1$s or newer is recommended. This site is running PHP %2$s.', 'jjs-post-scheduler' ),
				$recommended['php'],
				PHP_VERSION
			);
		}

		if ( isset( $recommended['wordpress'], $wp_version ) && 'latest' !== $recommended['wordpress'] && VersionPolicy::meets( $wp_version, VersionPolicy::MIN_WORDPRESS ) && ! VersionPolicy::meets( $wp_version, $recommended['wordpress'] ) ) {
			$messages[] = sprintf(
				/* translators: 1: recommended WordPress version, 2: current WordPress version. */
				__( 'JJ\'s Post Scheduler supports this WordPress version, but WordPress %1$s or newer is recommended. This site is running WordPress %2$s.', 'jjs-post-scheduler' ),
				$recommended['wordpress'],
				$wp_version
			);
		}

		$database = VersionPolicy::databaseEnvironment();

		if ( 'supported_legacy' === $database['status'] ) {
			$engine     = 'mariadb' === $database['engine'] ? 'MariaDB' : 'MySQL';
			$messages[] = sprintf(
				/* translators: 1: database engine, 2: recommended version, 3: current version. */
				__( 'JJ\'s Post Scheduler supports this database version, but %1$s %2$s or newer is recommended. This site is running %1$s %3$s.', 'jjs-post-scheduler' ),
				$engine,
				$database['recommended'],
				$database['version']
			);
		}

		return $messages;
	}

	/**
	 * Determines whether all requirements are satisfied.
	 */
	public function satisfied(): bool {
		global $wp_version;

		if ( ! VersionPolicy::meets( PHP_VERSION, VersionPolicy::MIN_PHP ) ) {
			return false;
		}

		if ( isset( $wp_version ) && ! VersionPolicy::meets( $wp_version, VersionPolicy::MIN_WORDPRESS ) ) {
			return false;
		}

		return 'unsupported' !== VersionPolicy::databaseEnvironment()['status'];
	}
}
