<?php
/**
 * Bootstrap du plugin.
 *
 * @package CVS
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

final class CVS_Plugin {

	private static $instance = null;

	const CRON_HOOK = 'cvs_daily_maintenance';

	/** @var Volade_SDK|null */
	public $volade = null;

	/** @var CVS_Settings */
	public $settings;

	/** @var CVS_Core_Switch */
	public $core_switch;

	/** @var CVS_Admin|null */
	public $admin = null;

	/** @var CVS_Network_Admin|null */
	public $network_admin = null;

	/** @var CVS_Diagnostics */
	public $diagnostics;

	/** @var CVS_I18n */
	public $i18n;

	/** @var CVS_Site_Health */
	public $site_health;

	/** @var CVS_Dashboard */
	public $dashboard;

	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		$this->i18n        = new CVS_I18n();
		$this->settings    = new CVS_Settings( $this );
		$this->core_switch = new CVS_Core_Switch( $this );
		$this->diagnostics = new CVS_Diagnostics( $this );
		$this->site_health = new CVS_Site_Health( $this );
		$this->dashboard   = new CVS_Dashboard( $this );
		$this->admin       = new CVS_Admin( $this );

		if ( is_multisite() ) {
			$this->network_admin = new CVS_Network_Admin( $this );
		}

		add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 20 );
		add_action( 'admin_init', array( $this, 'maybe_redirect_after_activation' ) );
		add_action( self::CRON_HOOK, array( $this, 'run_daily_maintenance' ) );
	}

	public function run_daily_maintenance() {
		$this->settings->maybe_clear_expired_pin();
	}

	public function maybe_redirect_after_activation() {
		if ( ! get_option( 'cvs_activation_redirect' ) || ! current_user_can( 'update_core' ) ) {
			return;
		}

		if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			delete_option( 'cvs_activation_redirect' );
			return;
		}

		delete_option( 'cvs_activation_redirect' );
		wp_safe_redirect( admin_url( CVS_Admin::instance_settings_path( $this ) ) );
		exit;
	}

	public function on_plugins_loaded() {
		if ( class_exists( 'Volade_SDK' ) ) {
			$this->volade = Volade_SDK::get_instance();
			if ( $this->volade ) {
				$this->register_volade_features();
			}
		}

		$this->core_switch->init();
		$this->settings->maybe_clear_expired_pin();

		if ( ! wp_next_scheduled( self::CRON_HOOK ) ) {
			wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_HOOK );
		}
	}

	private function register_volade_features() {
		$this->volade->register_features(
			array(
				'custom_download_url' => array( 'level' => 'premium' ),
				'auto_clear_target'   => array( 'level' => 'premium' ),
				'switch_log'          => array( 'level' => 'premium' ),
				'export_import'       => array( 'level' => 'connected' ),
			),
			CVS_SLUG
		);
	}

	public function has_feature( $feature ) {
		if ( 'export_import' === $feature ) {
			return $this->settings->can_export_import();
		}
		if ( ! $this->volade ) {
			return false;
		}
		return $this->volade->has_access( $feature, 0, CVS_SLUG );
	}

	public function render_gate( $feature, $message = '' ) {
		if ( ! $this->volade ) {
			echo '<p class="cvs-guest-hint">' . esc_html__( 'Volade account required — install the SDK (see notice at the top of the admin).', 'core-version-switcher-by-volade' ) . '</p>';
			return;
		}
		$args = $message ? array( 'message' => $message ) : array();
		$this->volade->render_access_gate( $feature, $args, CVS_SLUG );
	}

	public static function activate() {
		CVS_Migration::maybe_migrate_from_wp_downgrade();

		if ( ! get_option( CVS_Settings::OPTION_KEY ) ) {
			update_option( CVS_Settings::OPTION_KEY, CVS_Settings::defaults() );
		}

		if ( is_multisite() && ! get_site_option( CVS_Settings::NETWORK_OPTION_KEY ) ) {
			update_site_option( CVS_Settings::NETWORK_OPTION_KEY, CVS_Settings::defaults() );
		}

		if ( class_exists( 'Volade_SDK' ) ) {
			$sdk = Volade_SDK::get_instance();
			if ( $sdk && method_exists( $sdk, 'run_activation_tasks' ) ) {
				$sdk->run_activation_tasks();
			}
		}

		if ( class_exists( 'Volade_SDK' ) && Volade_SDK::get_instance() && ! get_option( 'volade_api_url' ) ) {
			$default_api = defined( 'VOLADE_API_URL' ) ? VOLADE_API_URL : 'https://volade.com/api';
			update_option( 'volade_api_url', $default_api );
		}

		if ( ! wp_next_scheduled( self::CRON_HOOK ) ) {
			wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_HOOK );
		}

		if ( ! wp_doing_ajax() ) {
			update_option( 'cvs_activation_redirect', 1, false );
		}
	}

	public static function deactivate() {
		wp_clear_scheduled_hook( self::CRON_HOOK );
	}
}
