server {
    listen 443 ssl http2;
    server_name dak.complexcaresolutions.de;

    # SSL certificates (managed by Plesk/Let's Encrypt)
    ssl_certificate /etc/letsencrypt/live/dak.complexcaresolutions.de/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dak.complexcaresolutions.de/privkey.pem;

    # Frontend — serve static files from Vite build
    root /opt/dak-portal/frontend/dist;
    index index.html;

    # API proxy to FastAPI backend
    location /api/ {
        proxy_pass http://127.0.0.1:8000/api/;
        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;
        client_max_body_size 20M;
    }

    # FastAPI docs (optional, remove in production)
    location /docs {
        proxy_pass http://127.0.0.1:8000/docs;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /openapi.json {
        proxy_pass http://127.0.0.1:8000/openapi.json;
    }

    # SPA fallback — all other routes serve index.html
    location / {
        try_files $uri $uri/ /index.html;
    }

    # Security headers
    add_header X-Content-Type-Options nosniff always;
    add_header X-Frame-Options DENY always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy strict-origin-when-cross-origin always;
}

server {
    listen 80;
    server_name dak.complexcaresolutions.de;
    return 301 https://$host$request_uri;
}
