<?php
/**
 * WAF Integration - Load WAF Components
 *
 * @package VMPFence
 * @since 2.0.0
 * @since 2.2.4 - Added WAF Optimizer controller and AJAX handler
 */

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

// Load WAF models
require_once VMPFENCE_PLUGIN_DIR . 'firewall/models/class-waf-rule.php';
require_once VMPFENCE_PLUGIN_DIR . 'firewall/models/class-waf-log.php';
require_once VMPFENCE_PLUGIN_DIR . 'firewall/models/class-rule-importer.php';

// Load WAF controller
require_once VMPFENCE_PLUGIN_DIR . 'firewall/controllers/class-waf-controller.php';

// Load WAF Optimizer controller
if (file_exists(VMPFENCE_PLUGIN_DIR . 'firewall/controllers/class-waf-optimizer.php')) {
    require_once VMPFENCE_PLUGIN_DIR . 'firewall/controllers/class-waf-optimizer.php';
}

// Load WAF AJAX handler
require_once VMPFENCE_PLUGIN_DIR . 'firewall/ajax/class-waf-ajax.php';

// Load WAF Optimizer AJAX handler
if (is_admin() && file_exists(VMPFENCE_PLUGIN_DIR . 'firewall/ajax/waf-optimizer-ajax.php')) {
    require_once VMPFENCE_PLUGIN_DIR . 'firewall/ajax/waf-optimizer-ajax.php';
}

// Load Delete IP AJAX handler
if (is_admin() && file_exists(VMPFENCE_PLUGIN_DIR . 'admin/ajax/delete-ip-ajax.php')) {
    require_once VMPFENCE_PLUGIN_DIR . 'admin/ajax/delete-ip-ajax.php';
}

/**
 * Install default WAF rules on first admin page load if table is empty
 */
function vmpfence_waf_check_default_rules() {
    // Only run on admin pages
    if (!is_admin()) {
        return;
    }

    // Check if we already checked in this session
    if (get_transient('vmpfence_waf_rules_checked')) {
        return;
    }

    // Check if default rules exist
    if (!VMPFence_WAF_Rule::has_default_rules()) {
        // Install default rules
        VMPFence_WAF_Rule::install_default_rules();
    }

    // Set transient to prevent checking again for 1 hour
    set_transient('vmpfence_waf_rules_checked', true, HOUR_IN_SECONDS);
}
add_action('admin_init', 'vmpfence_waf_check_default_rules');

/**
 * Auto-retry rule import if count doesn't match expected
 */
function vmpfence_waf_auto_retry_import() {
    // Only run on admin pages
    if (!is_admin()) {
        return;
    }

    // Check if we already retried in this session
    if (get_transient('vmpfence_waf_retry_checked')) {
        return;
    }

    // Only proceed if rules exist (don't retry during initial installation)
    if (!VMPFence_WAF_Rule::has_default_rules()) {
        return;
    }

    // Get expected count from JSON
    $importer = new VMPFence_Rule_Importer();
    $expected_count = $importer->get_expected_count();

    if ($expected_count === null) {
        // JSON file not available or error reading it
        set_transient('vmpfence_waf_retry_checked', true, HOUR_IN_SECONDS);
        return;
    }

    // Verify rule count
    if (!$importer->verify_rules_count($expected_count)) {
        // Count mismatch, attempt single retry silently
        $importer->retry_import(1);
    }

    // Set transient to prevent checking again for 1 hour
    set_transient('vmpfence_waf_retry_checked', true, HOUR_IN_SECONDS);
}
add_action('admin_init', 'vmpfence_waf_auto_retry_import', 11);

