<?php
/**
 * Audit métadonnées wp.org + suggestions de mise à jour.
 *
 * @package PCC
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class PCC_Audit {

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

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

	/**
	 * @param string $target_php Version PHP cible.
	 * @return array{items:array<int,array>,summary:array}
	 */
	public function run( $target_php ) {
		if ( ! $this->plugin->has_feature( 'scan_all_plugins' ) ) {
			return array( 'items' => array(), 'summary' => array() );
		}

		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		if ( ! function_exists( 'plugins_api' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
		}

		$items   = array();
		$plugins = get_plugins();

		foreach ( $plugins as $file => $data ) {
			$slug = dirname( $file );
			if ( '.' === $slug ) {
				$slug = basename( $file, '.php' );
			}

			$local_version = $data['Version'] ?? '';
			$requires_php  = $data['RequiresPHP'] ?? '';
			$row           = array(
				'type'           => 'plugin',
				'slug'           => $slug,
				'name'           => $data['Name'] ?? $slug,
				'local_version'  => $local_version,
				'issues'         => array(),
				'update_available' => false,
				'latest_version' => '',
			);

			if ( $requires_php && version_compare( $requires_php, $target_php, '>' ) ) {
				$row['issues'][] = sprintf(
					/* translators: 1: required PHP, 2: target PHP */
					__( 'Declares Requires PHP %1$s (target is %2$s).', 'php-compatibility-checker-by-volade' ),
					$requires_php,
					$target_php
				);
			}

			$api = plugins_api(
				'plugin_information',
				array(
					'slug'   => $slug,
					'fields' => array(
						'last_updated' => true,
						'tested'       => true,
						'version'      => true,
						'downloadlink' => false,
					),
				)
			);

			if ( is_wp_error( $api ) || empty( $api->slug ) ) {
				if ( $this->plugin->has_feature( 'scan_premium_code' ) ) {
					$row['issues'][] = __( 'Not on WordPress.org — local code scan only (premium/custom).', 'php-compatibility-checker-by-volade' );
				}
				$items[] = $row;
				continue;
			}

			if ( ! empty( $api->version ) && version_compare( $local_version, $api->version, '<' ) ) {
				$row['update_available'] = true;
				$row['latest_version']   = $api->version;
				$row['issues'][]         = sprintf(
					/* translators: 1: latest version */
					__( 'Update available: %s — may fix PHP issues.', 'php-compatibility-checker-by-volade' ),
					$api->version
				);
			}

			if ( ! empty( $api->last_updated ) ) {
				$updated = strtotime( $api->last_updated );
				if ( $updated && $updated < strtotime( '-2 years' ) ) {
					$row['issues'][] = __( 'No WordPress.org update in 2+ years — may be abandoned.', 'php-compatibility-checker-by-volade' );
				}
			}

			if ( ! empty( $api->tested ) && version_compare( $api->tested, '6.0', '<' ) ) {
				$row['issues'][] = sprintf(
					/* translators: %s: tested up to version */
					__( 'Tested up to WordPress %s only on WordPress.org.', 'php-compatibility-checker-by-volade' ),
					$api->tested
				);
			}

			if ( ! empty( $row['issues'] ) || $row['update_available'] ) {
				$items[] = $row;
			}
		}

		$theme = wp_get_theme();
		if ( $theme->exists() ) {
			$stylesheet = $theme->get_stylesheet();
			$t_row      = array(
				'type'             => 'theme',
				'slug'             => $stylesheet,
				'name'             => $theme->get( 'Name' ),
				'local_version'    => $theme->get( 'Version' ),
				'issues'           => array(),
				'update_available' => false,
				'latest_version'   => '',
			);

			if ( ! $theme->get( 'ThemeURI' ) && ! $stylesheet ) {
				$t_row['issues'][] = __( 'Custom theme — verify PHP compatibility manually.', 'php-compatibility-checker-by-volade' );
			}

			if ( ! empty( $t_row['issues'] ) ) {
				$items[] = $t_row;
			}
		}

		return array(
			'items'   => $items,
			'summary' => array(
				'count'           => count( $items ),
				'updates'         => count(
					array_filter(
						$items,
						function ( $i ) {
							return ! empty( $i['update_available'] );
						}
					)
				),
				'abandoned_hints' => count(
					array_filter(
						$items,
						function ( $i ) {
							return ! empty( $i['issues'] );
						}
					)
				),
			),
		);
	}

	/**
	 * Plugins concurrents connus.
	 *
	 * @return array<int, array{slug:string,name:string}>
	 */
	public static function known_conflicts() {
		return array(
			array( 'slug' => 'php-compatibility-checker/php-compatibility-checker.php', 'name' => 'PHP Compatibility Checker (WP Engine)' ),
			array( 'slug' => 'brightleaf-digital-php-compatibility-scanner/brightleaf-digital-php-compatibility-scanner.php', 'name' => 'BrightLeaf PHP Compatibility Scanner' ),
			array( 'slug' => 'eli-php-compatibility-scanner/eli-php-compatibility-scanner.php', 'name' => "Eli's PHP Compatibility Scanner" ),
			array( 'slug' => 'upgrade-readiness-monitor/upgrade-readiness-monitor.php', 'name' => 'Upgrade Readiness Monitor' ),
		);
	}

	/**
	 * @return array<int, array{slug:string,name:string}>
	 */
	public static function detect_conflicts() {
		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		$found = array();
		foreach ( self::known_conflicts() as $conflict ) {
			if ( is_plugin_active( $conflict['slug'] ) ) {
				$found[] = $conflict;
			}
		}
		return $found;
	}
}
