<?php

/**
 * Guard Dog CAPTCHA Protection Page
 * 
 * Professional admin page for managing CAPTCHA protection settings.
 * Showcases Guard Dog's unique 4-provider support as a competitive advantage.
 *
 * @package GuardDog
 * @since 1.6.0
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

class Guard_Dog_Captcha_Page {

    /**
     * Feature instances
     * @var array
     */
    private $features;

    /**
     * CAPTCHA instance
     * @var Guard_Dog_Captcha|null
     */
    private $captcha;

    /**
     * Constructor
     *
     * @param array $features Feature class instances
     */
    public function __construct(array $features = []) {
        $this->features = $features;
        $this->captcha = $features['captcha'] ?? null;
    }

    /**
     * Render the CAPTCHA protection page
     */
    public function render(): void {
        // Prepare data for page
        $this->prepare_page_data();
        
        // Render main page
        $this->render_captcha_main_page();
    }

    /**
     * Prepare data for page partials
     */
    public function prepare_page_data(): void {
        // Get current CAPTCHA settings
        $current_provider = get_option('guard_dog_captcha_provider', 'none');
        $site_key = get_option('guard_dog_' . $current_provider . '_site_key', '');
        $secret_key = get_option('guard_dog_' . $current_provider . '_secret_key', '');
        $theme = get_option('guard_dog_recaptcha_v2_theme', 'light');
        $size = get_option('guard_dog_recaptcha_v2_size', 'normal');
        
        // Pass data to partials via global variables
        $GLOBALS['guard_dog_captcha_data'] = [
            'current_provider' => $current_provider,
            'site_key' => $site_key,
            'secret_key' => $secret_key,
            'theme' => $theme,
            'size' => $size,
        ];
    }

    /**
     * Register settings
     */
    public static function register_settings(): void {
        // Register CAPTCHA provider setting
        register_setting('guard_dog_captcha', 'guard_dog_captcha_provider', [
            'type' => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default' => 'none',
        ]);
        
        // Register provider-specific settings
        $providers = ['recaptcha_v2', 'recaptcha_v3', 'turnstile', 'hcaptcha'];
        
        foreach ($providers as $provider) {
            // Register site key setting for each provider
            register_setting('guard_dog_captcha', 'guard_dog_' . $provider . '_site_key', [
                'type' => 'string',
                'sanitize_callback' => 'sanitize_text_field',
                'default' => '',
            ]);
            
            // Register secret key setting for each provider
            register_setting('guard_dog_captcha', 'guard_dog_' . $provider . '_secret_key', [
                'type' => 'string',
                'sanitize_callback' => function($input) use ($provider) {
                    return self::sanitize_secret_key($input, $provider);
                },
                'default' => '',
            ]);
        }
        
        // Register reCAPTCHA v2 specific settings
        register_setting('guard_dog_captcha', 'guard_dog_recaptcha_v2_theme', [
            'type' => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default' => 'light',
        ]);
        
        register_setting('guard_dog_captcha', 'guard_dog_recaptcha_v2_size', [
            'type' => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default' => 'normal',
        ]);
        
        // Register reCAPTCHA v3 specific settings
        register_setting('guard_dog_captcha', 'guard_dog_recaptcha_v3_threshold', [
            'type' => 'number',
            'sanitize_callback' => function($value) {
                $value = floatval($value);
                return max(0.0, min(1.0, $value)); // Ensure between 0.0 and 1.0
            },
            'default' => 0.5,
        ]);

        // Add provider change handler to clean up inactive credentials AFTER save
        add_action('updated_option', [self::class, 'handle_option_updated'], 10, 3);
        
        // Add success message callback
        add_action('admin_notices', [self::class, 'show_success_message']);
    }



    /**
     * Handle any option update and check if provider changed
     */
    public static function handle_option_updated($option_name, $old_value, $value): void {
        // Only handle our provider option
        if ($option_name !== 'guard_dog_captcha_provider') {
            return;
        }
        
        // Prevent infinite loops by checking if we're already processing
        static $processing = false;
        if ($processing) {
            return;
        }
        
        $new_provider = sanitize_text_field($value);
        $old_provider = $old_value;
        
        // If provider changed, clean up credentials for inactive providers
        if ($new_provider !== $old_provider) {
            $processing = true;
            self::cleanup_inactive_provider_credentials($new_provider);
            $processing = false;
        }
    }

    /**
     * Clean up credentials for inactive providers
     */
    private static function cleanup_inactive_provider_credentials(string $active_provider): void {
        $all_providers = ['recaptcha_v2', 'recaptcha_v3', 'turnstile', 'hcaptcha'];
        
        // Log the cleanup action for debugging
        if (function_exists('Guard_Dog') && Guard_Dog()->debug) {
            Guard_Dog()->debug->captcha('Cleaning up inactive providers', [
                'active_provider' => $active_provider
            ]);
        }
        
        // If 'none' is selected, clear ALL provider credentials
        if ($active_provider === 'none') {
            if (function_exists('Guard_Dog') && Guard_Dog()->debug) {
                Guard_Dog()->debug->captcha('Provider set to none, clearing all credentials');
            }
            
            foreach ($all_providers as $provider) {
                delete_option('guard_dog_' . $provider . '_site_key');
                delete_option('guard_dog_' . $provider . '_secret_key');
                
                // Clear provider-specific settings
                if ($provider === 'recaptcha_v2') {
                    delete_option('guard_dog_recaptcha_v2_theme');
                    delete_option('guard_dog_recaptcha_v2_size');
                } elseif ($provider === 'recaptcha_v3') {
                    delete_option('guard_dog_recaptcha_v3_threshold');
                }
            }
            return; // Exit early to avoid duplicate cleanup
        }
        
        // Clean up inactive providers (when a specific provider is selected)
        foreach ($all_providers as $provider) {
            // Skip the active provider - keep its credentials
            if ($provider === $active_provider) {
                continue;
            }
            
            // Log what we're cleaning
            if (function_exists('Guard_Dog') && Guard_Dog()->debug) {
                Guard_Dog()->debug->captcha('Removing credentials for inactive provider', [
                    'provider' => $provider
                ]);
            }
            
            // Clear credentials for inactive providers
            delete_option('guard_dog_' . $provider . '_site_key');
            delete_option('guard_dog_' . $provider . '_secret_key');
            
            // Clear provider-specific settings
            if ($provider === 'recaptcha_v2') {
                delete_option('guard_dog_recaptcha_v2_theme');
                delete_option('guard_dog_recaptcha_v2_size');
            } elseif ($provider === 'recaptcha_v3') {
                delete_option('guard_dog_recaptcha_v3_threshold');
            }
        }
    }

    /**
     * Show success message after settings are saved
     */
    public static function show_success_message(): void {
        if (!isset($_GET['page']) || sanitize_text_field(wp_unslash($_GET['page'])) !== 'guard-dog-captcha') {
            return;
        }
        // Check if settings were updated
        if (isset($_GET['settings-updated']) && sanitize_text_field(wp_unslash($_GET['settings-updated'])) === 'true') {
            ?>
            <div class="notice notice-success is-dismissible">
                <p><?php esc_html_e('CAPTCHA settings saved successfully.', 'guard-dog'); ?></p>
            </div>
            <?php
        }
    }

    /**
     * Add settings sections and fields
     */
    public static function add_settings_sections(): void {
        // Add main CAPTCHA settings section
        add_settings_section(
            'guard_dog_captcha_main',
            __('CAPTCHA Protection Settings', 'guard-dog'),
            [self::class, 'render_section_description'],
            'guard_dog_captcha'
        );

        // Add provider selection field
        add_settings_field(
            'guard_dog_captcha_provider',
            __('CAPTCHA Provider', 'guard-dog'),
            [self::class, 'render_provider_selection_field'],
            'guard_dog_captcha',
            'guard_dog_captcha_main'
        );

        // Add provider-specific sections
        $providers = [
            'recaptcha_v2' => __('Google reCAPTCHA v2 Settings', 'guard-dog'),
            'recaptcha_v3' => __('Google reCAPTCHA v3 Settings', 'guard-dog'),
            'turnstile' => __('Cloudflare Turnstile Settings', 'guard-dog'),
            'hcaptcha' => __('hCaptcha Settings', 'guard-dog'),
        ];

        foreach ($providers as $provider => $title) {
            add_settings_section(
                'guard_dog_captcha_' . $provider,
                $title,
                [self::class, 'render_provider_section_description'],
                'guard_dog_captcha'
            );

            // Add site key field
            add_settings_field(
                'guard_dog_' . $provider . '_site_key',
                __('Site Key', 'guard-dog'),
                [self::class, 'render_site_key_field'],
                'guard_dog_captcha',
                'guard_dog_captcha_' . $provider,
                ['provider' => $provider]
            );

            // Add secret key field
            add_settings_field(
                'guard_dog_' . $provider . '_secret_key',
                __('Secret Key', 'guard-dog'),
                [self::class, 'render_secret_key_field'],
                'guard_dog_captcha',
                'guard_dog_captcha_' . $provider,
                ['provider' => $provider]
            );

            // Add provider-specific fields
            if ($provider === 'recaptcha_v2') {
                add_settings_field(
                    'guard_dog_recaptcha_v2_theme',
                    __('Theme', 'guard-dog'),
                    [self::class, 'render_theme_field'],
                    'guard_dog_captcha',
                    'guard_dog_captcha_' . $provider
                );

                add_settings_field(
                    'guard_dog_recaptcha_v2_size',
                    __('Size', 'guard-dog'),
                    [self::class, 'render_size_field'],
                    'guard_dog_captcha',
                    'guard_dog_captcha_' . $provider
                );
            }

            if ($provider === 'recaptcha_v3') {
                add_settings_field(
                    'guard_dog_recaptcha_v3_threshold',
                    __('Score Threshold', 'guard-dog'),
                    [self::class, 'render_threshold_field'],
                    'guard_dog_captcha',
                    'guard_dog_captcha_' . $provider
                );
            }
        }
    }

    /**
     * Render section description
     */
    public static function render_section_description(): void {
        ?>
        <p><?php esc_html_e('Configure your CAPTCHA protection settings below.', 'guard-dog'); ?></p>
        <?php
    }

    /**
     * Render provider section description
     */
    public static function render_provider_section_description(): void {
        // This will be handled by the custom form rendering
    }

    /**
     * Render provider selection field
     */
    public static function render_provider_selection_field(): void {
        $current_provider = get_option('guard_dog_captcha_provider', 'none');
        
        $providers = [
            'none' => __('Disabled', 'guard-dog'),
            'recaptcha_v2' => __('Google reCAPTCHA v2', 'guard-dog'),
            'recaptcha_v3' => __('Google reCAPTCHA v3', 'guard-dog'),
            'turnstile' => __('Cloudflare Turnstile', 'guard-dog'),
            'hcaptcha' => __('hCaptcha', 'guard-dog'),
        ];
        ?>
        <select name="guard_dog_captcha_provider" id="guard_dog_captcha_provider">
            <?php foreach ($providers as $value => $label): ?>
                <option value="<?php echo esc_attr($value); ?>"<?php echo selected($current_provider, $value, false); ?>><?php echo esc_html($label); ?></option>
            <?php endforeach; ?>
        </select>
        
        <p class="description"><?php esc_html_e('Choose your preferred CAPTCHA provider. Each provider has different features and privacy policies.', 'guard-dog'); ?></p>
        <?php
    }

    /**
     * Render site key field
     */
    public static function render_site_key_field(array $args): void {
        $provider = $args['provider'];
        $option_name = 'guard_dog_' . $provider . '_site_key';
        $value = get_option($option_name, '');
        ?>
        <input type="text" name="<?php echo esc_attr($option_name); ?>" value="<?php echo esc_attr($value); ?>" class="regular-text" autocomplete="off" />
        <?php
    }

    /**
     * Render secret key field
     */
    public static function render_secret_key_field(array $args): void {
        $provider = $args['provider'];
        $option_name = 'guard_dog_' . $provider . '_secret_key';
        $value = get_option($option_name, '');
        
        // Obfuscate the value for display
        $obfuscated_value = !empty($value) ? str_repeat('•', 20) : '';
        ?>
        <input type="password" name="<?php echo esc_attr($option_name); ?>" value="<?php echo esc_attr($obfuscated_value); ?>" class="regular-text" autocomplete="new-password" data-has-value="<?php echo esc_attr(!empty($value) ? '1' : '0'); ?>" />
        <?php
    }

    /**
     * Render theme field
     */
    public static function render_theme_field(): void {
        $value = get_option('guard_dog_recaptcha_v2_theme', 'light');
        ?>
        <select name="guard_dog_recaptcha_v2_theme">
            <option value="light"<?php echo selected($value, 'light', false); ?>><?php esc_html_e('Light', 'guard-dog'); ?></option>
            <option value="dark"<?php echo selected($value, 'dark', false); ?>><?php esc_html_e('Dark', 'guard-dog'); ?></option>
        </select>
        <?php
    }

    /**
     * Render size field
     */
    public static function render_size_field(): void {
        $value = get_option('guard_dog_recaptcha_v2_size', 'normal');
        ?>
        <select name="guard_dog_recaptcha_v2_size">
            <option value="normal"<?php echo selected($value, 'normal', false); ?>><?php esc_html_e('Normal', 'guard-dog'); ?></option>
            <option value="compact"<?php echo selected($value, 'compact', false); ?>><?php esc_html_e('Compact', 'guard-dog'); ?></option>
        </select>
        <?php
    }

    /**
     * Render threshold field
     */
    public static function render_threshold_field(): void {
        $value = get_option('guard_dog_recaptcha_v3_threshold', 0.5);
        ?>
        <input type="number" name="guard_dog_recaptcha_v3_threshold" value="<?php echo esc_attr($value); ?>" min="0.0" max="1.0" step="0.1" class="small-text" />
        <p class="description"><?php esc_html_e('Score threshold for reCAPTCHA v3 (0.0 to 1.0). Lower values are more strict.', 'guard-dog'); ?></p>
        <?php
    }

    /**
     * Sanitize secret key input - handles obfuscated values
     */
    private static function sanitize_secret_key($input, string $provider): string {
        // If input is null or empty, user wants to clear the key
        if ($input === null || $input === '') {
            return '';
        }
        
        // Ensure input is a string
        $input = (string) $input;
        
        // Check if the input contains obfuscation characters (bullet points •)
        // If so, this means the user didn't change the key, so keep the existing value
        if (strpos($input, '•') !== false) {
            $existing_key = get_option('guard_dog_' . $provider . '_secret_key', '');
            return $existing_key;
        }
        
        // Otherwise, this is a new key that should be sanitized and saved
        return sanitize_text_field($input);
    }

    /**
     * Render the main CAPTCHA page
     */
    private function render_captcha_main_page(): void {
        // Get data from global variables
        $captcha_data = $GLOBALS['guard_dog_captcha_data'] ?? [
            'current_provider' => 'none',
            'site_key' => '',
            'secret_key' => '',
            'theme' => 'light',
            'size' => 'normal'
        ];

        $current_provider = $captcha_data['current_provider'];
        $site_key = $captcha_data['site_key'];
        $secret_key = $captcha_data['secret_key'];
        $theme = $captcha_data['theme'];
        $size = $captcha_data['size'];
        ?>
        <div class="wrap guard-dog-admin">
            <h1 class="guard-dog-page-title">
                <span class="dashicons dashicons-shield-alt"></span>
                <?php esc_html_e('CAPTCHA Protection', 'guard-dog'); ?>
            </h1>
            
            <p class="guard-dog-page-subtitle">
                <?php esc_html_e('Protect your site from automated attacks with multiple CAPTCHA providers, including privacy-focused options.', 'guard-dog'); ?>
            </p>

            <?php 
            // Display settings messages (success/error notifications)
            settings_errors('guard_dog_captcha');
            ?>

            <div class="guard-dog-admin-grid">
                <div class="guard-dog-main-content">
                    <form method="post" action="options.php" class="guard-dog-settings-form">
                        <?php 
                        // Add WordPress Settings API fields
                        settings_fields('guard_dog_captcha');
                        ?>

                        <!-- Provider Selection -->
                        <?php $this->render_provider_selection($current_provider); ?>

                        <!-- Provider Configuration -->
                        <?php $this->render_provider_configuration($current_provider, $site_key, $secret_key, $theme, $size); ?>
                        
                        <p class="submit">
                            <input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e('Save CAPTCHA Settings', 'guard-dog'); ?>">
                        </p>
                    </form>
                </div>

                <div class="guard-dog-sidebar">
                    <?php $this->render_captcha_status($current_provider, $site_key); ?>
                    <?php $this->render_captcha_help(); ?>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Render CAPTCHA form content without page wrapper or form tags
     *
     * Used by the consolidated Authentication page to embed CAPTCHA
     * settings within its own form. Renders provider selection and
     * provider configuration sections only.
     *
     * @return void
     */
    public function render_captcha_form_content(): void {
        $this->prepare_page_data();

        $captcha_data = $GLOBALS['guard_dog_captcha_data'] ?? [
            'current_provider' => 'none',
            'site_key' => '',
            'secret_key' => '',
            'theme' => 'light',
            'size' => 'normal',
        ];

        $this->render_provider_selection($captcha_data['current_provider']);
        $this->render_provider_configuration(
            $captcha_data['current_provider'],
            $captcha_data['site_key'],
            $captcha_data['secret_key'],
            $captcha_data['theme'],
            $captcha_data['size']
        );
    }

    /**
     * Render provider selection section
     *
     * @param string $current_provider Current selected provider
     */
    private function render_provider_selection(string $current_provider): void {
        ?>
        <div class="guard-dog-form-section">
            <h3>
                <span class="dashicons dashicons-admin-network"></span>
                <?php esc_html_e('Choose CAPTCHA Provider', 'guard-dog'); ?>
            </h3>
            
            <div class="guard-dog-radio-grid">
                <div class="guard-dog-radio-item <?php echo esc_attr($current_provider === 'none' ? 'selected' : ''); ?>">
                    <label>
                        <input type="radio" name="guard_dog_captcha_provider" value="none" <?php checked($current_provider, 'none'); ?>>
                        <strong><?php esc_html_e('Disabled', 'guard-dog'); ?></strong>
                    </label>
                    <div class="guard-dog-provider-info">
                        <?php esc_html_e('No CAPTCHA protection', 'guard-dog'); ?>
                    </div>
                </div>
                
                <div class="guard-dog-radio-item <?php echo esc_attr($current_provider === 'recaptcha_v2' ? 'selected' : ''); ?>">
                    <label>
                        <input type="radio" name="guard_dog_captcha_provider" value="recaptcha_v2" <?php checked($current_provider, 'recaptcha_v2'); ?>>
                        <strong><?php esc_html_e('reCAPTCHA v2', 'guard-dog'); ?></strong>
                    </label>
                    <div class="guard-dog-provider-info">
                        <?php esc_html_e('Classic checkbox challenge', 'guard-dog'); ?>
                    </div>
                </div>
                
                <div class="guard-dog-radio-item <?php echo esc_attr($current_provider === 'recaptcha_v3' ? 'selected' : ''); ?>">
                    <label>
                        <input type="radio" name="guard_dog_captcha_provider" value="recaptcha_v3" <?php checked($current_provider, 'recaptcha_v3'); ?>>
                        <strong><?php esc_html_e('reCAPTCHA v3', 'guard-dog'); ?></strong>
                    </label>
                    <div class="guard-dog-provider-info">
                        <?php esc_html_e('Invisible protection', 'guard-dog'); ?>
                    </div>
                </div>
                
                <div class="guard-dog-radio-item <?php echo esc_attr($current_provider === 'turnstile' ? 'selected' : ''); ?>">
                    <label>
                        <input type="radio" name="guard_dog_captcha_provider" value="turnstile" <?php checked($current_provider, 'turnstile'); ?>>
                        <strong><?php esc_html_e('Cloudflare Turnstile', 'guard-dog'); ?></strong>
                    </label>
                    <div class="guard-dog-provider-info">
                        <?php esc_html_e('Privacy-focused alternative', 'guard-dog'); ?>
                    </div>
                </div>
                
                <div class="guard-dog-radio-item <?php echo esc_attr($current_provider === 'hcaptcha' ? 'selected' : ''); ?>">
                    <label>
                        <input type="radio" name="guard_dog_captcha_provider" value="hcaptcha" <?php checked($current_provider, 'hcaptcha'); ?>>
                        <strong><?php esc_html_e('hCaptcha', 'guard-dog'); ?></strong>
                    </label>
                    <div class="guard-dog-provider-info">
                        <?php esc_html_e('Privacy-first verification', 'guard-dog'); ?>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Render provider configuration section
     *
     * @param string $current_provider Current selected provider
     * @param string $site_key Site key
     * @param string $secret_key Secret key
     * @param string $theme Theme setting
     * @param string $size Size setting
     */
    private function render_provider_configuration(string $current_provider, string $site_key, string $secret_key, string $theme, string $size): void {
        $providers = ['recaptcha_v2', 'recaptcha_v3', 'turnstile', 'hcaptcha'];
        
        foreach ($providers as $provider):
            $active_class = $current_provider === $provider ? 'active' : '';
            ?>
            <div class="guard-dog-form-section guard-dog-provider-fields <?php echo esc_attr($active_class); ?>" data-provider="<?php echo esc_attr($provider); ?>">
                <h3>
                    <span class="dashicons dashicons-admin-settings"></span>
                    <?php
                    // Translators: %s is the name of the CAPTCHA provider.
                    printf(esc_html__('%s Configuration', 'guard-dog'), esc_html(ucwords(str_replace('_', ' ', $provider))));
                    ?>
                </h3>
                
                <table class="guard-dog-form-table">
                    <tr>
                        <th><?php esc_html_e('Site Key', 'guard-dog'); ?></th>
                        <td>
                            <input type="text" name="guard_dog_<?php echo esc_attr($provider); ?>_site_key" value="<?php echo esc_attr(get_option('guard_dog_' . $provider . '_site_key', '')); ?>" autocomplete="off" />
                        </td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Secret Key', 'guard-dog'); ?></th>
                        <td>
                            <?php
                            $secret_key_value = get_option('guard_dog_' . $provider . '_secret_key', '');
                            $obfuscated_value = '';
                            if (!empty($secret_key_value)) {
                                // Obfuscate the secret key server-side for security
                                $key_length = strlen($secret_key_value);
                                if ($key_length > 8) {
                                    // Show first 4 and last 4 characters with dots in between
                                    $obfuscated_value = substr($secret_key_value, 0, 4) . str_repeat('•', $key_length - 8) . substr($secret_key_value, -4);
                                } else {
                                    // For shorter keys, just show dots
                                    $obfuscated_value = str_repeat('•', $key_length);
                                }
                            }
                            ?>
                            <input type="password" 
                                   name="guard_dog_<?php echo esc_attr($provider); ?>_secret_key" 
                                   value="<?php echo esc_attr($obfuscated_value); ?>" 
                                   placeholder="<?php echo !empty($secret_key_value) ? esc_attr__('Key configured - enter new key to update', 'guard-dog') : esc_attr__('Enter your secret key', 'guard-dog'); ?>"
                                   data-has-value="<?php echo esc_attr(!empty($secret_key_value) ? '1' : '0'); ?>"
                                   autocomplete="new-password" />
                            <?php if (!empty($secret_key_value)): ?>
                            <p class="description">
                                <?php esc_html_e('Secret key is configured. Enter a new key to update, or leave unchanged to keep current key.', 'guard-dog'); ?>
                            </p>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php if ($provider === 'recaptcha_v2'): ?>
                    <tr>
                        <th><?php esc_html_e('Theme', 'guard-dog'); ?></th>
                        <td>
                            <select name="guard_dog_recaptcha_v2_theme">
                                <option value="light" <?php selected(get_option('guard_dog_recaptcha_v2_theme'), 'light'); ?>><?php esc_html_e('Light', 'guard-dog'); ?></option>
                                <option value="dark" <?php selected(get_option('guard_dog_recaptcha_v2_theme'), 'dark'); ?>><?php esc_html_e('Dark', 'guard-dog'); ?></option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Size', 'guard-dog'); ?></th>
                        <td>
                            <select name="guard_dog_recaptcha_v2_size">
                                <option value="normal" <?php selected(get_option('guard_dog_recaptcha_v2_size'), 'normal'); ?>><?php esc_html_e('Normal', 'guard-dog'); ?></option>
                                <option value="compact" <?php selected(get_option('guard_dog_recaptcha_v2_size'), 'compact'); ?>><?php esc_html_e('Compact', 'guard-dog'); ?></option>
                            </select>
                        </td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($provider === 'recaptcha_v3'): ?>
                    <tr>
                        <th><?php esc_html_e('Score Threshold', 'guard-dog'); ?></th>
                        <td>
                            <input type="number" name="guard_dog_recaptcha_v3_threshold" min="0.0" max="1.0" step="0.1" value="<?php echo esc_attr(get_option('guard_dog_recaptcha_v3_threshold', '0.5')); ?>" />
                            <p class="description"><?php esc_html_e('Minimum score to accept (0.0 = strict, 1.0 = lenient)', 'guard-dog'); ?></p>
                        </td>
                    </tr>
                    <?php endif; ?>
                </table>
            </div>
            <?php
        endforeach;
    }

    /**
     * Render CAPTCHA status card
     *
     * @param string $current_provider Current selected provider
     * @param string $site_key Site key
     */
    private function render_captcha_status(string $current_provider, string $site_key): void {
        $status = ($current_provider !== 'none' && !empty($site_key)) ? 'enabled' : 'disabled';
        $status_text = $status === 'enabled' ? __('Active', 'guard-dog') : __('Inactive', 'guard-dog');
        ?>
        <div class="guard-dog-card">
            <h3><?php esc_html_e('CAPTCHA Status', 'guard-dog'); ?></h3>
            
            <div class="guard-dog-status-indicator <?php echo esc_attr($status); ?>">
                <?php echo esc_html($status_text); ?>
            </div>
            
            <?php if ($current_provider !== 'none'): ?>
                <p class="guard-dog-status-details">
                    <strong><?php esc_html_e('Provider:', 'guard-dog'); ?></strong>
                    <?php echo esc_html(ucwords(str_replace('_', ' ', $current_provider))); ?>
                </p>
                
                <?php if (!empty($site_key)): ?>
                    <p class="guard-dog-status-details">
                        <strong><?php esc_html_e('Site Key:', 'guard-dog'); ?></strong>
                        <?php echo esc_html(substr($site_key, 0, 10) . '...'); ?>
                    </p>
                <?php else: ?>
                    <p class="guard-dog-status-warning">
                        <?php esc_html_e('Site key not configured', 'guard-dog'); ?>
                    </p>
                <?php endif; ?>
            <?php else: ?>
                <p class="guard-dog-status-info">
                    <?php esc_html_e('CAPTCHA protection is currently disabled.', 'guard-dog'); ?>
                </p>
            <?php endif; ?>
        </div>
        <?php
    }

    /**
     * Render CAPTCHA help card
     */
    private function render_captcha_help(): void {
        ?>
        <div class="guard-dog-card">
            <h3><?php esc_html_e('Setup Help', 'guard-dog'); ?></h3>
            
            <div class="guard-dog-help-section">
                <h4><?php esc_html_e('Google reCAPTCHA', 'guard-dog'); ?></h4>
                <ol>
                    <li><?php esc_html_e('Visit the Google reCAPTCHA admin console', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Create a new site registration', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Add your domain(s) to the allowed list', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Copy the Site Key and Secret Key', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Paste them in the configuration fields above', 'guard-dog'); ?></li>
                </ol>
                <p><a href="https://www.google.com/recaptcha/admin" target="_blank"><?php esc_html_e('Get reCAPTCHA Keys →', 'guard-dog'); ?></a></p>
            </div>
            
            <div class="guard-dog-help-section">
                <h4><?php esc_html_e('Cloudflare Turnstile', 'guard-dog'); ?></h4>
                <ol>
                    <li><?php esc_html_e('Visit the Cloudflare Turnstile dashboard', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Create a new site', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Add your domain(s) to the allowed list', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Copy the Site Key and Secret Key', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Paste them in the configuration fields above', 'guard-dog'); ?></li>
                </ol>
                <p><a href="https://dash.cloudflare.com/?to=/:account/turnstile" target="_blank"><?php esc_html_e('Get Turnstile Keys →', 'guard-dog'); ?></a></p>
            </div>
            
            <div class="guard-dog-help-section">
                <h4><?php esc_html_e('hCaptcha', 'guard-dog'); ?></h4>
                <ol>
                    <li><?php esc_html_e('Visit the hCaptcha dashboard', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Create a new site', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Add your domain(s) to the allowed list', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Copy the Site Key and Secret Key', 'guard-dog'); ?></li>
                    <li><?php esc_html_e('Paste them in the configuration fields above', 'guard-dog'); ?></li>
                </ol>
                <p><a href="https://dashboard.hcaptcha.com/" target="_blank"><?php esc_html_e('Get hCaptcha Keys →', 'guard-dog'); ?></a></p>
            </div>
            
            <div class="guard-dog-help-section">
                <h4><?php esc_html_e('Why Multiple Providers?', 'guard-dog'); ?></h4>
                <p><?php esc_html_e('Guard Dog supports multiple CAPTCHA providers to give you flexibility and choice. Each provider has different strengths:', 'guard-dog'); ?></p>
                <ul>
                    <li><strong><?php esc_html_e('reCAPTCHA v2:', 'guard-dog'); ?></strong> <?php esc_html_e('Most familiar to users, good for high-security needs', 'guard-dog'); ?></li>
                    <li><strong><?php esc_html_e('reCAPTCHA v3:', 'guard-dog'); ?></strong> <?php esc_html_e('Invisible protection, better user experience', 'guard-dog'); ?></li>
                    <li><strong><?php esc_html_e('Turnstile:', 'guard-dog'); ?></strong> <?php esc_html_e('Privacy-focused, no tracking, GDPR compliant', 'guard-dog'); ?></li>
                    <li><strong><?php esc_html_e('hCaptcha:', 'guard-dog'); ?></strong> <?php esc_html_e('Privacy-first, no personal data collection', 'guard-dog'); ?></li>
                </ul>
            </div>
        </div>
        <?php
    }
}
