<?php
/**
 * Loader
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/includes/common
 * @author     miniOrange
 */

declare(strict_types=1);

namespace MOMCS_Sync\Includes\Common;

use MOMCS_Sync\Includes\Common\Hooks\Product_Sync_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Cron_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Amazon_Auth_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Stepper_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Listing_Status_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Category_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Field_Mapping_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Product_Type_Schema_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Template_Mapping_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Unified_Mapping_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Dashboard_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Orders_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Sync_Queue_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Channels_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Shipment_Hooks;
use MOMCS_Sync\Includes\Common\Hooks\Deactivation_Feedback_Hooks;
use MOMCS_Sync\Includes\Common\Utils\Current_Action;
use MOMCS_Sync\Includes\Common\Utils\Utils;
use MOMCS_Sync\Includes\Common\Constants\Plugin_Constants as Constants;

/**
 * Loader Class
 *
 * Loads and initializes all controllers and hooks
 */
class Loader {
	/**
	 * Initialize the loader
	 *
	 * @return void
	 */
	public function init() {
		// Load controllers.
		$this->load_controllers();
		// Load hooks.
		$this->load_hooks();
	}

	/**
	 * Load controllers
	 *
	 * @return void
	 */
	private function load_controllers() {

		// Product settings handler for per-product configuration.
		require_once MOMCS_PLUGIN_DIR . 'includes' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'handlers' . DIRECTORY_SEPARATOR . 'class-product-settings-handler.php';
		$product_settings_handler = new \MOMCS_Sync\Includes\Common\Handlers\Product_Settings_Handler();
		$product_settings_handler->init();

		// User account handler for miniOrange login/registration.
		require_once MOMCS_PLUGIN_DIR . 'includes' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'handlers' . DIRECTORY_SEPARATOR . 'class-user-account-handler.php';
		$user_account_handler = new \MOMCS_Sync\Includes\Common\Handlers\User_Account_Handler();
		$user_account_handler->init();

		// Initialise Current_Action tracker so services can access running Action-Scheduler ID.
		Current_Action::init();
	}

	/**
	 * Load hooks
	 *
	 * @return void
	 */
	private function load_hooks() {
		new Hooks( MOMCS_PLUGIN_BASENAME );
		new Cron_Hooks();
		new Amazon_Auth_Hooks();
		new Stepper_Hooks();
		new Listing_Status_Hooks();
		new Product_Sync_Hooks();
		new Category_Hooks();
		new Field_Mapping_Hooks();
		new Product_Type_Schema_Hooks();
		new Template_Mapping_Hooks();
		new Unified_Mapping_Hooks();
		new Dashboard_Hooks();
		new Orders_Hooks();
		new Sync_Queue_Hooks();
		new Channels_Hooks();
		new Shipment_Hooks();

		if ( ! Utils::is_premium_plan() ) {
			new Deactivation_Feedback_Hooks();
		}

		// Load plan-specific hooks (premium vs free).
		$this->load_plan_hooks();
	}

	/**
	 * Load plan-specific hook registrars.
	 *
	 * Premium endpoints and logic are registered only when the current plan is premium.
	 * The current plan can be set via the MOMCS_PLAN constant or the 'momcs_current_plan' filter.
	 *
	 * @return void
	 */
	private function load_plan_hooks(): void {
		// License hooks: premium plan only (class may exist in unified package but should not run on free).
		if ( Utils::is_premium_plan() && class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\License_Hooks' ) ) {
			new \MOMCS_Sync\Includes\Plans\Premium\Hooks\License_Hooks();
		}

		if ( Constants::PLAN_NAME === 'premium' ) {
			// Template Mapping premium endpoints (multi-template CRUD).
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Template_Mapping_Premium_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Template_Mapping_Premium_Hooks();
			}

			// Premium plan limits.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Plan_Limits_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Plan_Limits_Hooks();
			}

			// Premium sync settings hooks (AJAX + filters for sync settings persistence).
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Sync_Settings_Premium_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Sync_Settings_Premium_Hooks();
			}

			// ASIN migration hooks for standard to premium plan migration.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Asin_Migration_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Asin_Migration_Hooks();
			}

			// ASIN sync hooks for bulk ASIN synchronization functionality.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Asin_Sync_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Asin_Sync_Hooks();
			}

			// Scheduled sync hooks for automated attribute synchronization.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Scheduled_Sync_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Scheduled_Sync_Hooks();
			}

			// Order scheduled sync hooks for automated order fetching.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Order_Scheduled_Sync_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Order_Scheduled_Sync_Hooks();
			}

			// Order fulfillment hooks for WooCommerce to Amazon auto-fulfillment.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Order_Fulfillment_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Order_Fulfillment_Hooks();
			}

			// Orders premium hooks for background order fetching with pagination.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Orders_Premium_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Orders_Premium_Hooks();
			}

			// Multi-channel sync hooks for individual marketplace sync operations.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Multi_Channel_Sync_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Multi_Channel_Sync_Hooks();
			}

			// Amazon Product Sync hooks for comprehensive product sync from Amazon.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Amazon_Product_Sync_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Amazon_Product_Sync_Hooks();
			}

			// Amazon to WooCommerce scheduled sync hooks for recurring full imports.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\Amazon_To_WC_Scheduled_Sync_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\Amazon_To_WC_Scheduled_Sync_Hooks();
			}

			// FBA stock scheduled sync hooks for recurring inventory updates.
			if ( class_exists( '\\MOMCS_Sync\\Includes\\Plans\\Premium\\Hooks\\FBA_Stock_Scheduled_Sync_Hooks' ) ) {
				new \MOMCS_Sync\Includes\Plans\Premium\Hooks\FBA_Stock_Scheduled_Sync_Hooks();
			}
		}
	}
}
