<?php
/**
 * Vérification environnement serveur avant scan.
 *
 * @package PCC
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class PCC_Environment {

	/**
	 * @return array{ok:bool,messages:array<int,string>,details:array<string,string>}
	 */
	public static function check() {
		$messages = array();
		$ok       = true;

		$memory = ini_get( 'memory_limit' );
		$bytes  = self::memory_to_bytes( $memory );
		if ( $bytes > 0 && $bytes < 128 * 1024 * 1024 ) {
			$messages[] = sprintf(
				/* translators: %s: memory limit */
				__( 'Low memory limit (%s) — large scans may fail. 128M+ recommended.', 'php-compatibility-checker-by-volade' ),
				$memory
			);
		}

		$max_time = (int) ini_get( 'max_execution_time' );
		if ( $max_time > 0 && $max_time < 30 ) {
			$messages[] = sprintf(
				/* translators: %d: seconds */
				__( 'Short max_execution_time (%ds) — batch scanning mitigates this.', 'php-compatibility-checker-by-volade' ),
				$max_time
			);
		}

		if ( ! function_exists( 'token_get_all' ) ) {
			$ok         = false;
			$messages[] = __( 'PHP tokenizer unavailable — cannot scan files.', 'php-compatibility-checker-by-volade' );
		}

		$vendor = is_readable( PCC_DIR . 'vendor/autoload.php' );
		if ( ! $vendor ) {
			$messages[] = __( 'PHPCompatibilityWP not bundled — using built-in rules (install Composer vendor for deeper analysis).', 'php-compatibility-checker-by-volade' );
		}

		$extensions = self::check_php_extensions();
		foreach ( $extensions['missing'] as $ext ) {
			$messages[] = sprintf(
				/* translators: %s: extension name */
				__( 'PHP extension %s is not loaded — some plugins may require it.', 'php-compatibility-checker-by-volade' ),
				$ext
			);
		}

		return array(
			'ok'       => $ok,
			'messages' => $messages,
			'details'  => array(
				'php'              => phpversion(),
				'memory_limit'     => $memory,
				'max_execution'    => (string) $max_time,
				'phpcs_bundled'    => $vendor ? 'yes' : 'no',
				'batch_recommended'=> (string) self::recommended_batch_size(),
				'extensions'       => $extensions,
			),
		);
	}

	/**
	 * @return array{loaded:array<int,string>,missing:array<int,string>}
	 */
	public static function check_php_extensions() {
		$recommended = array( 'json', 'mbstring', 'curl', 'openssl', 'mysqli', 'intl', 'dom', 'xml' );
		$loaded      = array();
		$missing     = array();
		foreach ( $recommended as $ext ) {
			if ( extension_loaded( $ext ) ) {
				$loaded[] = $ext;
			} else {
				$missing[] = $ext;
			}
		}
		return array(
			'loaded'  => $loaded,
			'missing' => $missing,
		);
	}

	public static function recommended_batch_size() {
		$bytes = self::memory_to_bytes( ini_get( 'memory_limit' ) );
		if ( $bytes > 0 && $bytes < 128 * 1024 * 1024 ) {
			return 10;
		}
		if ( $bytes > 0 && $bytes < 256 * 1024 * 1024 ) {
			return 25;
		}
		return 50;
	}

	private static function memory_to_bytes( $value ) {
		$value = trim( (string) $value );
		if ( '' === $value || '-1' === $value ) {
			return -1;
		}
		$unit = strtolower( substr( $value, -1 ) );
		$num  = (float) $value;
		switch ( $unit ) {
			case 'g':
				return (int) ( $num * 1024 * 1024 * 1024 );
			case 'm':
				return (int) ( $num * 1024 * 1024 );
			case 'k':
				return (int) ( $num * 1024 );
			default:
				return (int) $num;
		}
	}
}
