# =============================================================================
# TipSharks ELO API - Nginx Reverse Proxy Configuration
# =============================================================================
# Proxies requests to the FastAPI application running on api:8000.
# Serves static files directly for performance.
# Supports WebSocket upgrade for /ws/ endpoints.
# =============================================================================

upstream tipsharks_api {
    server api:8000;
    # Add additional upstream servers when scaling horizontally:
    # server api:8001;
    # server api:8002;
    keepalive 64;
}

# ── HTTP Server (primary) ─────────────────────────────────────────────────
server {
    listen 80 default_server;
    server_name _;

    # -----------------------------------------------------------------------
    # Gzip compression
    # -----------------------------------------------------------------------
    gzip on;
    gzip_comp_level 5;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    gzip_types
        application/javascript
        application/json
        application/xml
        application/x-javascript
        image/svg+xml
        text/css
        text/javascript
        text/plain
        text/xml;

    # -----------------------------------------------------------------------
    # Security headers
    # -----------------------------------------------------------------------
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), interest-cohort=()" always;
    # Uncomment once HTTPS is configured:
    # add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://cdn.jsdelivr.net; style-src 'self' https://cdn.jsdelivr.net 'unsafe-inline'; img-src 'self' data:; font-src 'self' https://cdn.jsdelivr.net; connect-src 'self'; frame-ancestors 'none'" always;

    # Hide nginx version
    server_tokens off;

    # Set max body size for admin/webhook payloads
    client_max_body_size 10M;
    client_body_buffer_size 128k;

    # Timeouts
    proxy_connect_timeout 60s;
    proxy_send_timeout    60s;
    proxy_read_timeout    60s;

    # -----------------------------------------------------------------------
    # Health check endpoint — proxied directly to the API
    # -----------------------------------------------------------------------
    location /health {
        access_log off;
        proxy_pass http://tipsharks_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 $scheme;
    }

    # -----------------------------------------------------------------------
    # Static files — served directly by nginx (no proxy overhead)
    # -----------------------------------------------------------------------
    location /static/ {
        alias /app/static/;
        expires 7d;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # -----------------------------------------------------------------------
    # WebSocket support — upgrade connection for /ws/ endpoints
    # -----------------------------------------------------------------------
    location /ws/ {
        proxy_pass http://tipsharks_api;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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 $scheme;

        # WebSocket proxy timeouts (longer for persistent connections)
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }

    # -----------------------------------------------------------------------
    # Main reverse proxy — all other requests go to the FastAPI app
    # -----------------------------------------------------------------------
    location / {
        proxy_pass http://tipsharks_api;
        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 $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;

        # Disable buffering for streaming responses
        proxy_buffering off;
        proxy_request_buffering off;

        # Pass original client IP for rate limiting
        proxy_set_header X-Original-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

# ── HTTPS Server (uncomment when TLS certificates are available) ──────────
# server {
#     listen 443 ssl http2 default_server;
#     server_name api.tipsharks.com;
#
#     ssl_certificate     /etc/ssl/certs/tipsharks.crt;
#     ssl_certificate_key /etc/ssl/private/tipsharks.key;
#     ssl_protocols       TLSv1.2 TLSv1.3;
#     ssl_ciphers         HIGH:!aNULL:!MD5;
#     ssl_prefer_server_ciphers on;
#     ssl_session_cache   shared:SSL:10m;
#     ssl_session_timeout 10m;
#
#     # HSTS (uncomment only after verifying HTTPS works)
#     # add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
#
#     # Same locations as the HTTP server above
#     location /health { ... }
#     location /static/ { ... }
#     location /ws/ { ... }
#     location / { ... }
# }
#
# # Redirect HTTP to HTTPS
# server {
#     listen 80;
#     server_name api.tipsharks.com;
#     return 301 https://$host$request_uri;
# }
