server {
    listen 80;
    server_name _;

    client_max_body_size 20m;

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

    # Proxy API calls to the FastAPI backend service
    location /api/ {
        proxy_pass http://api:8601;
        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_read_timeout 120s;
    }

    # Cache static assets
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # Handle /peregrine/ base path — used when accessed directly (no Caddy prefix stripping).
    # ^~ blocks regex location matches so assets at /peregrine/assets/... are served correctly.
    location ^~ /peregrine/assets/ {
        alias /usr/share/nginx/html/assets/;
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    location /peregrine/ {
        alias /usr/share/nginx/html/;
        try_files $uri $uri/ /index.html;
    }

    # SPA fallback — must come after API and assets
    location / {
        try_files $uri $uri/ /index.html;
    }
}
