<?php
/**
 * WP-CLI — wp cvs status | target | clear | rollback | update
 *
 * @package CVS
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
	return;
}

class CVS_CLI {

	/**
	 * Show current core version, target pin, and diagnostics summary.
	 */
	public function status( $args, $assoc_args ) {
		unset( $args, $assoc_args );
		$plugin = CVS_Plugin::instance();
		$target = CVS_Settings::sanitize_version( $plugin->settings->get( 'target_version' ) );

		WP_CLI::log( 'Installed: ' . get_bloginfo( 'version' ) );
		WP_CLI::log( 'Target:    ' . ( $target ?: '(none)' ) );
		WP_CLI::log( 'Latest:    ' . CVS_Releases::get_latest_version() );

		$rollback = get_option( CVS_Settings::PREVIOUS_VERSION_KEY, '' );
		if ( $rollback ) {
			WP_CLI::log( 'Rollback:  ' . $rollback );
		}

		$expires = $plugin->settings->pin_expires_at();
		if ( $expires ) {
			WP_CLI::log( 'Pin expires: ' . gmdate( 'Y-m-d H:i:s', $expires ) . ' UTC' );
		}

		$diag = ( new CVS_Diagnostics( $plugin ) )->run();
		foreach ( $diag['checks'] as $check ) {
			WP_CLI::log( sprintf( '[%s] %s — %s', strtoupper( $check['status'] ), $check['label'], $check['message'] ) );
		}

		if ( $target ) {
			$compat = CVS_Preflight::compat_report( $target );
			foreach ( $compat['warnings'] as $line ) {
				WP_CLI::warning( $line );
			}
			foreach ( $compat['errors'] as $line ) {
				WP_CLI::warning( $line );
			}
		}
	}

	/**
	 * Pin a target WordPress version.
	 *
	 * ## OPTIONS
	 *
	 * <version>
	 * : Semver like 6.4.3
	 */
	public function target( $args, $assoc_args ) {
		unset( $assoc_args );
		$version = CVS_Settings::sanitize_version( $args[0] ?? '' );
		if ( ! $version ) {
			WP_CLI::error( 'Invalid version.' );
		}

		$plugin = CVS_Plugin::instance();
		$valid  = $plugin->settings->validate_target( $version );
		if ( is_wp_error( $valid ) ) {
			WP_CLI::error( $valid->get_error_message() );
		}

		$plugin->settings->remember_previous_version();
		$plugin->settings->mark_pin_set();
		$settings                   = $plugin->settings->all();
		$settings['target_version'] = $version;
		$plugin->settings->save( $settings, 'site' );

		WP_CLI::success( 'Target set to ' . $version . '. Run: wp cvs update' );
	}

	/**
	 * Clear the pinned target version.
	 */
	public function clear( $args, $assoc_args ) {
		unset( $args, $assoc_args );
		CVS_Plugin::instance()->settings->clear_target( 'site' );
		WP_CLI::success( 'Target cleared.' );
	}

	/**
	 * Pin the last known good version (one-click rollback helper).
	 */
	public function rollback( $args, $assoc_args ) {
		unset( $args, $assoc_args );
		$version = CVS_Settings::sanitize_version( get_option( CVS_Settings::PREVIOUS_VERSION_KEY, '' ) );
		if ( ! $version ) {
			WP_CLI::error( 'No previous version stored.' );
		}
		WP_CLI::runcommand( 'cvs target ' . $version, array( 'launch' => false ) );
	}

	/**
	 * Run core update to the pinned target (native upgrader).
	 *
	 * ## OPTIONS
	 *
	 * [--force]
	 * : Force reinstall even if versions appear equal.
	 */
	public function update( $args, $assoc_args ) {
		unset( $args );
		$plugin = CVS_Plugin::instance();
		$target = CVS_Settings::sanitize_version( $plugin->settings->get( 'target_version' ) );
		if ( ! $target ) {
			WP_CLI::error( 'No target version set. Use: wp cvs target <version>' );
		}

		$force = ! empty( $assoc_args['force'] );
		$cmd   = 'core update --version=' . $target;
		if ( $force ) {
			$cmd .= ' --force';
		}

		WP_CLI::log( 'Running: wp ' . $cmd );
		WP_CLI::runcommand( $cmd, array( 'launch' => true ) );
	}
}

WP_CLI::add_command( 'cvs', 'CVS_CLI' );
