<?php

namespace GriffinForms\Admin\Html\Pages\Settings;

use GriffinForms\Includes\Integrations\Categories\Captcha as CaptchaRegistry;

class AntiSpam extends \GriffinForms\Admin\Html\Pages\Settings\Format
{
    protected function settingsGroupOptions()
    {
        $this->getOptionHtml('enable_honeypot');
        $this->getActiveCaptchaProviderHtml();
        $this->renderRateLimitInfoBanner();
        $this->getOptionHtml('rate_limit_storage');
        // Add more options as needed
    }

    protected function getActiveCaptchaProviderHtml()
    {
        $registry = CaptchaRegistry::getInstance();
        $options = $registry->getProviderOptions();

        if (empty($options)) {
            return;
        }

        $input_id = 'griffinforms-settings-antispam-activecaptchaprovider';
        $current_raw = sanitize_key($this->settings->getOption('active_captcha_provider', ''));
        $current = $current_raw ? $current_raw : 'none';
        $this->option = $this->settings->getOptionHistory('active_captcha_provider');

        echo '<tr>';
        echo '<th scope="row"><label for="' . esc_attr($input_id) . '">' . esc_html__('Active CAPTCHA Provider', 'griffinforms-form-builder') . '</label></th>';
        echo '<td><fieldset>';
        echo '<select id="' . esc_attr($input_id) . '" name="active_captcha_provider">';
        echo '<option value="none"' . selected('none', $current, false) . '>' . esc_html__('None (Disabled)', 'griffinforms-form-builder') . '</option>';

        foreach ($options as $option) {
            $label = $option['label'];
            if (empty($option['configured'])) {
                $label .= ' ' . esc_html__('(Not configured)', 'griffinforms-form-builder');
            }

            echo '<option value="' . esc_attr($option['id']) . '"' . selected($option['id'], $current, false) . '>' . esc_html($label) . '</option>';
        }

        echo '</select>';
        echo '<p class="description">' . esc_html__('Choose which CAPTCHA integration protects forms globally. Pick “None” to disable CAPTCHA entirely. Configure each provider from the Integrations modal before selecting it here.', 'griffinforms-form-builder') . '</p>';
        $this->displayOptionHistory();
        echo '</fieldset></td>';
        echo '</tr>';
    }

    protected function getEnableHoneyPotHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('enable_honeypot');    
        $current_value = !empty($this->option['current_value']) ? $this->option['current_value'] : 0;
    
        echo '<fieldset>';
        echo '<label for="' . esc_attr($input_id) . '">';
        echo '<input name="enable_honeypot" type="checkbox" id="' . esc_attr($input_id) . '" value="1"' . checked(1, $current_value, false) . '>';
        echo esc_html($this->lang->getText('enable_honeypot_checkbox_label'));
        echo '</label>';
        $this->getOptionDescription('enable_honeypot');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    // TODO: Remove legacy reCAPTCHA settings once admin modal fully replaces them.
    // protected function getEnableRecaptchaHtml($input_id = '')
    // {
    //     // Legacy UI kept for reference; all CAPTCHA config now lives in Integrations modal.
    // }

    // protected function getRecaptchaSiteKeyHtml($is_enabled = 0) {}

    // protected function getRecaptchaSecretKeyHtml($is_enabled = 0) {}

    // protected function getRecaptchaVersionHtml($is_enabled = 0) {}

    protected function getRateLimitStorageHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('rate_limit_storage');
        $current_value = !empty($this->option['current_value']) ? $this->option['current_value'] : 'auto';
        $input_id = $input_id ? $input_id : 'griffinforms-settings-antispam-ratelimitstorage';

        echo '<fieldset>';
        echo '<select id="' . esc_attr($input_id) . '" name="rate_limit_storage">';
        echo '<option value="auto"' . selected('auto', $current_value, false) . '>' . esc_html($this->lang->getText('rate_limit_storage_option_auto')) . '</option>';
        echo '<option value="transient"' . selected('transient', $current_value, false) . '>' . esc_html($this->lang->getText('rate_limit_storage_option_transient')) . '</option>';
        echo '<option value="db"' . selected('db', $current_value, false) . '>' . esc_html($this->lang->getText('rate_limit_storage_option_db')) . '</option>';
        echo '</select><br>';
        $this->getOptionDescription('rate_limit_storage');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    /**
     * Renders an informational banner about the current hardcoded rate limits
     * used by the frontend BotLimiter. This is a temporary notice until
     * these values become fully configurable via settings.
     */
    protected function renderRateLimitInfoBanner()
    {
        // Keep in sync with BotLimiter defaults:
        // burst: 5 actions / 15s, window: 30 actions / 5m
        $burst_limit  = 5;
        $burst_ttl_s  = 15;
        $window_limit = 30;
        $window_ttl_s = 300; // 5 minutes

        $message = sprintf(
            /* translators: 1: burst limit, 2: burst seconds, 3: window limit, 4: window minutes */
            esc_html__(
                'Current protection: up to %1$d actions every %2$d seconds (burst) and up to %3$d actions every %4$d minutes (rolling window). These limits are currently hardcoded and apply to page submissions.',
                'griffinforms-form-builder'
            ),
            $burst_limit,
            $burst_ttl_s,
            $window_limit,
            (int) round($window_ttl_s / 60)
        );

        echo '<tr><td colspan="2"><div class="" style="margin:12px 0;">'
           . '<p style="margin:8px 0;font-weight:600;">' . esc_html($message) . '</p>'
           . '</div></td></tr>';
    }
}
