<?php
/**
 * AJAX handler for settings functionality.
 *
 * @package SEO_Pro_Stack
 * @subpackage SEO_Pro_Stack/Admin/Settings/AJAX
 */

// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * AJAX handler for settings.
 */
class SEOProStack_AJAX_Settings {

    /**
     * Initialize the class
     */
    public function __construct() {
        add_action('wp_ajax_seoprostack_update_option', array($this, 'update_option'));
        add_action('wp_ajax_seoprostack_get_options', array($this, 'get_options'));
    }

    /**
     * Update a plugin option via AJAX.
     */
    public function update_option() {
        // Check nonce
        if (!check_ajax_referer('seoprostack-nonce', 'nonce', false)) {
            wp_send_json_error(array(
                'message' => 'Invalid security token sent.'
            ));
            return;
        }

        // Check permissions
        if (!current_user_can('manage_options')) {
            wp_send_json_error(array(
                'message' => 'You do not have permission to update settings.'
            ));
            return;
        }
        
        // Get option data
        $option_group = isset($_POST['option_group']) ? sanitize_text_field($_POST['option_group']) : '';
        $option_name = isset($_POST['option_name']) ? sanitize_text_field($_POST['option_name']) : '';
        $option_value = isset($_POST['option_value']) ? sanitize_text_field($_POST['option_value']) : '';
        
        if (empty($option_group) || empty($option_name)) {
            wp_send_json_error(array(
                'message' => 'Missing required parameters.'
            ));
            return;
        }
        
        // Get current option values
        $options = get_option($option_group, array());
        
        // Update value
        $options[$option_name] = $option_value;
        
        // Save updated options
        $result = update_option($option_group, $options);
        
        if ($result) {
            wp_send_json_success(array(
                'message' => 'Option updated successfully',
                'option_group' => $option_group,
                'option_name' => $option_name,
                'option_value' => $option_value
            ));
        } else {
            wp_send_json_error(array(
                'message' => 'Failed to update option or no changes were made.'
            ));
        }
    }
    
    /**
     * Get plugin options via AJAX.
     */
    public function get_options() {
        // Check nonce
        if (!check_ajax_referer('seoprostack-nonce', 'nonce', false)) {
            wp_send_json_error(array(
                'message' => 'Invalid security token sent.'
            ));
            return;
        }

        // Check permissions
        if (!current_user_can('manage_options')) {
            wp_send_json_error(array(
                'message' => 'You do not have permission to retrieve settings.'
            ));
            return;
        }
        
        // Get option group
        $option_group = isset($_POST['option_group']) ? sanitize_text_field($_POST['option_group']) : '';
        
        if (empty($option_group)) {
            wp_send_json_error(array(
                'message' => 'Missing required parameters.'
            ));
            return;
        }
        
        // Get options
        $options = get_option($option_group, array());
        
        wp_send_json_success(array(
            'options' => $options
        ));
    }
}