# Home Assistant — nginx reverse proxy config
# Domein: www.ha.el-kadi.nl → backend op localhost:8765
#
# Plaats dit bestand in /etc/nginx/sites-available/ en symlink naar sites-enabled/
# Of in /etc/nginx/conf.d/ (afhankelijk van je nginx setup)
#
# Zorg dat je SSL certificaten klaarliggen (bijv. via Let's Encrypt / certbot).

server {
    listen 80;
    server_name www.ha.el-kadi.nl ha.el-kadi.nl;

    # Redirect alle HTTP naar HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.ha.el-kadi.nl ha.el-kadi.nl;

    # ── SSL certificaten ─────────────────────────────────────────────────
    # Vervang deze paden met jouw certificaat-locatie
    ssl_certificate     /etc/letsencrypt/live/ha.el-kadi.nl/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ha.el-kadi.nl/privkey.pem;

    # ── SSL beveiliging ──────────────────────────────────────────────────
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;

    # ── security headers ─────────────────────────────────────────────────
    add_header Strict-Transport-Security "max-age=63072000" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;

    # ── logging ──────────────────────────────────────────────────────────
    access_log /var/log/nginx/ha-voice-access.log;
    error_log  /var/log/nginx/ha-voice-error.log;

    # ── proxy naar de FastAPI web server ─────────────────────────────────
    location / {
        proxy_pass http://127.0.0.1:8765;
        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 $scheme;

        # WebSocket ondersteuning (voor toekomstige live updates)
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # Timeouts voor lange audio uploads
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        client_max_body_size 10M;
    }

    # ── API specifiek ────────────────────────────────────────────────────
    location /api/ {
        proxy_pass http://127.0.0.1:8765;
        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 $scheme;

        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        client_max_body_size 10M;
    }
}
