<?php
/**
 * Compatibilité plugins / thèmes avec la version WordPress cible.
 *
 * @package CVS
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class CVS_Compat {

	/**
	 * @param string $target_wp_version
	 * @return array{plugins:array<int,array>,themes:array<int,array>}
	 */
	public static function scan( $target_wp_version ) {
		$target_wp_version = CVS_Settings::sanitize_version( $target_wp_version );
		if ( ! $target_wp_version ) {
			return array(
				'plugins' => array(),
				'themes'  => array(),
			);
		}

		return array(
			'plugins' => self::scan_plugins( $target_wp_version ),
			'themes'  => self::scan_themes( $target_wp_version ),
		);
	}

	/**
	 * @param string $target_wp_version
	 * @return array<int,array{name:string,slug:string,issue:string,severity:string}>
	 */
	public static function scan_plugins( $target_wp_version ) {
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$issues = array();
		foreach ( get_plugins() as $file => $data ) {
			if ( ! is_plugin_active( $file ) ) {
				continue;
			}

			$row = self::check_headers(
				$data['Name'] ?? $file,
				$file,
				$data,
				$target_wp_version
			);
			if ( $row ) {
				$issues[] = $row;
			}
		}

		return $issues;
	}

	/**
	 * @param string $target_wp_version
	 * @return array<int,array{name:string,slug:string,issue:string,severity:string}>
	 */
	public static function scan_themes( $target_wp_version ) {
		$theme  = wp_get_theme();
		$issues = array();

		$rows = array( $theme );
		if ( $theme->parent() ) {
			$rows[] = $theme->parent();
		}

		foreach ( $rows as $item ) {
			if ( ! $item || ! $item->exists() ) {
				continue;
			}

			$headers = array(
				'Name'              => $item->get( 'Name' ),
				'RequiresWP'        => $item->get( 'RequiresWP' ) ?: $item->get( 'Requires at least' ),
				'Tested up to'      => $item->get( 'Tested up to' ),
				'Requires at least' => $item->get( 'Requires at least' ),
			);

			$row = self::check_headers(
				$item->get( 'Name' ),
				$item->get_stylesheet(),
				$headers,
				$target_wp_version
			);
			if ( $row ) {
				$issues[] = $row;
			}
		}

		return $issues;
	}

	/**
	 * @param string $name
	 * @param string $slug
	 * @param array  $data
	 * @param string $target_wp_version
	 * @return array|null
	 */
	private static function check_headers( $name, $slug, array $data, $target_wp_version ) {
		$requires = self::header_value( $data, array( 'RequiresWP', 'Requires at least' ) );
		$tested   = self::header_value( $data, array( 'Tested up to', 'TestedUpTo' ) );

		if ( $requires && version_compare( $target_wp_version, $requires, '<' ) ) {
			return array(
				'name'     => (string) $name,
				'slug'     => (string) $slug,
				'issue'    => sprintf(
					/* translators: 1: required WP version, 2: target version */
					__( 'Requires WordPress %1$s — target is %2$s.', 'core-version-switcher-by-volade' ),
					$requires,
					$target_wp_version
				),
				'severity' => 'error',
			);
		}

		if ( $tested && version_compare( $target_wp_version, $tested, '>' ) ) {
			return array(
				'name'     => (string) $name,
				'slug'     => (string) $slug,
				'issue'    => sprintf(
					/* translators: 1: tested up to version, 2: target version */
					__( 'Tested up to %1$s only — target %2$s is newer.', 'core-version-switcher-by-volade' ),
					$tested,
					$target_wp_version
				),
				'severity' => 'warning',
			);
		}

		return null;
	}

	/**
	 * @param array             $data
	 * @param array<int,string> $keys
	 */
	private static function header_value( array $data, array $keys ) {
		foreach ( $keys as $key ) {
			if ( ! empty( $data[ $key ] ) ) {
				return trim( (string) $data[ $key ] );
			}
		}
		return '';
	}

	public static function pcc_plugin_active() {
		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		return is_plugin_active( 'php-compatibility-checker-by-volade/php-compatibility-checker-by-volade.php' );
	}

	public static function pcc_admin_url() {
		if ( class_exists( 'Volade_SDK' ) && Volade_SDK::get_instance() ) {
			$slug = Volade_SDK::get_plugin_page_slug( 'php-compatibility-checker-by-volade' );
			if ( $slug ) {
				return admin_url( 'admin.php?page=' . $slug );
			}
		}
		return admin_url( 'options-general.php?page=php-compatibility-checker-by-volade' );
	}
}
