# nginx — testing.crm.aduanasoft.com
# CRM Agente de Carga (entorno de PRUEBAS). App (SvelteKit adapter-node :5173) + API (FastAPI :8000)
# servidas en el MISMO origen para evitar CORS. TLS con Let's Encrypt (certbot).
#
# Instalar en el servidor:
#   sudo cp testing.crm.aduanasoft.com.conf /etc/nginx/sites-available/
#   sudo ln -s /etc/nginx/sites-available/testing.crm.aduanasoft.com.conf /etc/nginx/sites-enabled/
#   sudo certbot certonly --webroot -w /var/www/certbot -d testing.crm.aduanasoft.com
#   sudo nginx -t && sudo systemctl reload nginx
#
# IMPORTANTE (seguridad): publica los puertos de la app SOLO en loopback del servidor
# (127.0.0.1:5173 y 127.0.0.1:8000) para que nginx sea el único acceso público.

# ---- HTTP: reto ACME + redirección a HTTPS ----
server {
    listen 80;
    listen [::]:80;
    server_name testing.crm.aduanasoft.com;

    # Renovación de certificados (webroot)
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

# ---- HTTPS ----
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name testing.crm.aduanasoft.com;

    ssl_certificate     /etc/letsencrypt/live/testing.crm.aduanasoft.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/testing.crm.aduanasoft.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 1d;

    # Encabezados de seguridad
    add_header Strict-Transport-Security "max-age=63072000" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    # Subida de documentos (máx. 25 MB en la app) + margen
    client_max_body_size 30m;

    # Buffers grandes para headers de respuesta: /auth/sso setea el JWT (fragmentado
    # si supera ~4KB) + refresh/id_token/tenant como cookies → el header excede el
    # buffer default de nginx (evita "upstream sent too big header" → 502).
    proxy_buffer_size 32k;
    proxy_buffers 16 32k;
    proxy_busy_buffers_size 64k;

    gzip on;
    gzip_types text/plain text/css application/javascript application/json image/svg+xml;
    gzip_min_length 1024;

    # ---- API (FastAPI) — mismo origen: /api/... -> backend :8000 (conserva el path) ----
    location /api/ {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_read_timeout 120s;
    }

    # ---- App (SvelteKit adapter-node) — todo lo demás -> frontend :5173 ----
    location / {
        proxy_pass http://127.0.0.1:5173;
        proxy_http_version 1.1;
        # WebSocket / upgrade (SSR streaming y HMR si corriera en modo dev)
        proxy_set_header Upgrade    $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_read_timeout 120s;
    }
}
