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

defined( 'ABSPATH' ) || exit;

final class PCC_Plugin {

	private static $instance = null;

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

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

	/** @var PCC_Scanner */
	public $scanner;

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

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

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

	/** @var PCC_Scheduler */
	public $scheduler;

	/** @var PCC_Runtime_Monitor */
	public $runtime;

	/** @var PCC_Health */
	public $health;

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

	private function __construct() {
		$this->i18n      = new PCC_I18n();
		$this->settings  = new PCC_Settings( $this );
		$this->scanner   = new PCC_Scanner( $this );
		$this->scheduler = new PCC_Scheduler( $this );
		$this->runtime   = new PCC_Runtime_Monitor( $this );
		$this->health    = new PCC_Health( $this );
		new PCC_Auto_Rescan( $this );
		$this->admin     = new PCC_Admin( $this );
		if ( is_multisite() ) {
			$this->network_admin = new PCC_Network_Admin( $this );
		}

		add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 20 );
		add_action( 'admin_init', array( $this, 'maybe_redirect_after_activation' ) );
	}

	public function maybe_redirect_after_activation() {
		if ( ! get_option( 'pcc_activation_redirect' ) || ! current_user_can( 'manage_options' ) ) {
			return;
		}

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

		delete_option( 'pcc_activation_redirect' );
		wp_safe_redirect( admin_url( PCC_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();
			}
		}
	}

	private function register_volade_features() {
		$this->volade->register_features(
			array(
				'scan_all_plugins'    => array( 'level' => 'connected' ),
				'php_target_full'     => array( 'level' => 'connected' ),
				'scan_premium_code'   => array( 'level' => 'connected' ),
				'false_positive_mark' => array( 'level' => 'connected' ),
				'scan_parent_theme'   => array( 'level' => 'connected' ),
				'scan_history'        => array( 'level' => 'premium', 'guest_limit' => 0, 'connected_limit' => 0, 'premium_limit' => 20 ),
				'export_csv'          => array( 'level' => 'premium' ),
				'wp_cli'              => array( 'level' => 'premium' ),
				'scheduled_scan'      => array( 'level' => 'premium' ),
			),
			PCC_SLUG
		);
	}

	public function has_feature( $feature ) {
		if ( ! $this->volade ) {
			return false;
		}
		return $this->volade->has_access( $feature, 0, PCC_SLUG );
	}

	public function feature_limit( $feature ) {
		if ( ! $this->volade ) {
			return 0;
		}
		return $this->volade->get_feature_limit( $feature, 0, PCC_SLUG );
	}

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

	public static function activate() {
		if ( ! get_option( PCC_Settings::OPTION_KEY ) ) {
			update_option( PCC_Settings::OPTION_KEY, PCC_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_doing_ajax() ) {
			update_option( 'pcc_activation_redirect', 1, false );
		}

		if ( class_exists( 'PCC_Scheduler' ) ) {
			$inst = self::instance();
			if ( $inst->scheduler ) {
				$inst->scheduler->reschedule();
			}
		}
	}

	public static function deactivate() {
		$timestamp = wp_next_scheduled( PCC_Scheduler::CRON_HOOK );
		if ( $timestamp ) {
			wp_unschedule_event( $timestamp, PCC_Scheduler::CRON_HOOK );
		}
	}
}
