<?php
/**
 * Status Modal AJAX Handler
 * 
 * Handles AJAX requests for status calculations
 * 
 * @since 2.1.0
 * @package VMPFence
 * @subpackage StatusModal
 */

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

/**
 * Handle status calculation AJAX request
 */
function vmpfence_status_modal_ajax() {
    // Verify nonce
    check_ajax_referer('vmpfence_status_modal_nonce', 'nonce');
    
    // Check user permissions
    if (!current_user_can('manage_options')) {
        wp_send_json_error('Unauthorized');
        return;
    }
    
    // Get status type from request
    $status_type = isset($_POST['status_type']) ? sanitize_text_field($_POST['status_type']) : '';
    
    if (empty($status_type)) {
        wp_send_json_error('Missing status type');
        return;
    }
    
    // Load the status calculator
    require_once VMPFENCE_PLUGIN_DIR . 'status-modal/controllers/class-status-calculator.php';
    
    // Get status data
    $data = VMPFence_Status_Calculator::get_status_data($status_type);
    
    if ($data === null) {
        wp_send_json_error('Invalid status type');
        return;
    }
    
    // Return data
    wp_send_json_success($data);
}

// Register AJAX handlers
add_action('wp_ajax_vmpfence_get_status', 'vmpfence_status_modal_ajax');
