<?php
/**
 * Brute Force Protection Integration - Load Brute Force Components
 *
 * @package VMPFence
 * @since 2.0.0
 */

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

// Load Brute Force model
require_once VMPFENCE_PLUGIN_DIR . 'firewall/models/class-brute-force.php';

// Load Brute Force controller
require_once VMPFENCE_PLUGIN_DIR . 'firewall/controllers/class-brute-force-controller.php';

// Load Brute Force AJAX handler
require_once VMPFENCE_PLUGIN_DIR . 'firewall/ajax/class-brute-force-ajax.php';

// Load admin AJAX handler for firewall options
require_once VMPFENCE_PLUGIN_DIR . 'admin/ajax/firewall-options-ajax.php';

// Load Strong Password Validator
require_once VMPFENCE_PLUGIN_DIR . 'firewall/models/class-strong-password-validator.php';

// Load Password Enforcement Controller
require_once VMPFENCE_PLUGIN_DIR . 'firewall/controllers/class-password-enforcement-controller.php';

// Load Brute Force Additional Option Controller
require_once VMPFENCE_PLUGIN_DIR . 'firewall/controllers/class-brute-force-additional-option-controller.php';


/**
 * Initialize brute force protection early (before init)
 */
function vmpfence_init_brute_force_protection() {
    // Only initialize if brute force protection is enabled
    $brute_force_enabled = VMPFence_Config::get('brute_force_enabled', 1);
    
    if ($brute_force_enabled) {
        // Initialize the brute force controller
        new VMPFence_Brute_Force_Controller();
    }
    
    // Initialize password enforcement if enabled (separate from brute force)
    $password_enforcement_enabled = VMPFence_Config::get('enforce_strong_passwords', 1);
    
    if ($password_enforcement_enabled) {
        // Initialize password enforcement controller
        new VMPFence_Password_Enforcement_Controller();
    }
    
    // Initialize additional brute force options controller
    new VMPFence_Brute_Force_Additional_Option_Controller();
}
add_action('plugins_loaded', 'vmpfence_init_brute_force_protection', 1);

/**
 * Initialize brute force AJAX handlers
 */
function vmpfence_init_brute_force_ajax() {
    // Initialize AJAX handlers (these need to be available even if protection is disabled for admin management)
    new VMPFence_Brute_Force_Ajax();
}
add_action('wp_loaded', 'vmpfence_init_brute_force_ajax');

/**
 * Setup brute force protection - ensure required classes are loaded
 */
function vmpfence_setup_brute_force_system() {
    // Check if we already verified setup in this session
    if (get_transient('vmpfence_brute_force_setup_checked')) {
        return;
    }

    // Verify that required classes are available
    if (!class_exists('VMPFence_Config')) {
        return;
    }

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