{% extends "base.html" %}

{% block title %}Rate Limiting{% endblock %}

{% block content %}
<div class="container mt-5 pt-4">
  <div class="row">
    <div class="col-12 mb-4">
      <h1>Configuración de Rate Limiting</h1>
      <p class="text-muted mb-0">Gestione límites por ruta de autenticación, whitelist de IPs y el modo estricto temporal.</p>
      <hr>
    </div>
  </div>

  {% with messages = get_flashed_messages(with_categories=true) %}
  {% if messages %}
  <div class="row">
    <div class="col-12">
      {% for category, message in messages %}
      <div class="alert alert-{{ 'warning' if category=='error' else category }}" role="alert">{{ message }}</div>
      {% endfor %}
    </div>
  </div>
  {% endif %}
  {% endwith %}

  <div class="row g-4">
    <div class="col-12 col-lg-7">
      <div class="card admin-module-card">
        <div class="card-header">
          <div class="card-icon"><i class="fas fa-tachometer-alt"></i></div>
          <h3>Límites de autenticación</h3>
        </div>
        <div class="card-body text-start">
          <form method="POST">
            {{ form.hidden_tag() }}

            <div class="mb-3">
              <label class="form-label">Límite Login</label>
              {{ form.login_limit(class="form-control", placeholder="10/minute; 50/hour") }}
            </div>

            <div class="mb-3">
              <label class="form-label">Límite Registro</label>
              {{ form.register_limit(class="form-control", placeholder="5/minute; 20/hour") }}
            </div>

            <div class="mb-3">
              <label class="form-label">Límite Solicitud de Reset</label>
              {{ form.reset_request_limit(class="form-control", placeholder="5/minute; 20/hour") }}
            </div>

            <div class="mb-3">
              <label class="form-label">Límite Reset</label>
              {{ form.reset_limit(class="form-control", placeholder="10/minute; 50/hour") }}
            </div>

            <div class="mb-3">
              <label class="form-label">Whitelist de IPs/CIDR (una por línea)</label>
              {{ form.whitelist(class="form-control", rows=6, placeholder="127.0.0.1\n10.0.0.0/24") }}
              <div class="form-text">Formatos válidos: IP exacta (p.ej. 1.2.3.4) o rango CIDR (p.ej. 10.0.0.0/24).</div>
            </div>

            {{ form.submit(class="btn btn-primary") }}
          </form>
        </div>
      </div>
    </div>

    <div class="col-12 col-lg-5">
      <div class="card admin-module-card h-100">
        <div class="card-header">
          <div class="card-icon"><i class="fas fa-shield-alt"></i></div>
          <h3>Modo Estricto</h3>
        </div>
        <div class="card-body text-start d-flex flex-column">
          {% if status and status.active %}
            <div class="alert alert-warning">Modo estricto ACTIVO{% if status.minutes_left is not none %} ({{ status.minutes_left }} min restantes){% endif %}.</div>
          {% else %}
            <div class="alert alert-success">Modo estricto inactivo.</div>
          {% endif %}

          <form method="POST" class="mb-3">
            {{ form.hidden_tag() }}
            <div class="mb-3">
              <label class="form-label">Minutos de modo estricto</label>
              {{ form.strict_minutes(class="form-control", placeholder="60") }}
              <div class="form-text">Mientras esté activo, se aplican límites más estrictos en las rutas de autenticación.</div>
            </div>
            {{ form.activate_strict(class="btn btn-danger") }}
          </form>

          <form method="POST" class="mt-auto">
            {{ form.hidden_tag() }}
            {{ form.deactivate_strict(class="btn btn-outline-secondary") }}
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
{% endblock %}
