<?php

namespace MOWCPS\Common\Hooks;

use MOWCPS\Common\Constants\Plugin_Hooks;

/**
 * Init_Hooks Class
 *
 * Handles the registration and initialization of all WordPress hooks
 * for the WooCommerce Product Sync plugin. This class ensures hooks
 * are loaded in the correct sequence during WordPress initialization.
 *
 * @package MOWCPS\Common\Hooks
 * @since 1.0.0
 */
class Init_Hooks {

	/**
	 * Class default constructor.
	 *
	 * Registers the init_hooks method to run on WordPress 'init' action.
	 * Only adds the hook if it hasn't been added already.
	 */
	public function __construct() {
		// Check if the action is already hooked before adding it (// for backend testing.)
		if ( ! has_action( 'rest_api_init', array( $this, 'init_rest_api_hooks' ) ) ) {
			add_action( 'rest_api_init', array( $this, 'init_rest_api_hooks' ) );
		}
		// Check if the action is already hooked before adding it
		if ( ! has_action( 'init', array( $this, 'init_hooks' ) ) ) {
			add_action( 'init', array( $this, 'init_hooks' ) );
		}
	}

	/**
	 * Initialize all REST API hooks.
	 *
	 * @return void
	 */
	public function init_rest_api_hooks() {
		// new \MOWCPS\Common\REST_APIs\REST_API_Handler();
		// for backend testing.
		new \MOWCPS\Common\REST_APIs\Rest_APIs();
	}

	/**
	 * Initialize all plugin hooks in the correct sequence.
	 *
	 * This method registers all action and filter hooks in the proper order:
	 * 1. Cron schedules
	 * 2. File management hooks
	 * 3. Batch sync hooks
	 * 4. Backup sync hooks
	 * 5. Cleanup hooks
	 *
	 * @return void
	 */
	public function init_hooks() {
		$this->register_cron_schedules();
		$this->register_file_management_hooks();
		$this->register_batch_sync_hooks();
		$this->register_backup_sync_hooks();
		$this->register_cleanup_hooks();
	}

	/**
	 * Register custom cron schedules.
	 *
	 * Registers custom cron frequencies needed for backup and batch sync operations.
	 *
	 * @return void
	 */
	private function register_cron_schedules() {
		add_filter( 'cron_schedules', array( '\MOWCPS\Common\Services\Cron\Cron_Manager', 'custom_scheduling_frequency' ) );
	}

	/**
	 * Register file management hooks.
	 *
	 * Handles cleanup and file deletion operations.
	 *
	 * @return void
	 */
	private function register_file_management_hooks() {
		/**
		 * Hook for deleting test result files.
		 * Triggered when test operations are completed and cleanup is needed.
		 *
		 * @param mixed $file_data File data or identifier to be deleted
		 */
		add_action( 'MOWCPS_delete_file_event', array( '\MOWCPS\Common\Utils', 'delete_test_result_file' ), 10, 1 );
	}

	/**
	 * Register batch sync hooks.
	 *
	 * Handles batch processing of product sync operations.
	 *
	 * @return void
	 */
	private function register_batch_sync_hooks() {
		/**
		 * Hook for initiating batch sync operations.
		 * Processes products in batches to avoid memory and timeout issues.
		 *
		 * @param array $cron_arguments Arguments containing batch information and sync manager
		 */
		add_action( 'mowcps_product_batch_sync_cron', array( '\MOWCPS\Common\Controllers\Cron_Controller', 'start_batch_sync' ), 10, 1 );
	}

	/**
	 * Register backup sync hooks.
	 *
	 * Handles backup and recovery operations during sync.
	 *
	 * @return void
	 */
	private function register_backup_sync_hooks() {
		/**
		 * Hook for initiating backup sync operations.
		 * Creates backup points during sync operations for recovery purposes.
		 *
		 * @param array $cron_arguments Arguments containing sync manager and backup details
		 */
		add_action( 'mowcps_product_sync_backup_cron', array( '\MOWCPS\Common\Controllers\Cron_Controller', 'start_backup_sync' ), 10, 1 );
	}

	/**
	 * Register cleanup hooks.
	 *
	 * Handles cleanup operations after sync completion.
	 *
	 * @return void
	 */
	private function register_cleanup_hooks() {
		/**
		 * Hook for deleting product images by IDs.
		 * Cleanup operation to remove unused product images.
		 *
		 * @param array $product_ids Array of product IDs whose images should be deleted
		 */
		add_action( 'mowcps_delete_product_images_cron', array( '\MOWCPS\Common\Utils', 'delete_product_images_by_ids' ), 10, 1 );
		/**
		 * Hook for processing one chunk of bulk product deletion (cron-based).
		 * Runs every 1–2 seconds until the queue is empty.
		 */
		add_action( Plugin_Hooks::HOOK_BULK_DELETION_CRON_HOOK, array( '\MOWCPS\Common\Utils\Model_Utils', 'process_bulk_deletion_cron' ), 10, 1 );
	}
}
