<?php
/**
 * Fonctions WordPress dépréciées par version cible (analyse statique locale).
 *
 * @package PCC
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class PCC_Wp_Deprecations {

	/**
	 * Liste conservative — fonctions WP retirées ou fortement dépréciées.
	 *
	 * @return array<string, array<string, string>>
	 */
	public static function by_version() {
		return array(
			'6.4' => array(
				'like_escape' => 'wpdb::esc_like()',
			),
			'6.5' => array(
				'set_screen_options' => 'per-user screen options API',
			),
			'6.6' => array(
				'block_core_latest_posts' => 'block variations',
			),
			'6.7' => array(),
			'6.8' => array(),
		);
	}

	/**
	 * @return string[]
	 */
	public static function available_targets() {
		global $wp_version;
		$current = self::normalize( $wp_version );
		$out     = array();
		foreach ( array_keys( self::by_version() ) as $ver ) {
			if ( version_compare( $ver, $current, '>' ) ) {
				$out[] = $ver;
			}
		}
		if ( empty( $out ) ) {
			$out[] = $current;
		}
		return $out;
	}

	public static function normalize( $version ) {
		$parts = explode( '.', (string) $version );
		return $parts[0] . '.' . ( $parts[1] ?? '0' );
	}

	/**
	 * @param string $target_wp Version WordPress cible (ex. 6.8).
	 * @return array<string, string>
	 */
	public static function functions_up_to( $target_wp ) {
		$target = self::normalize( $target_wp );
		$out    = array();
		foreach ( self::by_version() as $ver => $funcs ) {
			if ( version_compare( self::normalize( $ver ), $target, '<=' ) ) {
				$out = array_merge( $out, $funcs );
			}
		}
		return $out;
	}

	/**
	 * @param string $file
	 * @param string $target_wp
	 * @return array<int, array{severity:string,file:string,line:int,message:string,code:string}>
	 */
	public static function scan_file( $file, $target_wp ) {
		if ( ! $target_wp || ! is_readable( $file ) ) {
			return array();
		}

		$deprecated = self::functions_up_to( $target_wp );
		if ( empty( $deprecated ) ) {
			return array();
		}

		$code = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
		if ( false === $code || '' === $code ) {
			return array();
		}

		$findings = array();
		foreach ( array_keys( $deprecated ) as $func ) {
			if ( ! preg_match_all( '/\b' . preg_quote( $func, '/' ) . '\s*\(/i', $code, $m, PREG_OFFSET_CAPTURE ) ) {
				continue;
			}
			$replacement = $deprecated[ $func ];
			foreach ( $m[0] as $match ) {
				$findings[] = array(
					'severity' => 'warning',
					'file'     => $file,
					'line'     => substr_count( substr( $code, 0, (int) $match[1] ), "\n" ) + 1,
					'message'  => sprintf(
						/* translators: 1: function name, 2: WP version, 3: replacement hint */
						__( 'WordPress function %1$s is deprecated since %2$s — use %3$s instead.', 'php-compatibility-checker-by-volade' ),
						$func,
						$target_wp,
						$replacement
					),
					'code'     => 'pcc.wp_deprecated.' . $func,
				);
			}
		}

		return $findings;
	}
}
