<?php
/**
 * The core plugin loader class.
 *
 * Registers all hooks and initializes plugin components.
 *
 * @package MasterQuiz
 * @since   1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class MasterQuiz_Loader
 *
 * Orchestrates plugin hook registration and component initialization.
 *
 * @since 1.0.0
 */
class MasterQuiz_Loader {

	/**
	 * The i18n instance.
	 *
	 * @var MasterQuiz_I18n
	 */
	private $i18n;

	/**
	 * The admin instance.
	 *
	 * @var MasterQuiz_Admin
	 */
	private $admin;

	/**
	 * The public instance.
	 *
	 * @var MasterQuiz_Public
	 */
	private $public;

	/**
	 * The settings instance.
	 *
	 * @var MasterQuiz_Settings
	 */
	private $settings;

	/**
	 * The AJAX handler instance.
	 *
	 * @var MasterQuiz_Ajax_Handler
	 */
	private $ajax_handler;

	/**
	 * The shortcode instance.
	 *
	 * @var MasterQuiz_Shortcode
	 */
	private $shortcode;

	/**
	 * The background process instance.
	 *
	 * @var MasterQuiz_Background_Process
	 */
	private $background_process;

	/**
	 * Initialize the plugin.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$this->i18n               = new MasterQuiz_I18n();
		$this->settings           = new MasterQuiz_Settings();
		$this->admin              = new MasterQuiz_Admin( $this->settings );
		$this->public             = new MasterQuiz_Public();
		$this->shortcode          = new MasterQuiz_Shortcode();
		$this->ajax_handler       = new MasterQuiz_Ajax_Handler();
		$this->background_process = new MasterQuiz_Background_Process();
	}

	/**
	 * Register all hooks and run the plugin.
	 *
	 * @since 1.0.0
	 */
	public function run() {
		// Internationalization.
		add_action( 'plugins_loaded', array( $this->i18n, 'load_textdomain' ) );

		// Admin hooks.
		add_action( 'admin_menu', array( $this->admin, 'add_menu_page' ) );
		add_action( 'admin_init', array( $this->settings, 'register_settings' ) );
		add_action( 'admin_enqueue_scripts', array( $this->admin, 'enqueue_assets' ) );

		// Plugin settings link on plugins page.
		add_filter(
			'plugin_action_links_' . MASTERQUIZ_PLUGIN_BASENAME,
			array( $this->admin, 'add_settings_link' )
		);

		// Public hooks.
		add_action( 'wp_enqueue_scripts', array( $this->public, 'register_assets' ) );

		// Shortcode.
		add_shortcode( 'masterquiz_ai', array( $this->shortcode, 'render' ) );

		// AJAX handlers (logged-in users).
		add_action( 'wp_ajax_masterquiz_upload', array( $this->ajax_handler, 'handle_upload' ) );
		add_action( 'wp_ajax_masterquiz_generate', array( $this->ajax_handler, 'handle_generate' ) );
		add_action( 'wp_ajax_masterquiz_job_status', array( $this->ajax_handler, 'handle_job_status' ) );
		add_action( 'wp_ajax_masterquiz_export', array( $this->ajax_handler, 'handle_export' ) );
		add_action( 'wp_ajax_masterquiz_create_learndash_quiz', array( $this->ajax_handler, 'handle_create_learndash_quiz' ) );
		add_action( 'wp_ajax_masterquiz_test_api_key', array( $this->ajax_handler, 'handle_test_api_key' ) );

		// AJAX handlers (logged-out users — for public shortcode use).
		add_action( 'wp_ajax_nopriv_masterquiz_upload', array( $this->ajax_handler, 'handle_upload' ) );
		add_action( 'wp_ajax_nopriv_masterquiz_generate', array( $this->ajax_handler, 'handle_generate' ) );
		add_action( 'wp_ajax_nopriv_masterquiz_job_status', array( $this->ajax_handler, 'handle_job_status' ) );
		add_action( 'wp_ajax_nopriv_masterquiz_export', array( $this->ajax_handler, 'handle_export' ) );
		add_action( 'wp_ajax_nopriv_masterquiz_create_learndash_quiz', array( $this->ajax_handler, 'handle_create_learndash_quiz' ) );

		// Background processing cron hook.
		add_action( 'masterquiz_process_job', array( $this->background_process, 'process_job' ) );

		// Upload cleanup cron hook.
		add_action( 'masterquiz_cleanup_uploads', array( MasterQuiz_Upload_Handler::class, 'cleanup_old_uploads' ) );
	}
}
