server {
    listen 80;
    server_name _;

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

    # Proxy API requests to the FastAPI container via Docker bridge network.
    # In cloud, 'api' resolves to the api service container — no host networking needed.
    location /api/ {
        proxy_pass http://api:8510;
        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 $http_x_forwarded_proto;
        # Forward the session header injected by Caddy from the cf_session cookie.
        # Caddy adds: header_up X-CF-Session {http.request.cookie.cf_session}
        proxy_set_header X-CF-Session $http_x_cf_session;
        # eBay search + comps can take 60-90s (Marketplace Insights 404 → Browse fallback).
        # Default 60s proxy_read_timeout drops slow searches with a NetworkError on the client.
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
    }

    # index.html — never cache; ensures clients always get the latest entry point
    location = /index.html {
        add_header Cache-Control "no-cache, no-store, must-revalidate";
        try_files $uri /index.html;
    }

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

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