"""Production : tout vient de l'environnement, HTTPS obligatoire derrière Apache."""

from .base import *  # noqa: F403
from .env import config
from .messaging import TURNSTILE_SECRET_KEY, TURNSTILE_SITE_KEY

if SECRET_KEY in ("", "change-me") or SECRET_KEY.startswith("django-insecure"):  # noqa: F405
    raise RuntimeError("DJANGO_SECRET_KEY doit être une vraie clé secrète en production.")

# Sans ces variables, Django retomberait sur SQLite, un cache par worker ou aucun email.
for _name in ("DATABASE_URL", "REDIS_URL", "EMAIL_HOST"):
    if not config(_name, default=""):
        raise RuntimeError(f"{_name} doit être défini en production.")

# Les clés de test officielles commencent par 1x, 2x ou 3x : elles valident n'importe quel envoi.
for _key in (TURNSTILE_SITE_KEY, TURNSTILE_SECRET_KEY):
    if not _key or _key.startswith(("1x", "2x", "3x")):
        raise RuntimeError("Il faut les vraies clés Cloudflare Turnstile en production.")

API_DOCS_ENABLED = config("API_DOCS_ENABLED", default=False, cast=bool)

# Nombre de proxys de confiance : seule l'adresse ajoutée par Apache identifie le client.
_num_proxies = config("DJANGO_NUM_PROXIES", default=1, cast=int)
REST_FRAMEWORK = {**REST_FRAMEWORK, "NUM_PROXIES": _num_proxies}  # noqa: F405

SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
SECURE_SSL_REDIRECT = config("DJANGO_SECURE_SSL_REDIRECT", default=True, cast=bool)
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_REFERRER_POLICY = "strict-origin-when-cross-origin"
X_FRAME_OPTIONS = "DENY"
# HSTS est posé par Apache ; on peut aussi l'activer ici une fois HTTPS stable.
SECURE_HSTS_SECONDS = config("DJANGO_HSTS_SECONDS", default=0, cast=int)

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {"console": {"class": "logging.StreamHandler"}},
    "root": {"handlers": ["console"], "level": "INFO"},
}
