# Vida Chart — in-container web tier.
# Serves static files from /app/public and proxies dynamic routes to the Node
# `app` container over the internal Docker network.

# Preserve the original request scheme across the cPanel proxy chain
# (EA-Nginx → Apache → this nginx → Node) so Express still sets Secure cookies.
map $http_x_forwarded_proto $vc_proto {
    default $scheme;
    https   https;
    http    http;
}

upstream vc_app {
    server app:3000;
    keepalive 32;
}

server {
    listen 80;
    listen [::]:80;
    server_name _;

    root  /app/public;
    index index.html;

    # ── gzip ──────────────────────────────────────────────────────────────
    gzip on;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/json application/javascript
               text/javascript image/svg+xml application/manifest+json text/event-stream;

    # ── shared proxy config ───────────────────────────────────────────────
    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 $vc_proto;

    # Consistent security headers (drop any upstream copy, set once)
    proxy_hide_header X-Frame-Options;
    proxy_hide_header X-Content-Type-Options;
    add_header X-Frame-Options        SAMEORIGIN always;
    add_header X-Content-Type-Options nosniff    always;

    client_max_body_size 12m;   # allow service-account JSON uploads via /api/*

    # ── tracker.js — static, public, cacheable, embeddable cross-origin ───
    location = /tracker.js {
        add_header X-Frame-Options        SAMEORIGIN always;
        add_header X-Content-Type-Options nosniff    always;
        add_header Cache-Control "public, max-age=3600, stale-while-revalidate=86400";
        add_header Access-Control-Allow-Origin "*";
        try_files $uri =404;
    }

    # ── SSE realtime stream — never buffer ────────────────────────────────
    location /api/stream/ {
        proxy_pass http://vc_app;
        proxy_set_header Connection '';
        proxy_buffering off;
        proxy_cache off;
        gzip off;
        proxy_read_timeout 3600s;
        chunked_transfer_encoding on;
    }

    # ── beacon collector — fast, unbuffered ───────────────────────────────
    location = /api/collect {
        proxy_pass http://vc_app;
        proxy_buffering off;
    }

    # ── dynamic app routes ────────────────────────────────────────────────
    location /api/        { proxy_pass http://vc_app; }
    location = /health    { proxy_pass http://vc_app; }
    location = /big-board { proxy_pass http://vc_app; }
    location = /ga4-setup { proxy_pass http://vc_app; }

    # ── long-cache static assets, fall back to the app if absent ──────────
    location ~* \.(?:css|js|mjs|png|jpg|jpeg|gif|ico|svg|woff2?)$ {
        expires 7d;
        access_log off;
        try_files $uri @app;
    }

    # ── everything else: static first, then the app ──────────────────────
    location / {
        try_files $uri $uri/ @app;
    }

    location @app {
        proxy_pass http://vc_app;
    }
}
