{% extends request.ajax ? 'layout_blank.html.twig' : 'layout_default.html.twig' %}

{% block meta_title %}{{ 'Anti-Spam Settings'|trans }}{% endblock %}

{% set active_menu = 'system' %}

{% block breadcrumbs %}
    <ul class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{{ '/'|url(area: 'admin') }}">
                <svg class="icon">
                    <use xlink:href="#home"/>
                </svg>
            </a>
        </li>
        <li class="breadcrumb-item">
            <a href="{{ 'system'|url(area: 'admin') }}">{{ 'Settings'|trans }}</a>
        </li>
        <li class="breadcrumb-item active" aria-current="page">{{ 'Anti-Spam Settings'|trans }}</li>
    </ul>
{% endblock %}

{% block content %}
    {% set params = admin.extension_config_get({ ext: 'mod_antispam' }) %}
    <div class="card">
        <div class="card-header">
            <div>
                <h3 class="card-title">{{ 'Anti-Spam Settings'|trans }}</h3>
                <p class="card-subtitle">{{ 'Configure anti-spam checks for signups, contact forms, public tickets, and automated abuse protection.'|trans }}</p>
            </div>
            <div class="card-actions">
                <a href="https://docs.fossbilling.org/admin-guide/anti-spam/" target="_blank" rel="noopener" class="btn btn-outline-primary">
                    <svg class="icon">
                        <use xlink:href="#help" />
                    </svg>
                    {{ 'Read the documentation'|trans }}
                </a>
            </div>
        </div>

        <form method="post" action="{{ 'extension/config_save'|api_url(role: 'admin') }}" {{ fb_api_form({message: 'Configuration Updated'|trans}) }}>
            <input type="hidden" name="ext" value="mod_antispam">

            <div class="card-body">
                <div class="alert alert-info" role="alert">
                    <strong class="me-1">{{ 'Information:'|trans }}</strong>{{ 'Use these protections together: CAPTCHA and honeypot help stop automated signups, while IP and email checks help block abusive traffic.'|trans }}
                </div>

                <div class="row row-cards">
                    <div class="col-12">
                        <section class="card card-sm">
                            <div class="card-body">
                                <div class="d-flex flex-column flex-lg-row align-items-lg-start justify-content-between gap-3 mb-3">
                                    <div>
                                        <h4 class="card-title mb-2">{{ 'IP Blocking'|trans }}</h4>
                                        <p class="text-secondary mb-0">{{ 'Blocked IPs are not allowed to log in, register, or open public tickets. Add one IP address per line.'|trans }}</p>
                                    </div>
                                    <label class="form-check form-switch form-switch-2 m-0">
                                        <input id="block_ips_check" class="form-check-input" name="block_ips" type="checkbox" {% if params.block_ips %}checked="checked"{% endif %}>
                                        <span class="form-check-label">{{ 'Enable IP Blocking'|trans }}</span>
                                    </label>
                                </div>

                                <label class="form-label" for="blocked_ips">{{ 'Blocked IP List'|trans }}</label>
                                <textarea id="blocked_ips" class="form-control" name="blocked_ips" rows="5" placeholder="127.0.0.1">{{ params.blocked_ips }}</textarea>
                            </div>
                        </section>
                    </div>

                    <div class="col-12">
                        <section class="card card-sm">
                            <div class="card-body">
                                <div class="d-flex flex-column flex-lg-row align-items-lg-start justify-content-between gap-3 mb-3">
                                    <div>
                                        <h4 class="card-title mb-2">{{ 'CAPTCHA Protection'|trans }}</h4>
                                        <p class="text-secondary mb-0">{{ 'Enable CAPTCHA for client registration and contact forms. Get keys from %recaptcha%, %turnstile%, or %hcaptcha%.'|trans({ '%recaptcha%': '<a href="https://www.google.com/recaptcha/about/" target="_blank">Google reCAPTCHA</a>', '%turnstile%': '<a href="https://www.cloudflare.com/products/turnstile/" target="_blank">Cloudflare Turnstile</a>', '%hcaptcha%': '<a href="https://www.hcaptcha.com" target="_blank">hCaptcha</a>' })|raw }}</p>
                                    </div>
                                    <label class="form-check form-switch form-switch-2 m-0">
                                        <input id="captcha_enabled" class="form-check-input" name="captcha_enabled" type="checkbox" {% if params.captcha_enabled %}checked="checked"{% endif %}>
                                        <span class="form-check-label">{{ 'Enable CAPTCHA'|trans }}</span>
                                    </label>
                                </div>

                                <div class="row g-3">
                                    <div class="col-lg-6">
                                        <label class="form-label" for="provider-select">{{ 'CAPTCHA Provider'|trans }}</label>
                                        <select name="captcha_provider" class="form-select" id="provider-select">
                                            <option value="recaptcha_v2" {% if not params.captcha_provider or params.captcha_provider == 'recaptcha_v2' %}selected{% endif %}>{{ 'Google reCAPTCHA v2'|trans }}</option>
                                            <option value="recaptcha_v3" {% if params.captcha_provider == 'recaptcha_v3' %}selected{% endif %}>{{ 'Google reCAPTCHA v3'|trans }}</option>
                                            <option value="turnstile" {% if params.captcha_provider == 'turnstile' %}selected{% endif %}>{{ 'Cloudflare Turnstile'|trans }}</option>
                                            <option value="hcaptcha" {% if params.captcha_provider == 'hcaptcha' %}selected{% endif %}>{{ 'hCaptcha'|trans }}</option>
                                        </select>
                                    </div>
                                </div>

                                <div id="recaptcha_v2_settings" class="mt-3">
                                    <div class="row g-3">
                                        <div class="col-lg-6">
                                            <label class="form-label" for="captcha_recaptcha_publickey">{{ 'reCAPTCHA Site Key'|trans }}</label>
                                            <input class="form-control" type="text" id="captcha_recaptcha_publickey" name="captcha_recaptcha_publickey" value="{{ params.captcha_recaptcha_publickey }}">
                                        </div>
                                        <div class="col-lg-6">
                                            <label class="form-label" for="captcha_recaptcha_privatekey">{{ 'reCAPTCHA Secret Key'|trans }}</label>
                                            <input class="form-control" type="text" id="captcha_recaptcha_privatekey" name="captcha_recaptcha_privatekey" value="{{ params.captcha_recaptcha_privatekey }}">
                                        </div>
                                        <div class="col-lg-6" id="recaptcha_v3_threshold_wrap">
                                            <label class="form-label" for="captcha_recaptcha_v3_threshold">{{ 'reCAPTCHA v3 Minimum Score'|trans }}</label>
                                            <input class="form-control" type="number" id="captcha_recaptcha_v3_threshold" name="captcha_recaptcha_v3_threshold" value="{{ params.captcha_recaptcha_v3_threshold|default(0.5) }}" min="0" max="1" step="0.1">
                                            <small class="form-hint">{{ 'Google returns a score from 0.0 to 1.0. Higher thresholds are stricter.'|trans }}</small>
                                        </div>
                                    </div>
                                </div>

                                <div id="turnstile_settings" class="mt-3">
                                    <div class="row g-3">
                                        <div class="col-lg-6">
                                            <label class="form-label" for="turnstile_site_key">{{ 'Turnstile Site Key'|trans }}</label>
                                            <input class="form-control" type="text" id="turnstile_site_key" name="turnstile_site_key" value="{{ params.turnstile_site_key }}">
                                        </div>
                                        <div class="col-lg-6">
                                            <label class="form-label" for="turnstile_secret_key">{{ 'Turnstile Secret Key'|trans }}</label>
                                            <input class="form-control" type="text" id="turnstile_secret_key" name="turnstile_secret_key" value="{{ params.turnstile_secret_key }}">
                                        </div>
                                    </div>
                                </div>

                                <div id="hcaptcha_settings" class="mt-3">
                                    <div class="row g-3">
                                        <div class="col-lg-6">
                                            <label class="form-label" for="hcaptcha_site_key">{{ 'hCaptcha Site Key'|trans }}</label>
                                            <input class="form-control" type="text" id="hcaptcha_site_key" name="hcaptcha_site_key" value="{{ params.hcaptcha_site_key }}">
                                        </div>
                                        <div class="col-lg-6">
                                            <label class="form-label" for="hcaptcha_secret_key">{{ 'hCaptcha Secret Key'|trans }}</label>
                                            <input class="form-control" type="text" id="hcaptcha_secret_key" name="hcaptcha_secret_key" value="{{ params.hcaptcha_secret_key }}">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </section>
                    </div>

                    <div class="col-xl-4 col-md-6">
                        <section class="card card-sm h-100">
                            <div class="card-body">
                                <div class="d-flex flex-column gap-3 h-100">
                                    <div>
                                        <h4 class="card-title mb-2">{{ 'Stop Forum Spam'|trans }}</h4>
                                        <p class="text-secondary mb-0">{{ 'Check client IP addresses against the <a href="https://stopforumspam.com/" target="_blank">Stop Forum Spam</a> database before registration.'|trans|raw }}</p>
                                    </div>
                                    <label class="form-check form-switch form-switch-2 mt-auto">
                                        <input id="sfs" class="form-check-input" name="sfs" type="checkbox" {% if params.sfs %}checked="checked"{% endif %}>
                                        <span class="form-check-label">{{ 'Enable Stop Forum Spam'|trans }}</span>
                                    </label>
                                </div>
                            </div>
                        </section>
                    </div>

                    <div class="col-xl-4 col-md-6">
                        <section class="card card-sm h-100">
                            <div class="card-body">
                                <div class="d-flex flex-column gap-3 h-100">
                                    <div>
                                        <h4 class="card-title mb-2">{{ 'Disposable Email Protection'|trans }}</h4>
                                        <p class="text-secondary mb-2">{{ 'Check email addresses against a database of temporary and disposable email domains.'|trans }}</p>
                                        <p class="text-secondary mb-0">{{ 'This database is updated daily and provided free of charge by the FakeFilter project under the BSD 3-Clause License.'|trans }}</p>
                                    </div>
                                    <label class="form-check form-switch form-switch-2 mt-auto">
                                        <input id="check_temp_emails" class="form-check-input" name="check_temp_emails" type="checkbox" {% if params.check_temp_emails %}checked="checked"{% endif %}>
                                        <span class="form-check-label">{{ 'Check for Disposable Email Addresses'|trans }}</span>
                                    </label>
                                </div>
                            </div>
                        </section>
                    </div>

                    <div class="col-xl-4 col-md-12">
                        <section class="card card-sm h-100">
                            <div class="card-body">
                                <div class="d-flex flex-column gap-3 h-100">
                                    <div>
                                        <h4 class="card-title mb-2">{{ 'Honeypot Protection'|trans }}</h4>
                                        <p class="text-secondary mb-0">{{ 'Add a hidden form field that bots typically fill out but humans do not. Registration fails when that field is submitted.'|trans }}</p>
                                    </div>
                                    <label class="form-check form-switch form-switch-2">
                                        <input id="honeypot_enabled" class="form-check-input" name="honeypot_enabled" type="checkbox" {% if params.honeypot_enabled ?? true %}checked="checked"{% endif %}>
                                        <span class="form-check-label">{{ 'Enable Honeypot Protection'|trans }}</span>
                                    </label>
                                    <div>
                                        <label class="form-label" for="honeypot_field">{{ 'Honeypot Field Name'|trans }}</label>
                                        <input class="form-control" type="text" id="honeypot_field" name="honeypot_field" value="{{ params.honeypot_field|default('bio') }}">
                                        <small class="form-hint">{{ 'Change this if you suspect bots are bypassing the honeypot field.'|trans }}</small>
                                    </div>
                                </div>
                            </div>
                        </section>
                    </div>
                </div>
            </div>

            <div class="card-footer text-end">
                <button type="submit" class="btn btn-primary">{{ 'Update'|trans }}</button>
            </div>
        </form>
    </div>
{% endblock %}

{% block js %}
<script>
document.addEventListener('DOMContentLoaded', function () {
    function toggleProviderSettings() {
        const selectedProvider = document.getElementById('provider-select').value;

        document.getElementById('recaptcha_v2_settings').style.display = selectedProvider === 'recaptcha_v2' || selectedProvider === 'recaptcha_v3' ? 'block' : 'none';
        document.getElementById('recaptcha_v3_threshold_wrap').style.display = selectedProvider === 'recaptcha_v3' ? 'block' : 'none';
        document.getElementById('turnstile_settings').style.display = selectedProvider === 'turnstile' ? 'block' : 'none';
        document.getElementById('hcaptcha_settings').style.display = selectedProvider === 'hcaptcha' ? 'block' : 'none';
    }

    toggleProviderSettings();
    document.getElementById('provider-select').addEventListener('change', toggleProviderSettings);
});
</script>
{% endblock %}
