<?php

/**
 * Assets Registration
 */

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

/**
 * Register and enqueue admin assets
 */
function content_craft_ai_register_admin_assets($hook)
{
    // Only load on our plugin pages
    if (strpos($hook, 'content-craft-ai') === false) {
        return;
    }

    // Enqueue the minified and obfuscated React bundle
    wp_enqueue_script(
        'content-craft-ai-admin',
        CONTENT_CRAFT_AI_PLUGIN_URL . 'dist/admin.min.js',
        array(),
        CONTENT_CRAFT_AI_VERSION,
        true
    );

    // Add initial data to window object
    wp_localize_script(
        'content-craft-ai-admin',
        'contentCraftAIData',
        array(
            'externalApiUrl' => CONTENT_CRAFT_AI_EXTERNAL_API,
            'wpApiUrl' => rest_url('content-craft-ai/v1'),
            'nonce' => wp_create_nonce('wp_rest'),
            'siteUrl' => get_site_url(),
            'adminUrl' => admin_url(),
            'isDebug' => defined('WP_DEBUG') && WP_DEBUG
        )
    );
}

// Hook to admin_enqueue_scripts action
add_action('admin_enqueue_scripts', 'content_craft_ai_register_admin_assets');
