<?php
/**
 * Settings API Integration
 * Handles passing WordPress settings to the Vue app
 */

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

/**
 * Add settings to the page for Vue app to use
 */
function smartpress_localize_settings() {
    // Only add settings on admin pages or frontend with shortcode
    if (!is_admin() && !has_shortcode(get_post()->post_content ?? '', 'smartpress_app')) {
        return;
    }
    
    // Get menu configuration
    $menu_items = smartpress_get_menu_config();
    
    // Get current settings
    $settings = array(
        'enableFrontend' => SMARTPRESS_ENABLE_FRONTEND,
        'enableBackend' => SMARTPRESS_ENABLE_BACKEND,
        'enableBackendGlobal' => SMARTPRESS_ENABLE_BACKEND_GLOBAL,
        'devMode' => SMARTPRESS_DEV_MODE,
        'debugMode' => SMARTPRESS_ENABLE_DEBUG,
        'menuSlug' => SMARTPRESS_MENU_SLUG,
        'menuSlugs' => smartpress_get_menu_slugs(),
        'baseSlug' => SMARTPRESS_MENU_SLUG,
        'menuItems' => $menu_items,  // Pass the full menu configuration to Vue
        'apiUrl' => rest_url('smartpress/v1'),
        'nonce' => wp_create_nonce('wp_rest')
    );
    
    // Register settings script
    wp_register_script('smartpress-settings', '', array(), SMARTPRESS_APP_VERSION, true);
    
    // Localize the script with settings data
    wp_localize_script('smartpress-settings', 'smartpress_vueWpSettings', $settings);
    
    // Add custom event trigger after settings are available
    $trigger_event = "
        if (typeof window.dispatchEvent === 'function') {
            window.dispatchEvent(new Event('smartpress_vueWpSettingsLoaded'));
        }
    ";
    wp_add_inline_script('smartpress-settings', $trigger_event, 'after');
    
    // Enqueue the script
    wp_enqueue_script('smartpress-settings');
}

// Add settings before the Vue app is loaded
add_action('admin_head', 'smartpress_localize_settings');
add_action('wp_head', 'smartpress_localize_settings');

/**
 * Register REST API endpoints for settings
 */
function smartpress_register_rest_routes() {
    register_rest_route('smartpress/v1', '/settings', array(
        'methods' => 'GET',
        'callback' => 'smartpress_get_settings',
        'permission_callback' => function() {
            return current_user_can('manage_options');
        }
    ));
    
    register_rest_route('smartpress/v1', '/settings', array(
        'methods' => 'POST',
        'callback' => 'smartpress_update_settings',
        'permission_callback' => function() {
            return current_user_can('manage_options');
        }
    ));
}
add_action('rest_api_init', 'smartpress_register_rest_routes');

/**
 * Get settings for REST API
 */
function smartpress_get_settings() {
    $settings = array(
        'enableFrontend' => SMARTPRESS_ENABLE_FRONTEND,
        'enableBackend' => SMARTPRESS_ENABLE_BACKEND,
        'enableBackendGlobal' => SMARTPRESS_ENABLE_BACKEND_GLOBAL,
        'devMode' => SMARTPRESS_DEV_MODE,
        'debugMode' => SMARTPRESS_ENABLE_DEBUG
    );
    
    return rest_ensure_response($settings);
}

/**
 * Update settings via REST API
 */
function smartpress_update_settings($request) {
    // This is a simplified example - in a real implementation,
    // you would update the .env file or a WordPress option
    
    $params = $request->get_params();
    
    // Validate and sanitize input
    $settings = array(
        'enableFrontend' => isset($params['enableFrontend']) ? (bool) $params['enableFrontend'] : SMARTPRESS_ENABLE_FRONTEND,
        'enableBackend' => isset($params['enableBackend']) ? (bool) $params['enableBackend'] : SMARTPRESS_ENABLE_BACKEND,
        'enableBackendGlobal' => isset($params['enableBackendGlobal']) ? (bool) $params['enableBackendGlobal'] : SMARTPRESS_ENABLE_BACKEND_GLOBAL,
        'devMode' => isset($params['devMode']) ? (bool) $params['devMode'] : SMARTPRESS_DEV_MODE,
        'debugMode' => isset($params['debugMode']) ? (bool) $params['debugMode'] : SMARTPRESS_ENABLE_DEBUG
    );
    
    // In a real implementation, you would save these settings
    // For now, we'll just return them
    
    return rest_ensure_response(array(
        'success' => true,
        'settings' => $settings
    ));
} 