<?php
/**
 * Main Plugin Class
 *
 * Core plugin functionality and initialization.
 *
 * @package RedFoxWP\RedFoxTabManager
 * @since 1.0.0
 */

namespace RedFoxWP\RedFoxTabManager;

use RedFoxWP\RedFoxTabManager\Abstracts\Abstract_Singleton;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Main plugin class
 *
 * Handles plugin initialization, hooks, and core functionality.
 *
 * @since 1.0.0
 */
class Plugin extends Abstract_Singleton {
	/**
	 * Constructor
	 *
	 * @since 1.0.0
	 * @access protected
	 *
	 * @return void
	 */
	protected function __construct() {
		$this->includes();
		$this->hooks();
		$this->init();
	}

	/**
	 * Include required files.
	 *
	 * @since 1.0.0
	 * @access private
	 *
	 * @return void
	 */
	private function includes() {
		require_once REDFOX_TAB_MANAGER_PLUGIN_DIR . 'includes/class-asset-manager.php';
		require_once REDFOX_TAB_MANAGER_PLUGIN_DIR . 'includes/class-tabs-cpt.php';
		require_once REDFOX_TAB_MANAGER_PLUGIN_DIR . 'includes/class-admin-menu.php';

		require_once REDFOX_TAB_MANAGER_PLUGIN_DIR . 'includes/class-global-tabs.php';
		require_once REDFOX_TAB_MANAGER_PLUGIN_DIR . 'includes/class-woocommerce-integration.php';

		// Include API files.
		require_once REDFOX_TAB_MANAGER_PLUGIN_DIR . 'includes/api/class-available-tabs-api.php';
		require_once REDFOX_TAB_MANAGER_PLUGIN_DIR . 'includes/api/class-global-tabs-api.php';
		require_once REDFOX_TAB_MANAGER_PLUGIN_DIR . 'includes/api/class-api-registry.php';
	}

	/**
	 * Initialize WordPress hooks
	 *
	 * @since 1.0.0
	 * @access private
	 *
	 * @return void
	 */
	private function hooks() {
		// Declare HPOS compatibility early.
		add_action( 'before_woocommerce_init', array( $this, 'declare_hpos_compatibility' ) );
	}

	/**
	 * Initialize plugin components
	 *
	 * @since 1.0.0
	 * @access private
	 *
	 * @return void
	 */
	private function init() {
		// Initialize Asset Manager.
		Asset_Manager::get_instance();
		Tabs_CPT::get_instance();

		// Initialize REST API Registry.
		API\API_Registry::get_instance();

		// Initialize Admin components (only in admin).
		if ( is_admin() ) {
			Admin_Menu::get_instance();
		}

		// Initialize WooCommerce integration (if WooCommerce is active).
		if ( class_exists( 'WooCommerce' ) ) {
			WooCommerce_Integration::get_instance();
		}
	}

	/**
	 * Declare HPOS compatibility
	 *
	 * Declares that this plugin is compatible with WooCommerce High-Performance Order Storage (HPOS).
	 * This plugin only works with product tabs and doesn't interact with order data, so it's fully compatible.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function declare_hpos_compatibility() {
		if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', REDFOX_TAB_MANAGER_PLUGIN_FILE, true );
		}
	}
}
