<?php
/**
 * Firewall Setup Admin Page
 *
 * @package BotFend\Admin\Pages
 */

namespace BotFend\Admin\Pages;

use BotFend\Core\Config;
use BotFend\Core\Core;
use BotFend\Core\FirewallSetup;

defined( 'ABSPATH' ) || exit;

class FirewallPage {

    private $config;
    private $firewall_setup;

    /**
     * Whether this is a Pro installation.
     *
     * @since 4.9.0
     * @var bool
     */
    private $is_pro = false;

    public function __construct( Core $core ) {
        $this->config = $core->get_config();
        $this->firewall_setup = new FirewallSetup( $this->config, $core->get_utilities() );

        // Check if Pro feature is available
        $this->is_pro = $this->check_pro_feature();

        $this->register_hooks();
    }

    /**
     * Check if Pro license is active.
     *
     * @since 4.9.0
     *
     * @return bool
     */
    private function check_pro_feature(): bool {
        // Check if license class exists
        if ( ! class_exists( 'BotFend\\License\\LicenseKey' ) ) {
            // If license class doesn't exist, treat as free version
            return false;
        }

        $license = \BotFend\License\LicenseKey::get_instance();
        // Full Protection can be a Pro feature
        // You can change this to any feature check you want
        return $license->has_feature( 'firewall' ) || $license->has_feature( 'full_protection' );
    }

    private function register_hooks(): void {
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );

        // Only register AJAX handlers if Pro (or for status display even if not Pro)
        add_action( 'wp_ajax_botfend_firewall_status', [ $this, 'ajax_get_status' ] );
        
