<?php
/**
 * Main plugin bootstrap / service container.
 *
 * @package SmartCategories
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class SCWC_Plugin
 */
final class SCWC_Plugin {

	/**
	 * Singleton instance.
	 *
	 * @var SCWC_Plugin|null
	 */
	private static $instance = null;

	/**
	 * Whether the environment is satisfied (WooCommerce active).
	 *
	 * @var bool
	 */
	private $booted = false;

	/**
	 * Get the singleton instance.
	 *
	 * @return SCWC_Plugin
	 */
	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor: load dependencies and wire hooks.
	 */
	private function __construct() {
		$this->includes();

		if ( ! $this->is_woocommerce_active() ) {
			add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
			return;
		}

		$this->init();
		$this->booted = true;
	}

	/**
	 * Require class files.
	 */
	private function includes() {
		require_once SCWC_DIR . 'includes/class-scwc-install.php';
		require_once SCWC_DIR . 'includes/class-scwc-fields.php';
		require_once SCWC_DIR . 'includes/class-scwc-templates.php';
		require_once SCWC_DIR . 'includes/class-scwc-rule-engine.php';
		require_once SCWC_DIR . 'includes/class-scwc-term-rules.php';
		require_once SCWC_DIR . 'includes/class-scwc-assigner.php';
		require_once SCWC_DIR . 'includes/class-scwc-scheduler.php';
		require_once SCWC_DIR . 'includes/class-scwc-product-hooks.php';
		require_once SCWC_DIR . 'includes/class-scwc-frontend-sort.php';
		require_once SCWC_DIR . 'includes/class-scwc-rest-controller.php';
		require_once SCWC_DIR . 'includes/class-scwc-admin.php';
		require_once SCWC_DIR . 'includes/class-scwc-review.php';
		require_once SCWC_DIR . 'includes/class-scwc-block.php';
		// The Upgrade page is the one Pro pointer wp.org allows in free — it ships
		// in BOTH builds and hides itself for Pro users. NOT premium-only.
		require_once SCWC_DIR . 'includes/class-scwc-upgrade.php';
		require_once SCWC_DIR . 'includes/class-scwc-help.php';

		if ( defined( 'WP_CLI' ) && WP_CLI ) {
			require_once SCWC_DIR . 'includes/class-scwc-cli.php';
		}
	}

	/**
	 * Initialise components once WooCommerce is confirmed active.
	 */
	private function init() {
		// Ensure DB schema is current (runs in web + CLI contexts, gated by version).
		SCWC_Install::maybe_upgrade();

		// Background processing must register its hooks early (Action Scheduler callbacks).
		SCWC_Scheduler::init();
		SCWC_Product_Hooks::init();
		SCWC_Block::init();
		SCWC_Frontend_Sort::init();

		// REST API.
		add_action(
			'rest_api_init',
			static function () {
				( new SCWC_REST_Controller() )->register_routes();
			}
		);

		// Admin UI (term meta box + assets + menu), the review ask, and the
		// Upgrade page (the single Pro pointer allowed in the free build).
		if ( is_admin() ) {
			SCWC_Admin::init();
			SCWC_Review::init();
			SCWC_Upgrade::init();
			SCWC_Help::init();
		}


		// WP-CLI.
		if ( defined( 'WP_CLI' ) && WP_CLI && class_exists( 'SCWC_CLI' ) ) {
			WP_CLI::add_command( 'scwc', 'SCWC_CLI' );
		}
	}

	/**
	 * Whether Pro features are unlocked. Delegates to the main plugin file's
	 * scwc_is_pro() (Freemius-bound in the pro build); the free build carries no
	 * premium code so it always resolves false. Falls back to the raw filter when
	 * the helper is unavailable (e.g. the class loaded in isolation under tests).
	 *
	 * @return bool
	 */
	public static function is_pro() {
		if ( function_exists( 'scwc_is_pro' ) ) {
			return scwc_is_pro();
		}
		/**
		 * Filter the Pro flag. The pro build's scwc_is_pro() binds this to Freemius.
		 *
		 * @param bool $is_pro Default false.
		 */
		return (bool) apply_filters( 'scwc_is_pro', false );
	}

	/**
	 * Whether WooCommerce is active.
	 *
	 * @return bool
	 */
	public function is_woocommerce_active() {
		return class_exists( 'WooCommerce' );
	}

	/**
	 * Whether the plugin booted fully.
	 *
	 * @return bool
	 */
	public function is_booted() {
		return $this->booted;
	}

	/**
	 * Admin notice shown when WooCommerce is missing.
	 */
	public function woocommerce_missing_notice() {
		echo '<div class="notice notice-error"><p>';
		echo esc_html__( 'Smart Categories for WooCommerce requires the WooCommerce plugin to be installed and active.', 'smart-categories-for-woocommerce' );
		echo '</p></div>';
	}
}
