<?php
/**
 * Handles the enqueueing of admin scripts and styles for the plugin.
 *
 * @package EONSR_AEO_Agent
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Enqueue Admin Scripts and Styles.
 *
 * Checks the current hook to ensure assets are only loaded on the plugin page.
 * Localizes the JavaScript file with necessary AJAX URLs and Security Nonces.
 *
 * @param string $hook The current admin page hook.
 * @return void
 */
function eonsr_aeo_agent_enqueue_admin_scripts($hook)
{
    // 1. Validate Admin Page
    // Only load assets on the 'toplevel_page_eonsr_aeo_agent' to prevent conflicts.
    if ($hook !== 'toplevel_page_eonsr_aeo_agent') {
        return;
    }

    // Define constants for cleaner code and maintenance
    $version   = '1.0.0';
    $base_url  = plugin_dir_url(__FILE__) . '../assets';

    // 2. Enqueue Styles (CSS)
    wp_enqueue_style(
        'eonsr-aeo-agent-admin',
        $base_url . '/css/admin-style.css',
        [],
        $version
    );

    // 3. Enqueue Scripts (JS)
    wp_enqueue_script(
        'eonsr-aeo-agent-admin', 
        $base_url . '/js/admin-script.js',
        ['jquery'],
        $version,
        true 
    );

    // 4. Wizard Setup Data (only when needed)
    $license_status  = get_option('eonsr_aeo_agent_license_status', 'unsubmitted');
    $setup_completed = get_option('eonsr_aeo_agent_setup_completed', false);

    if ( !$setup_completed && $license_status === 'activated' ) {
        $features      = get_option('eonsr_aeo_agent_license_features', []);
        $f_langs_limit = 24;
        $f_style_limit = 4;

        $all_languages = [
            'Vietnamese' => ['name' => 'Tiếng Việt', 'flag' => '🇻🇳'],
            'English'    => ['name' => 'English', 'flag' => '🇺🇸'],
            'French'     => ['name' => 'Français', 'flag' => '🇫🇷'],
            'German'     => ['name' => 'Deutsch', 'flag' => '🇩🇪'],
            'Japanese'   => ['name' => '日本語', 'flag' => '🇯🇵'],
            'Korean'     => ['name' => '한국어', 'flag' => '🇰🇷'],
            'Chinese'    => ['name' => '中文', 'flag' => '🇨🇳'],
            'Russian'    => ['name' => 'Русский', 'flag' => '🇷🇺'],
            'Spanish'    => ['name' => 'Español', 'flag' => '🇪🇸'],
            'Italian'    => ['name' => 'Italiano', 'flag' => '🇮🇹'],
            'Thai'       => ['name' => 'ไทย', 'flag' => '🇹🇭'],
            'Indonesian' => ['name' => 'Bahasa Indonesia', 'flag' => '🇮🇩'],
            'Portuguese' => ['name' => 'Português', 'flag' => '🇵🇹'],
            'Hindi'      => ['name' => 'हिन्दी', 'flag' => '🇮🇳'],
            'Arabic'     => ['name' => 'العربية', 'flag' => '🇸🇦'],
            'Turkish'    => ['name' => 'Türkçe', 'flag' => '🇹🇷'],
            'Dutch'      => ['name' => 'Nederlands', 'flag' => '🇳🇱'],
            'Swedish'    => ['name' => 'Svenska', 'flag' => '🇸🇪'],
            'Norwegian'  => ['name' => 'Norsk', 'flag' => '🇳🇴'],
            'Danish'     => ['name' => 'Dansk', 'flag' => '🇩🇰'],
            'Finnish'    => ['name' => 'Suomi', 'flag' => '🇫🇮'],
            'Polish'     => ['name' => 'Polski', 'flag' => '🇵🇱'],
            'Ukrainian'  => ['name' => 'Українська', 'flag' => '🇺🇦'],
            'Hebrew'     => ['name' => 'עברית', 'flag' => '🇮🇱'],
        ];

        $all_countries = [
            'VN' => ['name' => 'Vietnam', 'flag' => '🇻🇳'],
            'US' => ['name' => 'United States', 'flag' => '🇺🇸'],
            'GB' => ['name' => 'United Kingdom', 'flag' => '🇬🇧'],
            'FR' => ['name' => 'France', 'flag' => '🇫🇷'],
            'DE' => ['name' => 'Germany', 'flag' => '🇩🇪'],
            'JP' => ['name' => 'Japan', 'flag' => '🇯🇵'],
            'KR' => ['name' => 'South Korea', 'flag' => '🇰🇷'],
            'CN' => ['name' => 'China', 'flag' => '🇨🇳'],
            'RU' => ['name' => 'Russia', 'flag' => '🇷🇺'],
            'ES' => ['name' => 'Spain', 'flag' => '🇪🇸'],
            'IT' => ['name' => 'Italy', 'flag' => '🇮🇹'],
            'CA' => ['name' => 'Canada', 'flag' => '🇨🇦'],
            'AU' => ['name' => 'Australia', 'flag' => '🇦🇺'],
            'IN' => ['name' => 'India', 'flag' => '🇮🇳'],
            'BR' => ['name' => 'Brazil', 'flag' => '🇧🇷'],
            'TH' => ['name' => 'Thailand', 'flag' => '🇹🇭'],
            'ID' => ['name' => 'Indonesia', 'flag' => '🇮🇩'],
            'SG' => ['name' => 'Singapore', 'flag' => '🇸🇬'],
            'MY' => ['name' => 'Malaysia', 'flag' => '🇲🇾'],
            'PH' => ['name' => 'Philippines', 'flag' => '🇵🇭'],
            'TR' => ['name' => 'Turkey', 'flag' => '🇹🇷'],
            'SA' => ['name' => 'Saudi Arabia', 'flag' => '🇸🇦'],
            'AE' => ['name' => 'UAE', 'flag' => '🇦🇪'],
            'ZA' => ['name' => 'South Africa', 'flag' => '🇿🇦'],
            'NG' => ['name' => 'Nigeria', 'flag' => '🇳🇬'],
            'EG' => ['name' => 'Egypt', 'flag' => '🇪🇬'],
            'AR' => ['name' => 'Argentina', 'flag' => '🇦🇷'],
            'MX' => ['name' => 'Mexico', 'flag' => '🇲🇽'],
            'NL' => ['name' => 'Netherlands', 'flag' => '🇳🇱'],
            'SE' => ['name' => 'Sweden', 'flag' => '🇸🇪'],
            'NO' => ['name' => 'Norway', 'flag' => '🇳🇴'],
            'DK' => ['name' => 'Denmark', 'flag' => '🇩🇰'],
            'FI' => ['name' => 'Finland', 'flag' => '🇫🇮'],
            'PL' => ['name' => 'Poland', 'flag' => '🇵🇱'],
            'UA' => ['name' => 'Ukraine', 'flag' => '🇺🇦'],
            'PT' => ['name' => 'Portugal', 'flag' => '🇵🇹'],
            'IL' => ['name' => 'Israel', 'flag' => '🇮🇱'],
        ];

        $all_styles = [
            'Mac dinh'       => ['name' => 'EONSR Style (Chuẩn SEO)', 'url' => 'https://eonsr.com/vi/cach-tan-dung-kien-thuc-aws-certified-cloud-practitioner-cho-chuyen-doi-so-va-toi-uu-chi-phi/', 'desc' => 'Phong cách cân bằng, chuyên nghiệp, tối ưu cho SEO.'],
            'Sleek Blog'     => ['name' => 'Sleek Blog (Hiện đại)', 'url' => '#', 'desc' => 'Văn phong trẻ trung, hiện đại, thích hợp cho blog cá nhân.'],
            'Urban Polished' => ['name' => 'Urban Polished (Chuyên nghiệp)', 'url' => '#', 'desc' => 'Trang trọng, gãy gọn, phù hợp cho doanh nghiệp.'],
            'Universe'       => ['name' => 'Universe (Sáng tạo)', 'url' => '#', 'desc' => 'Bay bổng, dùng nhiều từ ngữ hình tượng.'],
        ];

        $wizard_data_js = 'var eonsr_aeo_wizard_data = ' . wp_json_encode([
            'allLanguages' => $all_languages,
            'allCountries' => $all_countries,
            'allStyles'    => $all_styles,
            'langLimit'    => (int) $f_langs_limit,
            'countryLimit' => 5,
            'styleLimit'   => (int) $f_style_limit,
            'setupNonce'   => wp_create_nonce('eonsr_aeo_agent_setup_nonce'),
        ]) . ';';

        wp_add_inline_script('eonsr-aeo-agent-admin', $wizard_data_js, 'before');
    }

    // 5. Localize Script
    wp_localize_script('eonsr-aeo-agent-admin', 'eonsr_aeo_agent_ajax', [
        'ajax_url' => admin_url('admin-ajax.php'),

        // Content Generation
        'generate_text_nonce'     => wp_create_nonce('eonsr_aeo_agent_generate_ajax'),
        'generate_images_nonce'   => wp_create_nonce('eonsr_aeo_agent_generate_images_ajax'),

        // Workflow
        'poll_nonce'              => wp_create_nonce('eonsr_aeo_agent_poll_job_ajax'),
        'translate_nonce'         => wp_create_nonce('eonsr_aeo_agent_polylang_translate_ajax'),
        'auto_translate_nonce'    => wp_create_nonce('eonsr_aeo_agent_auto_translate'),

        // Data Update
        'update_content_nonce'    => wp_create_nonce('eonsr_aeo_agent_update_content_ajax'),
        'update_categories_nonce' => wp_create_nonce('eonsr_aeo_agent_update_categories_ajax'),

        // Bulk Processor
        'bulk_upload_nonce'       => wp_create_nonce('eonsr_aeo_agent_bulk_upload_csv'),
        'bulk_status_nonce'       => wp_create_nonce('eonsr_aeo_agent_bulk_status_ajax'),
        
        // Setup & Check
        'check_active_nonce'      => wp_create_nonce('eonsr_aeo_agent_check_active_nonce'),
        'setup_nonce'             => wp_create_nonce('eonsr_aeo_agent_setup_nonce'),
    ]);
}

add_action('admin_enqueue_scripts', 'eonsr_aeo_agent_enqueue_admin_scripts');