<?php
/**
 * Commandes WP-CLI.
 *
 * @package PCC
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'WP_CLI' ) ) {
	return;
}

class PCC_CLI {

	/**
	 * Affiche le dernier scan ou l'état en cours.
	 *
	 * ## EXAMPLES
	 *
	 *     wp pcc status
	 */
	public function status( $args, $assoc_args ) {
		unset( $args, $assoc_args );
		$plugin = PCC_Plugin::instance();
		$scan   = $plugin->settings->get_last_scan();
		$state  = $plugin->settings->get_scan_state();

		WP_CLI::log( 'PHP Compatibility Checker by Volade v' . PCC_VERSION );
		WP_CLI::log( 'Runtime PHP: ' . phpversion() );

		if ( ! empty( $state['status'] ) && 'running' === $state['status'] ) {
			$total = count( $state['queue'] ?? array() );
			$pos   = (int) ( $state['position'] ?? 0 );
			WP_CLI::log( 'Scan in progress: ' . $pos . '/' . $total );
			return;
		}

		if ( empty( $scan['completed_at'] ) ) {
			WP_CLI::warning( 'No completed scan yet.' );
			return;
		}

		WP_CLI::log( 'Target PHP: ' . ( $scan['target_php'] ?? '?' ) );
		WP_CLI::log( 'Verdict: ' . ( $scan['verdict'] ?? 'unknown' ) );
		WP_CLI::log( 'Errors: ' . (int) ( $scan['summary']['errors'] ?? 0 ) );
		WP_CLI::log( 'Warnings: ' . (int) ( $scan['summary']['warnings'] ?? 0 ) );
		WP_CLI::log( 'Completed: ' . ( $scan['completed_at'] ?? '' ) );
	}

	/**
	 * Lance un scan complet (V+).
	 *
	 * ## OPTIONS
	 *
	 * [--target=<version>]
	 * : PHP version to test against (8.0-8.5).
	 *
	 * [--format=<format>]
	 * : table or json
	 *
	 * [--fail-on=<level>]
	 * : Exit with code 1 on error or warning (error|warning).
	 *
	 * ## EXAMPLES
	 *
	 *     wp pcc scan --target=8.4 --fail-on=error
	 */
	public function scan( $args, $assoc_args ) {
		unset( $args );
		$plugin = PCC_Plugin::instance();

		if ( ! $plugin->has_feature( 'wp_cli' ) ) {
			WP_CLI::error( 'WP-CLI scan requires V+ Premium.' );
		}

		if ( ! empty( $assoc_args['target'] ) ) {
			$plugin->settings->save( array_merge( $plugin->settings->all(), array( 'target_php' => $assoc_args['target'] ) ) );
		}

		$scan = $plugin->scanner->run_full_scan_cli();
		if ( is_wp_error( $scan ) ) {
			WP_CLI::error( $scan->get_error_message() );
		}

		$format  = $assoc_args['format'] ?? 'table';
		$fail_on = $assoc_args['fail-on'] ?? '';

		if ( 'json' === $format ) {
			WP_CLI::log( wp_json_encode( $scan, JSON_PRETTY_PRINT ) );
			$this->maybe_exit_fail_on( $scan, $fail_on );
			return;
		}

		WP_CLI::success( 'Verdict: ' . ( $scan['verdict'] ?? 'unknown' ) );
		WP_CLI::log( 'Errors: ' . (int) ( $scan['summary']['errors'] ?? 0 ) );
		WP_CLI::log( 'Warnings: ' . (int) ( $scan['summary']['warnings'] ?? 0 ) );

		$this->maybe_exit_fail_on( $scan, $fail_on );
	}

	/**
	 * @param array  $scan
	 * @param string $fail_on
	 */
	private function maybe_exit_fail_on( $scan, $fail_on ) {
		$errors   = (int) ( $scan['summary']['errors'] ?? 0 );
		$warnings = (int) ( $scan['summary']['warnings'] ?? 0 );

		if ( 'error' === $fail_on && $errors > 0 ) {
			WP_CLI::error( 'Scan has blocking errors.' );
		}
		if ( 'warning' === $fail_on && ( $errors > 0 || $warnings > 0 ) ) {
			WP_CLI::error( 'Scan has errors or warnings.' );
		}
		if ( ! $fail_on && 'blocked' === ( $scan['verdict'] ?? '' ) ) {
			exit( 1 );
		}
	}

	/**
	 * Exporte le dernier rapport JSON.
	 *
	 * ## OPTIONS
	 *
	 * [--file=<path>]
	 *
	 * ## EXAMPLES
	 *
	 *     wp pcc export --file=pcc-report.json
	 */
	public function export( $args, $assoc_args ) {
		unset( $args );
		$json = PCC_Plugin::instance()->settings->export_scan_json();
		$file = $assoc_args['file'] ?? 'pcc-' . gmdate( 'Y-m-d' ) . '.json';

		if ( false === file_put_contents( $file, $json ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
			WP_CLI::error( 'Unable to write the file.' );
		}
		WP_CLI::success( 'Exported to ' . $file );
	}
}

WP_CLI::add_command( 'pcc', 'PCC_CLI' );