        // These actions require Pro license
        if ( $this->is_pro ) {
            add_action( 'wp_ajax_botfend_firewall_configure', [ $this, 'ajax_configure' ] );
            add_action( 'wp_ajax_botfend_firewall_disable', [ $this, 'ajax_disable' ] );
            add_action( 'wp_ajax_botfend_firewall_basic_enable', [ $this, 'ajax_basic_enable' ] );
            add_action( 'wp_ajax_botfend_firewall_basic_disable', [ $this, 'ajax_basic_disable' ] );
        }
    }

    public function get_firewall_status(): array {
        return $this->firewall_setup->get_status();
    }

    public function enqueue_scripts( string $hook ): void {
        if ( strpos( $hook, 'botfend-firewall' ) === false ) {
            return;
        }

        wp_enqueue_style(
            'botfend-firewall-wizard',
            BOTFEND_ASSETS_URL . 'css/firewall-wizard.css',
            [],
            BOTFEND_VERSION
        );

        wp_enqueue_script(
            'botfend-firewall-wizard',
            BOTFEND_ASSETS_URL . 'js/firewall-wizard.js',
            [ 'jquery' ],
            BOTFEND_VERSION,
            true
        );

        wp_localize_script( 'botfend-firewall-wizard', 'botfendFirewall', [
            'ajax_url'        => admin_url( 'admin-ajax.php' ),
            'nonce'           => wp_create_nonce( 'botfend_firewall_nonce' ),
            'is_pro'          => $this->is_pro,
            'refresh_interval'=> 5000,
            'strings'         => [
                'enabling'           => esc_html__( 'Enabling Full Protection...', 'botfend-anti-bot-firewall' ),
                'disabling'          => esc_html__( 'Disabling Full Protection...', 'botfend-anti-bot-firewall' ),
                'basic_enabling'     => esc_html__( 'Enabling Basic Protection...', 'botfend-anti-bot-firewall' ),
                'basic_disabling'    => esc_html__( 'Disabling Basic Protection...', 'botfend-anti-bot-firewall' ),
                'success'            => esc_html__( 'Success!', 'botfend-anti-bot-firewall' ),
                'error'              => esc_html__( 'Error', 'botfend-anti-bot-firewall' ),
                'warning'            => esc_html__( 'Warning', 'botfend-anti-bot-firewall' ),
                'confirm_disable'    => esc_html__( 'Are you sure you want to disable Full Protection? Your site will fall back to Basic Protection mode.', 'botfend-anti-bot-firewall' ),
                'confirm_disable_delete' => esc_html__( 'Are you sure you want to disable Full Protection AND delete all firewall files? This cannot be undone.', 'botfend-anti-bot-firewall' ),
                'confirm_enable'     => esc_html__( 'Enable Full Protection? This will modify your server configuration (.user.ini or .htaccess).', 'botfend-anti-bot-firewall' ),
                'confirm_basic_disable' => esc_html__( 'Disable Basic Protection? Your site will have no early firewall protection.', 'botfend-anti-bot-firewall' ),
                'refreshing'         => esc_html__( 'Refreshing status...', 'botfend-anti-bot-firewall' ),
                'unknown_error'      => esc_html__( 'An unknown error occurred. Please try again.', 'botfend-anti-bot-firewall' ),
                'permission_denied'  => esc_html__( 'Permission denied. You do not have sufficient capabilities.', 'botfend-anti-bot-firewall' ),
                'copy_instruction'   => esc_html__( 'Copy to clipboard', 'botfend-anti-bot-firewall' ),
                'copied'             => esc_html__( 'Copied!', 'botfend-anti-bot-firewall' ),
                'pro_feature_title'  => esc_html__( 'Full Firewall Protection - Pro Feature', 'botfend-anti-bot-firewall' ),
                'pro_feature_desc'   => esc_html__( 'Enable Full Firewall Protection to block attacks before WordPress loads, providing maximum security against bots and attackers.', 'botfend-anti-bot-firewall' ),
                'upgrade_to_pro'     => esc_html__( 'Upgrade to Pro', 'botfend-anti-bot-firewall' ),
                'learn_more'         => esc_html__( 'Learn More', 'botfend-anti-bot-firewall' ),
                'pro_only'           => esc_html__( 'Pro Only Feature', 'botfend-anti-bot-firewall' ),
            ],
        ] );
    }

    public function render(): void {
        $status = $this->firewall_setup->get_status();
        
        // If not Pro, show upgrade notice
        if ( ! $this->is_pro ) {
            $this->render_pro_upgrade_notice();
            return;
        }
        ?>
        <div class="wrap botfend-firewall-wrap">
            <h1>
                <span class="dashicons dashicons-shield-alt" style="vertical-align: middle;"></span>
                <?php esc_html_e( 'Firewall Protection Setup', 'botfend-anti-bot-firewall' ); ?>
            </h1>

            <p class="description">
                <?php esc_html_e( 'Configure the early firewall to block malicious requests before WordPress loads. This provides maximum protection against bots and attackers.', 'botfend-anti-bot-firewall' ); ?>
            </p>

            <div class="botfend-firewall-container">
                <div class="botfend-card">
                    <h2><?php esc_html_e( 'Protection Status', 'botfend-anti-bot-firewall' ); ?></h2>

                    <div id="botfend-firewall-status">
                        <?php $this->render_status_section( $status ); ?>
                    </div>

                    <div id="botfend-firewall-actions" class="botfend-firewall-actions">
                        <?php $this->render_action_buttons( $status ); ?>
                    </div>

                    <div id="botfend-firewall-message" class="notice botfend-firewall-message" style="display:none;"></div>
                </div>

                <div class="botfend-card">
                    <h2><?php esc_html_e( 'Server Environment', 'botfend-anti-bot-firewall' ); ?></h2>
                    <?php $this->render_environment_info( $status['environment'] ); ?>
                </div>

                <div class="botfend-card">
                    <h2><?php esc_html_e( 'Manual Configuration', 'botfend-anti-bot-firewall' ); ?></h2>
                    <?php $this->render_manual_instructions( $status ); ?>
                </div>

                <div class="botfend-card botfend-card-full">
                    <h2><?php esc_html_e( 'Protection Modes Comparison', 'botfend-anti-bot-firewall' ); ?></h2>
                    <?php $this->render_mode_comparison(); ?>
                </div>

                <div class="botfend-card botfend-card-full">
                    <h2><?php esc_html_e( 'Troubleshooting', 'botfend-anti-bot-firewall' ); ?></h2>
                    <?php $this->render_troubleshooting(); ?>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Render Pro upgrade notice.
     *
     * @since 4.9.0
     *
     * @return void
     */
    private function render_pro_upgrade_notice(): void {
        ?>
        <div class="wrap botfend-firewall-wrap">
            <h1>
                <span class="dashicons dashicons-shield-alt" style="vertical-align: middle;"></span>
                <?php esc_html_e( 'Firewall Protection Setup', 'botfend-anti-bot-firewall' ); ?>
            </h1>

            <div class="notice notice-info botfend-pro-notice" style="margin: 20px 0; padding: 0;">
                <div class="botfend-pro-container" style="display: flex; flex-wrap: wrap; padding: 20px;">
                    <div class="botfend-pro-icon" style="flex: 0 0 80px; text-align: center; font-size: 48px;">
                        🛡️
                    </div>
                    <div class="botfend-pro-content" style="flex: 1; min-width: 250px;">
                        <h2><?php esc_html_e( 'Full Firewall Protection - Pro Feature', 'botfend-anti-bot-firewall' ); ?></h2>
                        <p><?php esc_html_e( 'Enable Full Firewall Protection to block attacks before WordPress loads, providing maximum security against bots and attackers.', 'botfend-anti-bot-firewall' ); ?></p>
                        <ul style="margin: 15px 0; padding-left: 20px;">
                            <li>✓ <?php esc_html_e( 'Blocks attacks before WordPress loads', 'botfend-anti-bot-firewall' ); ?></li>
                            <li>✓ <?php esc_html_e( 'Rate limiting at server level', 'botfend-anti-bot-firewall' ); ?></li>
                            <li>✓ <?php esc_html_e( 'Protects against DDoS attacks', 'botfend-anti-bot-firewall' ); ?></li>
                            <li>✓ <?php esc_html_e( '.user.ini and .htaccess support', 'botfend-anti-bot-firewall' ); ?></li>
                            <li>✓ <?php esc_html_e( 'Manual configuration options for Nginx', 'botfend-anti-bot-firewall' ); ?></li>
                        </ul>
                        <div class="botfend-pro-buttons" style="display: flex; gap: 15px; flex-wrap: wrap;">
                            <a href="<?php echo esc_url( admin_url( 'admin.php?page=botfend-license' ) ); ?>" class="button button-primary">
                                <?php esc_html_e( 'Activate License', 'botfend-anti-bot-firewall' ); ?>
                            </a>
                            <a href="https://your-site.com/botfend-pro" target="_blank" class="button button-secondary">
                                <?php esc_html_e( 'Purchase Pro License →', 'botfend-anti-bot-firewall' ); ?>
                            </a>
                        </div>
                    </div>
                    <div class="botfend-pro-badge" style="flex: 0 0 120px; text-align: center;">
                        <span style="background: #f05a28; color: #fff; padding: 5px 12px; border-radius: 20px; font-size: 12px; font-weight: bold;">
                            <?php echo esc_html__( 'PRO ONLY', 'botfend-anti-bot-firewall' ); ?>
                        </span>
                    </div>
                </div>
            </div>

            <div class="botfend-card" style="margin-top: 20px; text-align: center; padding: 40px;">
                <p style="font-size: 16px; margin-bottom: 20px;">
                    <?php esc_html_e( 'Full Firewall Protection is a Pro feature. Upgrade to protect your site at the server level.', 'botfend-anti-bot-firewall' ); ?>
                </p>
                <a href="https://your-site.com/botfend-pro" target="_blank" class="button button-primary button-hero">
                    <?php esc_html_e( 'Get BotFend Pro', 'botfend-anti-bot-firewall' ); ?>
                </a>
            </div>
            
            <!-- Show Basic Protection info even for free users -->
            <div class="botfend-card" style="margin-top: 20px;">
                <h2><?php esc_html_e( 'Basic Protection (Free)', 'botfend-anti-bot-firewall' ); ?></h2>
                <p><?php esc_html_e( 'Basic Protection is available in the free version and provides security after WordPress loads.', 'botfend-anti-bot-firewall' ); ?></p>
                <button type="button" class="button button-secondary" id="botfend-enable-basic" <?php echo $this->is_pro ? '' : 'disabled'; ?>>
                    <span class="dashicons dashicons-shield-alt"></span>
                    <?php esc_html_e( 'Enable Basic Protection', 'botfend-anti-bot-firewall' ); ?>
                </button>
                <?php if ( ! $this->is_pro ) : ?>
                    <p class="description" style="margin-top: 10px;">
                        <?php echo esc_html__( 'Upgrade to Pro for Full Protection that loads before WordPress!', 'botfend-anti-bot-firewall' ); ?>
                    </p>
                <?php endif; ?>
            </div>
        </div>
        <?php
    }

    private function render_status_section( array $status ): void {
        $mode_labels = [
            'full'  => [ 'label' => esc_html__( 'Full Protection', 'botfend-anti-bot-firewall' ), 'color' => 'green' ],
            'basic' => [ 'label' => esc_html__( 'Basic Protection', 'botfend-anti-bot-firewall' ), 'color' => 'orange' ],
            'none'  => [ 'label' => esc_html__( 'No Protection', 'botfend-anti-bot-firewall' ), 'color' => 'red' ],
        ];

        $current = $mode_labels[ $status['mode'] ] ?? $mode_labels['none'];
        ?>
        <div class="botfend-status-badge botfend-status-<?php echo esc_attr( $current['color'] ); ?>">
            <span class="dashicons dashicons-shield"></span>
            <?php echo esc_html( $current['label'] ); ?>
        </div>

        <?php if ( ! empty( $status['pending'] ) ) : ?>
            <div class="notice notice-info inline" style="margin:10px 0; padding:8px 12px; border-left-color: #72aee6;">
                <p style="margin:0;">
                    <span class="dashicons dashicons-clock" style="vertical-align: middle;"></span>
                    <?php esc_html_e( 'Firewall configuration written. Waiting for server to apply changes (may take up to 5 minutes).', 'botfend-anti-bot-firewall' ); ?>
                </p>
            </div>
        <?php endif; ?>

        <table class="botfend-status-table">
            <tr>
                <th><?php esc_html_e( 'Bootstrap File:', 'botfend-anti-bot-firewall' ); ?></th>
                <td>
                    <?php if ( $status['bootstrap_exists'] ): ?>
                        <span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span>
                        <?php esc_html_e( 'Present', 'botfend-anti-bot-firewall' ); ?>
                    <?php else: ?>
                        <span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span>
                        <?php esc_html_e( 'Missing', 'botfend-anti-bot-firewall' ); ?>
                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'auto_prepend_file:', 'botfend-anti-bot-firewall' ); ?></th>
                <td>
                    <?php if ( $status['active'] ): ?>
                        <span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span>
                        <?php esc_html_e( 'Configured', 'botfend-anti-bot-firewall' ); ?>
                    <?php else: ?>
                        <span class="dashicons dashicons-no-alt" style="color:#46b450;"></span>
                        <?php esc_html_e( 'Not Set', 'botfend-anti-bot-firewall' ); ?>
                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Firewall Directory:', 'botfend-anti-bot-firewall' ); ?></th>
                <td>
                    <?php if ( $status['firewall_dir_exists'] ): ?>
                        <span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span>
                        <?php esc_html_e( 'Exists', 'botfend-anti-bot-firewall' ); ?>
                    <?php else: ?>
                        <span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span>
                        <?php esc_html_e( 'Missing', 'botfend-anti-bot-firewall' ); ?>
                    <?php endif; ?>
                    <br><code><?php echo esc_html( $status['environment']['firewall_dir'] ); ?></code>
                </td>
            </tr>
            <?php if ( $status['has_conflict'] ): ?>
            <tr>
                <th><?php esc_html_e( 'Existing Prepend:', 'botfend-anti-bot-firewall' ); ?></th>
                <td>
                    <span class="dashicons dashicons-warning" style="color:#f56e28;"></span>
                    <?php esc_html_e( 'Another file is already prepended.', 'botfend-anti-bot-firewall' ); ?>
                    <br>
                    <code><?php echo esc_html( $status['environment']['current_prepend'] ); ?></code>
                    <p class="description">
                        <?php esc_html_e( 'BotFend will include this file to avoid conflicts.', 'botfend-anti-bot-firewall' ); ?>
                    </p>
                </td>
            </tr>
            <?php endif; ?>
            <tr>
                <th><?php esc_html_e( 'Recommended Method:', 'botfend-anti-bot-firewall' ); ?></th>
                <td>
                    <strong><?php echo esc_html( ucfirst( $status['recommended_method'] ) ); ?></strong>
                    <p class="description">
                        <?php
                        switch ( $status['recommended_method'] ) {
                            case 'user_ini':
                                esc_html_e( 'Your server supports .user.ini files for per-directory PHP configuration.', 'botfend-anti-bot-firewall' );
                                break;
                            case 'htaccess':
                                esc_html_e( 'Your server supports .htaccess files with PHP value directives.', 'botfend-anti-bot-firewall' );
                                break;
                            default:
                                esc_html_e( 'Manual configuration is required for your server environment.', 'botfend-anti-bot-firewall' );
                        }
                        ?>
                    </p>
                </td>
            </tr>
        </table>
        <?php
    }

    private function render_action_buttons( array $status ): void {
        $active = $status['active'];
        $mode = $status['mode'];
        $recommended = $status['recommended_method'];

        if ( $active ) {
            ?>
            <button type="button" class="button button-secondary" id="botfend-disable-firewall">
                <span class="dashicons dashicons-shield-off"></span>
                <?php esc_html_e( 'Disable Full Protection', 'botfend-anti-bot-firewall' ); ?>
            </button>
            <label class="botfend-delete-option">
                <input type="checkbox" id="botfend-delete-files-checkbox" value="1">
                <?php esc_html_e( 'Also delete firewall files', 'botfend-anti-bot-firewall' ); ?>
            </label>
            <p class="description" style="width: 100%;">
                <?php esc_html_e( 'Disabling will fall back to Basic Protection mode. Check the box to permanently remove all firewall files from the server.', 'botfend-anti-bot-firewall' ); ?>
            </p>
            <?php
        } elseif ( $mode === 'basic' ) {
            ?>
            <button type="button" class="button button-primary" id="botfend-enable-firewall" data-method="<?php echo esc_attr( $recommended ); ?>">
                <span class="dashicons dashicons-shield"></span>
                <?php esc_html_e( 'Upgrade to Full Protection', 'botfend-anti-bot-firewall' ); ?>
            </button>
            <button type="button" class="button button-secondary" id="botfend-disable-basic">
                <span class="dashicons dashicons-shield-off"></span>
                <?php esc_html_e( 'Disable Basic Protection', 'botfend-anti-bot-firewall' ); ?>
            </button>
            <p class="description" style="width: 100%;">
                <?php esc_html_e( 'Full protection loads the firewall before WordPress for maximum security. Recommended for production sites.', 'botfend-anti-bot-firewall' ); ?>
            </p>
            <?php
        } else {
            ?>
            <button type="button" class="button button-primary" id="botfend-enable-firewall" data-method="<?php echo esc_attr( $recommended ); ?>">
                <span class="dashicons dashicons-shield"></span>
                <?php esc_html_e( 'Enable Full Protection', 'botfend-anti-bot-firewall' ); ?>
            </button>
            <button type="button" class="button button-secondary" id="botfend-enable-basic">
                <span class="dashicons dashicons-shield-alt"></span>
                <?php esc_html_e( 'Enable Basic Protection', 'botfend-anti-bot-firewall' ); ?>
            </button>
            <p class="description" style="width: 100%;">
                <?php esc_html_e( 'Choose Full Protection for maximum security (recommended), or Basic if your host restricts auto_prepend_file.', 'botfend-anti-bot-firewall' ); ?>
            </p>
            <?php
        }
    }

    private function render_environment_info( array $env ): void {
        ?>
        <table class="botfend-env-table">
            <tr><th><?php esc_html_e( 'PHP SAPI:', 'botfend-anti-bot-firewall' ); ?></th><td><code><?php echo esc_html( $env['sapi'] ); ?></code></td></tr>
            
<tr><th><?php esc_html_e( 'Server Software:', 'botfend-anti-bot-firewall' ); ?></th><td><?php echo esc_html( $env['server_software'] ); ?></td>
            </tr>
            <tr><th><?php esc_html_e( 'PHP Version:', 'botfend-anti-bot-firewall' ); ?></th><td><?php echo esc_html( $env['php_version'] ); ?></td>
            </tr>
            <tr>
                <th><?php esc_html_e( '.user.ini Support:', 'botfend-anti-bot-firewall' ); ?></th>
                <td>
                    <?php if ( $env['can_use_user_ini'] ): ?>
                        <span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span>
                        <?php esc_html_e( 'Available', 'botfend-anti-bot-firewall' ); ?>
                    <?php else: ?>
                        <span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span>
                        <?php esc_html_e( 'Not Available', 'botfend-anti-bot-firewall' ); ?>
                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( '.htaccess Support:', 'botfend-anti-bot-firewall' ); ?></th>
                <td>
                    <?php if ( $env['can_use_htaccess'] ): ?>
                        <span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span>
                        <?php esc_html_e( 'Available', 'botfend-anti-bot-firewall' ); ?>
                    <?php else: ?>
                        <span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span>
                        <?php esc_html_e( 'Not Available', 'botfend-anti-bot-firewall' ); ?>
                    <?php endif; ?>
                </td>
            </tr>
            <tr><th><?php esc_html_e( 'Current auto_prepend_file:', 'botfend-anti-bot-firewall' ); ?></th><td><code><?php echo esc_html( $env['current_prepend'] ?: esc_html__( 'None', 'botfend-anti-bot-firewall' ) ); ?></code></td>
            </tr>
            <tr><th><?php esc_html_e( 'Firewall Directory:', 'botfend-anti-bot-firewall' ); ?></th><td><code><?php echo esc_html( $env['firewall_dir'] ); ?></code></td>
            </tr>
            <tr><th><?php esc_html_e( 'WordPress ABSPATH:', 'botfend-anti-bot-firewall' ); ?></th><td><code><?php echo esc_html( $env['abspath'] ); ?></code></td>
            </tr>
        </table>
        <?php
    }

    private function render_manual_instructions( array $status ): void {
        echo $this->firewall_setup->get_manual_instructions( $status['recommended_method'] );
        ?>
        <details>
            <summary><?php esc_html_e( 'Alternative Configuration Methods', 'botfend-anti-bot-firewall' ); ?></summary>
            <div style="margin-top:15px;">
                <h4><?php esc_html_e( 'php.ini Method', 'botfend-anti-bot-firewall' ); ?></h4>
                <?php echo $this->firewall_setup->get_manual_instructions( 'php_ini' ); ?>
                <h4 style="margin-top:20px;"><?php esc_html_e( 'Nginx Configuration', 'botfend-anti-bot-firewall' ); ?></h4>
                <p><?php esc_html_e( 'For Nginx servers, add the following to your server block or php.ini location block:', 'botfend-anti-bot-firewall' ); ?></p>
                <pre>fastcgi_param PHP_VALUE "auto_prepend_file=<?php echo esc_html( $status['environment']['firewall_dir'] . '/botfend-firewall.php' ); ?>";</pre>
            </div>
        </details>
        <?php
    }

    private function render_mode_comparison(): void {
        ?>
        <table class="botfend-mode-table">
            <thead>
                <tr>
                    <th><?php esc_html_e( 'Feature', 'botfend-anti-bot-firewall' ); ?></th>
                    <th><?php esc_html_e( 'Full Protection', 'botfend-anti-bot-firewall' ); ?></th>
                    <th><?php esc_html_e( 'Basic Protection', 'botfend-anti-bot-firewall' ); ?></th>
                    <th><?php esc_html_e( 'No Protection', 'botfend-anti-bot-firewall' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><?php esc_html_e( 'Loads before WordPress', 'botfend-anti-bot-firewall' ); ?></td>
                    <td><span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span></td>
                    <td><span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span></td>
                    <td><span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span></td>
                </tr>
                <tr>
                    <td><?php esc_html_e( 'Blocks attacks before plugins', 'botfend-anti-bot-firewall' ); ?></td>
                    <td><span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span></td>
                    <td><span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span></td>
                    <td><span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span></td>
                </tr>
                <tr>
                    <td><?php esc_html_e( 'Rate limiting at server level', 'botfend-anti-bot-firewall' ); ?></td>
                    <td><span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span></td>
                    <td><span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span></td>
                    <td><span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span></td>
                </tr>
                <tr>
                    <td><?php esc_html_e( 'Requires server configuration', 'botfend-anti-bot-firewall' ); ?></td>
                    <td><span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span></td>
                    <td><span class="dashicons dashicons-no-alt" style="color:#46b450;"></span></td>
                    <td><span class="dashicons dashicons-no-alt" style="color:#46b450;"></span></td>
                </tr>
                <tr>
                    <td><?php esc_html_e( 'Works on all hosts', 'botfend-anti-bot-firewall' ); ?></td>
                    <td><span class="dashicons dashicons-no-alt" style="color:#46b450;"></span></td>
                    <td><span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span></td>
                    <td><span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span></td>
                </tr>
                <tr>
                    <td><?php esc_html_e( 'Protects against DDoS', 'botfend-anti-bot-firewall' ); ?></td>
                    <td><span class="dashicons dashicons-yes-alt" style="color:#46b450;"></span></td>
                    <td><span class="dashicons dashicons-minus" style="color:#f56e28;"></span> <?php esc_html_e( 'Limited', 'botfend-anti-bot-firewall' ); ?></td>
                    <td><span class="dashicons dashicons-no-alt" style="color:#dc3232;"></span></td>
                </tr>
            </tbody>
        </table>
        <?php
    }

    private function render_troubleshooting(): void {
        $firewall_dir = WP_CONTENT_DIR . '/botfend-firewall';
        ?>
        <div class="botfend-troubleshooting">
            <h3><?php esc_html_e( 'Common Issues and Solutions', 'botfend-anti-bot-firewall' ); ?></h3>
            <details>
                <summary><strong><?php esc_html_e( 'Firewall not working after enabling Full Protection', 'botfend-anti-bot-firewall' ); ?></strong></summary>
                <div style="margin-top:15px;"><ol><li><?php esc_html_e( 'Wait 2-5 minutes for .user.ini changes to take effect (PHP-FPM cache).', 'botfend-anti-bot-firewall' ); ?></li><li><?php esc_html_e( 'Check if your hosting provider allows auto_prepend_file. Some shared hosts disable this.', 'botfend-anti-bot-firewall' ); ?></li><li><?php esc_html_e( 'Try the Basic Protection mode which works on all hosts.', 'botfend-anti-bot-firewall' ); ?></li><li><?php esc_html_e( 'Check the server error logs for any PHP errors related to the bootstrap file.', 'botfend-anti-bot-firewall' ); ?></li></ol></div>
            </details>
            <details>
                <summary><strong><?php esc_html_e( 'Cannot write to .user.ini or .htaccess', 'botfend-anti-bot-firewall' ); ?></strong></summary>
                <div style="margin-top:15px;"><p><?php esc_html_e( 'You may need to adjust file permissions:', 'botfend-anti-bot-firewall' ); ?></p><pre>chmod 644 .user.ini<br>chmod 644 .htaccess</pre><p><?php esc_html_e( 'Or use the manual instructions above to add the configuration yourself via FTP/cPanel.', 'botfend-anti-bot-firewall' ); ?></p></div>
            </details>
            <details>
                <summary><strong><?php esc_html_e( 'Site shows blank page or error 500', 'botfend-anti-bot-firewall' ); ?></strong></summary>
                <div style="margin-top:15px;"><p><?php esc_html_e( 'This usually indicates a syntax error or path issue. To fix:', 'botfend-anti-bot-firewall' ); ?></p><ol><li><?php esc_html_e( 'Disable Full Protection immediately.', 'botfend-anti-bot-firewall' ); ?></li><li><?php esc_html_e( 'Check the bootstrap file exists at:', 'botfend-anti-bot-firewall' ); ?> <code><?php echo esc_html( $firewall_dir . '/botfend-firewall.php' ); ?></code></li><li><?php esc_html_e( 'Enable WordPress debug mode to see any errors.', 'botfend-anti-bot-firewall' ); ?></li><li><?php esc_html_e( 'Contact support with the error log details.', 'botfend-anti-bot-firewall' ); ?></li></ol></div>
            </details>
            <details>
                <summary><strong><?php esc_html_e( 'Nginx servers - auto_prepend_file not working', 'botfend-anti-bot-firewall' ); ?></strong></summary>
                <div style="margin-top:15px;"><p><?php esc_html_e( 'Nginx does not support .htaccess or .user.ini for PHP values. You must add the directive to your Nginx configuration:', 'botfend-anti-bot-firewall' ); ?></p><pre>location ~ \.php$ {<br>    fastcgi_param PHP_VALUE "auto_prepend_file=<?php echo esc_html( $firewall_dir . '/botfend-firewall.php' ); ?>";<br>}</pre><p><?php esc_html_e( 'Contact your hosting provider to add this for you, or use Basic Protection mode.', 'botfend-anti-bot-firewall' ); ?></p></div>
            </details>
        </div>
        <?php
    }

    private function get_status_html(): array {
        $status = $this->firewall_setup->get_status();
        ob_start();
        $this->render_status_section( $status );
        $status_html = ob_get_clean();
        ob_start();
        $this->render_action_buttons( $status );
        $actions_html = ob_get_clean();
        return [ 'html' => $status_html, 'actions' => $actions_html, 'status' => $status ];
    }

    public function ajax_get_status(): void {
        check_ajax_referer( 'botfend_firewall_nonce', 'nonce' );
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( [ 'message' => esc_html__( 'Unauthorized.', 'botfend-anti-bot-firewall' ) ] );
        }
        $html_data = $this->get_status_html();
        wp_send_json_success( $html_data );
    }

    public function ajax_configure(): void {
        check_ajax_referer( 'botfend_firewall_nonce', 'nonce' );
        
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( [ 'message' => esc_html__( 'Unauthorized.', 'botfend-anti-bot-firewall' ) ] );
        }

        // Pro license check
        if ( ! $this->is_pro ) {
            wp_send_json_error( [ 
                'message' => esc_html__( 'Full Protection requires a Pro license. Please upgrade to enable this feature.', 'botfend-anti-bot-firewall' ),
                'requires_pro' => true 
            ] );
        }

        $method = isset( $_POST['method'] ) ? sanitize_text_field( wp_unslash( $_POST['method'] ) ) : '';
        $result = $this->firewall_setup->configure( $method );
        if ( $result['success'] ) {
            $html_data = $this->get_status_html();
            $result['html_data'] = $html_data;
        }
        wp_send_json( $result );
    }

    public function ajax_disable(): void {
        check_ajax_referer( 'botfend_firewall_nonce', 'nonce' );
        
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( [ 'message' => esc_html__( 'Unauthorized.', 'botfend-anti-bot-firewall' ) ] );
        }

        // Pro license check
        if ( ! $this->is_pro ) {
            wp_send_json_error( [ 
                'message' => esc_html__( 'Full Protection requires a Pro license.', 'botfend-anti-bot-firewall' ),
                'requires_pro' => true 
            ] );
        }

        $delete_files = isset( $_POST['delete_files'] ) && $_POST['delete_files'] == 1;
        $result = $this->firewall_setup->disable( $delete_files );
        if ( $result['success'] ) {
            $html_data = $this->get_status_html();
            $result['html_data'] = $html_data;
        }
        wp_send_json( $result );
    }

    public function ajax_basic_enable(): void {
        check_ajax_referer( 'botfend_firewall_nonce', 'nonce' );
        
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( [ 'message' => esc_html__( 'Unauthorized.', 'botfend-anti-bot-firewall' ) ] );
        }

        // Basic Protection is FREE - no license check needed
        $result = $this->firewall_setup->enable_basic_protection();
        if ( $result['success'] ) {
            $html_data = $this->get_status_html();
            $result['html_data'] = $html_data;
        }
        wp_send_json( $result );
    }

    public function ajax_basic_disable(): void {
        check_ajax_referer( 'botfend_firewall_nonce', 'nonce' );
        
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( [ 'message' => esc_html__( 'Unauthorized.', 'botfend-anti-bot-firewall' ) ] );
        }

        // Basic Protection is FREE - no license check needed
        $result = $this->firewall_setup->disable_basic_protection();
        if ( $result['success'] ) {
            $html_data = $this->get_status_html();
            $result['html_data'] = $html_data;
        }
        wp_send_json( $result );
    }
}