worker_processes 1;

events {
    worker_connections 1024;
}

http {
    client_max_body_size 10M;

    server {
        listen 8080;

        error_page 502 /502.html;
        location = /502.html {
            root /usr/share/nginx/html;
            internal;
        }

        # Backend (in dev_stage the server listens on port 3000).
        location /api-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;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://127.0.0.1:3000/api-docs/;
        }

        location /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;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length, Content-Range';
            proxy_pass http://127.0.0.1:3000/api/;
        }

        # Frontend (Vite) on port 3001 — serves the SPA (dev or `vite preview`).
        location / {
            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_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_pass http://127.0.0.1:3001/;
        }
    }
}
