<?php
/**
 * Script and Style Enqueuing
 * Handles loading of Vue.js assets in WordPress
 */

// Prevent direct access to this file
if (!defined('ABSPATH')) {
    exit;
}

// Add at the top of the file
require_once plugin_dir_path(__FILE__) . 'menu-config.php';
require_once plugin_dir_path(__FILE__) . 'custom-fields.php';




// Enqueue scripts and styles for both admin and frontend
function smartpress_enqueue_scripts() {
    // Only verify nonce for our plugin's specific AJAX requests
    if (isset($_POST['_wpnonce']) && isset($_POST['action']) && strpos($_POST['action'], 'smartpress_') === 0) {
        $nonce = sanitize_text_field(wp_unslash($_POST['_wpnonce']));
        if (!wp_verify_nonce($nonce, 'wp_rest')) {
            wp_die(
                esc_html__('Security check failed. Please try again.', 'smartpress-pro'),
                esc_html__('Security Error', 'smartpress-pro'),
                array('response' => 403, 'back_link' => true)
            );
        }
    }

    // If backend is not globally enabled
    if(!SMARTPRESS_ENABLE_BACKEND_GLOBAL) {
        // Get all menu slugs
        $menu_slugs = smartpress_get_menu_slugs();
        
        // Check if the current page is one of our admin pages
        $is_plugin_page = is_admin() && 
                         isset($_GET['page']) && 
                         in_array($_GET['page'], $menu_slugs);
        
        // If not admin or not one of our pages, return
        if(!$is_plugin_page) {
            return;
        }
    }
    if (SMARTPRESS_DEV_MODE) {
        // Development mode - use webpack dev server
        wp_register_script(
            'smartpress_app_js',
            'http://localhost:8080/js/app.js',
            array(),
            SMARTPRESS_APP_VERSION,
            true
        );
        
        wp_enqueue_script('smartpress_app_js');

        // Add CORS headers for development
        header('Access-Control-Allow-Origin: *');
        
        // Add debug message to help verify script loading
        // error_log('Vue app script enqueued in dev mode');
    } else {
        // Production mode - use built files
        wp_register_script(
            'smartpress_app_js',
            SMARTPRESS_URL . 'dist/js/app.js',
            array(),
            SMARTPRESS_APP_VERSION,
            true
        );
        
        wp_enqueue_script('smartpress_app_js');

        // Register and enqueue CSS if needed
        // wp_register_style(
        //     'smartpress_app_css',
        //     SMARTPRESS_URL . 'dist/css/app.css',
        //     array(),
        //     SMARTPRESS_APP_VERSION
        // );
        // wp_enqueue_style('smartpress_app_css');
    }

        // Add this to make sure the nonce is available in the Vue app
        wp_localize_script('smartpress_app_js', 'smartpress_wpApiSettings', array(
            'root' => esc_url_raw(rest_url()),
            'nonce' => wp_create_nonce('wp_rest'),
            'pluginUrl' => plugins_url('..', __FILE__)
        ));
        
        // Add SmartPress specific variables
        wp_localize_script('smartpress_app_js', 'smartpress_vars', array(
            'nonce' => wp_create_nonce('wp_rest'),
            'ajaxurl' => admin_url('admin-ajax.php'),
            'pluginUrl' => plugins_url('..', __FILE__)
        ));
}

// Hook for frontend
if (SMARTPRESS_ENABLE_FRONTEND) {
add_action('wp_enqueue_scripts', 'smartpress_enqueue_scripts');
}
if (SMARTPRESS_ENABLE_BACKEND) {
// Hook for admin
add_action('admin_enqueue_scripts', 'smartpress_enqueue_scripts'); 

// Add current user data to be available for Vue
function smartpress_add_current_user_data() {
    // Get current user
    $current_user = wp_get_current_user();
    
    // Create an array with the user data you want to expose
    $user_data = array(
        'ID' => $current_user->ID,
        'user_login' => $current_user->user_login,
        'user_email' => $current_user->user_email,
        'display_name' => $current_user->display_name,
        'roles' => $current_user->roles,
        'is_logged_in' => is_user_logged_in(),
        'site_url' => get_site_url() // Add WordPress site URL
    );
    
    // Localize the script with user data
    wp_localize_script(
        'smartpress_app_js', // This should match the handle used when you registered your script
        'smartpress_wpUserData',
        $user_data
    );
}

// Hook this function to the same action where you enqueue your scripts
add_action('admin_enqueue_scripts', 'smartpress_add_current_user_data');

// Enqueue scripts in Elementor editor
// if elementor is active && customFields.elementor_enabled is true
if (defined('ELEMENTOR_VERSION') && smartpress_get_custom_field('elementor_enabled', true)) {
    add_action('elementor/editor/after_enqueue_scripts', 'smartpress_enqueue_scripts');
    add_action( 'elementor/editor/after_enqueue_styles', function(){
        wp_enqueue_style('smartpress_admin_css', plugins_url('../css/admin-style.css', __FILE__), array(), SMARTPRESS_APP_VERSION);
    } );
}

}
