# ============================================================================
# Cognithor · Nginx Reverse Proxy
# ============================================================================
# Serves WebUI + Control Center API behind TLS.
#
# Expects:
#   /etc/nginx/certs/fullchain.pem  — TLS certificate chain
#   /etc/nginx/certs/privkey.pem    — TLS private key
#
# Upstream services (Docker network):
#   webui:8080   — Web UI + WebSocket
#   jarvis:8741  — Control Center API
# ============================================================================

worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /tmp/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout 65;
    client_max_body_size 55m;

    # Logging
    log_format main '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';
    access_log /var/log/nginx/access.log main;

    # Upstream definitions
    upstream webui {
        server webui:8080;
    }

    upstream control {
        server jarvis:8741;
    }

    # ── HTTP → HTTPS Redirect ─────────────────────────────────
    server {
        listen 80;
        server_name _;

        # Health check (no auth, no TLS)
        location /health {
            proxy_pass http://webui/api/v1/health;
            proxy_set_header Host $host;
        }

        location / {
            return 301 https://$host$request_uri;
        }
    }

    # ── HTTPS ─────────────────────────────────────────────────
    server {
        listen 443 ssl;
        http2 on;
        server_name _;

        # TLS
        ssl_certificate     /etc/nginx/certs/fullchain.pem;
        ssl_certificate_key /etc/nginx/certs/privkey.pem;
        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;

        # Security headers
        add_header X-Frame-Options DENY 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 Strict-Transport-Security "max-age=63072000; includeSubDomains" always;

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

        # ── Web UI (default) ──────────────────────────────────
        location / {
            proxy_pass http://webui;
            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 ─────────────────────────────────────────
        location /ws/ {
            proxy_pass http://webui;
            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;
            proxy_read_timeout 3600s;
            proxy_send_timeout 3600s;
        }

        # ── Control Center API ────────────────────────────────
        location /control/ {
            rewrite ^/control/(.*) /$1 break;
            proxy_pass http://control;
            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;
        }

        # ── Health (no auth) ─────────────────────────────────
        location = /health {
            proxy_pass http://webui/api/v1/health;
            proxy_set_header Host $host;
        }
    }
}
