<?php

/**
 * Core plugin orchestrator class.
 *
 * Initializes all plugin components and registers hooks.
 *
 * @package MediaTasker
 */
// Prevent direct file access.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Class Mediat_Media_Tasker
 *
 * Singleton that boots up all plugin subsystems.
 */
class Mediat_Media_Tasker {
    /**
     * Singleton instance.
     *
     * @var Mediat_Media_Tasker|null
     */
    private static $instance = null;

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

    /**
     * Constructor — registers all hooks.
     */
    private function __construct() {
        $this->load_dependencies();
        $this->init_hooks();
    }

    /**
     * Load required class files.
     *
     * @return void
     */
    private function load_dependencies() {
        require_once MEDIAT_PLUGIN_DIR . 'includes/class-mediat_post-types.php';
        require_once MEDIAT_PLUGIN_DIR . 'includes/class-mediat_admin.php';
        require_once MEDIAT_PLUGIN_DIR . 'includes/class-mediat_settings.php';
        require_once MEDIAT_PLUGIN_DIR . 'includes/class-mediat_shortcode.php';
        require_once MEDIAT_PLUGIN_DIR . 'includes/class-mediat_ajax.php';
        require_once MEDIAT_PLUGIN_DIR . 'includes/class-mediat_upload.php';
    }

    /**
     * Register WordPress hooks.
     *
     * @return void
     */
    private function init_hooks() {
        // Register CPT, taxonomy, and statuses.
        Mediat_Post_Types::init();
        // Admin menus, pages, and admin-only assets.
        if ( is_admin() ) {
            Mediat_Admin::init();
            Mediat_Settings::init();
        }
        // Frontend shortcode and AJAX handlers (must run outside is_admin).
        Mediat_Shortcode::init();
        Mediat_Ajax::init();
        Mediat_Upload::init();
        add_action( 'wp_enqueue_scripts', array($this, 'enqueue_frontend_assets') );
    }

    /**
     * Enqueue frontend stylesheets and scripts.
     *
     * @return void
     */
    public function enqueue_frontend_assets() {
        wp_enqueue_style(
            'mediat_frontend-style',
            MEDIAT_PLUGIN_URL . 'assets/css/style.css',
            array(),
            MEDIAT_VERSION
        );
        wp_enqueue_style(
            'mediat_date-header-style',
            MEDIAT_PLUGIN_URL . 'assets/css/mediat_date-header.css',
            array('mediat_frontend-style'),
            MEDIAT_VERSION
        );
        wp_enqueue_style(
            'mediat_floating-menu-style',
            MEDIAT_PLUGIN_URL . 'assets/css/mediat_floating-menu.css',
            array('mediat_frontend-style'),
            MEDIAT_VERSION
        );
        wp_enqueue_style(
            'mediat_action-bar-style',
            MEDIAT_PLUGIN_URL . 'assets/css/mediat_action-bar.css',
            array('mediat_frontend-style'),
            MEDIAT_VERSION
        );
        wp_enqueue_style(
            'mediat_dialog-style',
            MEDIAT_PLUGIN_URL . 'assets/css/mediat_dialog.css',
            array('mediat_frontend-style'),
            MEDIAT_VERSION
        );
        wp_enqueue_style(
            'mediat_task-modal-style',
            MEDIAT_PLUGIN_URL . 'assets/css/mediat_task-modal.css',
            array('mediat_frontend-style', 'mediat_dialog-style'),
            MEDIAT_VERSION
        );
        wp_enqueue_script(
            'mediat_dialog-script',
            MEDIAT_PLUGIN_URL . 'assets/js/mediat_dialog.js',
            array('jquery'),
            MEDIAT_VERSION,
            true
        );
        wp_enqueue_script(
            'mediat_frontend-script',
            MEDIAT_PLUGIN_URL . 'assets/js/frontend.js',
            array('jquery', 'mediat_dialog-script'),
            MEDIAT_VERSION,
            true
        );
        wp_enqueue_script(
            'mediat_upload-script',
            MEDIAT_PLUGIN_URL . 'assets/js/mediat_upload.js',
            array('jquery', 'mediat_frontend-script'),
            MEDIAT_VERSION,
            true
        );
        wp_enqueue_script(
            'mediat_task-modal-script',
            MEDIAT_PLUGIN_URL . 'assets/js/mediat_task-modal.js',
            array('jquery', 'mediat_dialog-script', 'mediat_frontend-script'),
            MEDIAT_VERSION,
            true
        );
        wp_enqueue_script(
            'mediat_floating-menu-script',
            MEDIAT_PLUGIN_URL . 'assets/js/mediat_floating-menu.js',
            array('jquery', 'mediat_frontend-script'),
            MEDIAT_VERSION,
            true
        );
        wp_localize_script( 'mediat_frontend-script', 'mediat_ajax_obj', array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'nonce'    => wp_create_nonce( 'mediat_frontend_nonce' ),
        ) );
    }

}
