# nginx config TEMPLATE for the preview container (flotilla SPEC §15).
# `${PORT}` is substituted by deploy/preview/entrypoint.sh (envsubst) with the
# port Cloud Run injects. Mirrors the prod vhost
# (deploy/nginx/ohm.wiggleverse.org.conf) minus TLS (Cloud Run terminates TLS)
# and minus the prod security headers tuned for the public host.

worker_processes 1;
pid /run/nginx.pid;
error_log /dev/stderr warn;

events { worker_connections 1024; }

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    access_log    /dev/stdout;
    sendfile      on;
    server_tokens off;

    server {
        listen ${PORT};
        listen [::]:${PORT};
        server_name _;

        root /opt/rfc-app/frontend/dist;
        index index.html;

        # API + auth proxy to the single-process uvicorn. SSE chat streams need
        # buffering off so chunks reach the browser immediately.
        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 $scheme;
            proxy_buffering off;
            proxy_cache off;
            proxy_read_timeout 1h;
        }

        location /auth/ {
            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 $scheme;
        }

        # SPA fallback so React Router can take over.
        location / {
            try_files $uri $uri/ /index.html;
        }

        location ~* \.(js|css|woff2?|ttf|otf|eot|png|jpg|jpeg|gif|svg|ico)$ {
            try_files $uri =404;
            expires 1y;
            add_header Cache-Control "public, immutable";
        }

        client_max_body_size 4M;
    }
}
