server_tokens off;
charset utf-8;

# Backend (Django gunicorn) service inside the cluster.
upstream airshelf_api {
    server airshelf-core-api:8000;
}

# Preserve the original scheme: traefik terminates TLS and forwards
# X-Forwarded-Proto=https; fall back to our own scheme if it's absent.
map $http_x_forwarded_proto $fwd_proto {
    default $http_x_forwarded_proto;
    ""      $scheme;
}

server {
    listen 80;
    server_name _;
    root /usr/share/nginx/html;
    index index.html;

    client_max_body_size 50m;

    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    # ---- Backend pass-through (same origin, no CORS) ----
    location /api/ {
        proxy_pass http://airshelf_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 $fwd_proto;
        proxy_read_timeout 300s;
    }

    location /admin/ {
        proxy_pass http://airshelf_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 $fwd_proto;
    }

    # Django/WhiteNoise admin static assets
    location /static/ {
        proxy_pass http://airshelf_api;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $fwd_proto;
    }

    # ---- Frontend SPA: hashed assets cache long, index.html never ----
    location /assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    location = /index.html {
        add_header Cache-Control "no-cache, must-revalidate" always;
        expires off;
    }

    # SPA fallback: every unknown path serves index.html (client-side routing)
    location / {
        try_files $uri $uri/ /index.html;
    }
}
