server {
    listen 80;
    server_name _;

    root /usr/share/nginx/html;
    index index.html;

    # Proxy all /api/ calls to the API container.
    # The API controller routes include the /api prefix, so the path is
    # forwarded unchanged: /api/v1/organisations → http://api:8080/api/v1/organisations
    location /api/ {
        resolver           127.0.0.11 valid=10s ipv6=off;
        set $api_upstream  http://api:8080;
        proxy_pass         $api_upstream;
        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 30s;
    }

    # SPA fallback — all other paths serve index.html so Vue Router handles them
    location / {
        try_files $uri $uri/ /index.html;
    }

    # Security headers
    add_header X-Frame-Options       "SAMEORIGIN"   always;
    add_header X-Content-Type-Options "nosniff"     always;
    add_header Referrer-Policy       "strict-origin" always;

    # Cache static assets aggressively (webpack hashes file names)
    location ~* \.(js|css|woff2?|ttf|eot|svg|png|jpg|ico)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    # Never cache the SPA entry point
    location = /index.html {
        add_header Cache-Control "no-store, no-cache, must-revalidate";
    }
}
