# Site nginx config (DOCKER_SPEC §1, ARCHITECTURE §6). Serves the static Astro build
# with security headers, immutable asset caching, revalidated HTML, canonical
# trailing slashes, the preserved /Linkedin redirect, and legacy-WP 410s.

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

    charset utf-8;
    sendfile on;
    tcp_nopush on;

    # --- Security headers (applied to document responses) ---
    add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self'; form-action 'self'; base-uri 'self'; frame-ancestors 'none'; object-src 'none'; upgrade-insecure-requests" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "geolocation=(), microphone=(), camera=(), interest-cohort=()" always;
    add_header X-Frame-Options "DENY" always;
    add_header Cache-Control "no-cache" always;

    # --- Compression ---
    gzip on;
    gzip_vary on;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_types text/plain text/css application/javascript application/json image/svg+xml application/xml application/xml+rss;

    # --- Preserved LinkedIn redirect (printed on the CVs). ---
    location = /Linkedin {
        return 301 https://www.linkedin.com/in/connor-babbington;
    }

    # --- Contact relay (same-origin; single public port) ---
    location = /api/contact {
        proxy_pass http://relay:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # --- Legacy WordPress URLs -> 410 Gone (crawler cleanup) ---
    location ~* ^/(wp-admin|wp-login|wp-content|wp-includes|wp-json|xmlrpc\.php|feed|comments/feed) {
        return 410;
    }

    # --- Immutable, content-hashed build assets ---
    location /_astro/ {
        expires 1y;
        add_header Cache-Control "public, immutable" always;
    }

    # --- Other static media (moderate cache) ---
    location ~* \.(?:woff2?|ttf|png|jpe?g|webp|avif|svg|ico)$ {
        expires 30d;
        add_header Cache-Control "public" always;
    }
    location = /theme-init.js {
        expires 1h;
        add_header Cache-Control "public" always;
    }

    # --- CVs update in place -> short cache so shared links fetch the newest ---
    location /cv/ {
        expires 1h;
        add_header Cache-Control "public, must-revalidate" always;
    }

    # --- HTML documents: canonical trailing slash + revalidate. Kept inside the
    #     catch-all so exact routes (e.g. /api/contact) are never slash-redirected. ---
    location / {
        rewrite ^([^.]*[^/])$ $1/ permanent;
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
}
