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

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Use Docker DNS resolver, IPv4 only to avoid IPv6 connection issues
    resolver 127.0.0.11 ipv6=off valid=10s;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    keepalive_timeout 65;

    # Map for WebSocket upgrade - use empty string for non-WebSocket to enable keepalive
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' '';
    }

    upstream gunicorn_asgi {
        server gunicorn-asgi:8000 max_fails=0;
        keepalive 32;
    }

    upstream gunicorn_asgi_ssl {
        server gunicorn-asgi-ssl:8443 max_fails=0;
        keepalive 32;
    }

    # HTTP server (port 8080)
    server {
        listen 8080;
        server_name localhost;

        # Increase body size limit for large request tests
        client_max_body_size 100m;

        # Health check endpoint
        location /health {
            return 200 'OK';
            add_header Content-Type text/plain;
        }

        # WebSocket locations
        location /ws/ {
            proxy_pass http://gunicorn_asgi;
            proxy_http_version 1.1;

            # WebSocket upgrade headers
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

            # Standard proxy headers
            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 timeouts
            proxy_connect_timeout 60s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;
        }

        # Streaming locations - disable buffering
        location /stream/ {
            proxy_pass http://gunicorn_asgi;
            proxy_http_version 1.1;

            # Disable buffering for streaming
            proxy_buffering off;
            proxy_cache off;

            # SSE specific
            proxy_set_header Connection '';
            chunked_transfer_encoding on;

            # Headers
            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-Accel-Buffering no;

            # Longer timeouts for streaming
            proxy_connect_timeout 60s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;
        }

        # Default location
        location / {
            proxy_pass http://gunicorn_asgi;
            proxy_http_version 1.1;

            # Retry on connection errors
            proxy_next_upstream error timeout http_502;
            proxy_next_upstream_tries 2;

            # Headers
            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;

            # Support WebSocket upgrade if requested
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

            # Buffering settings
            proxy_buffering on;
            proxy_buffer_size 4k;
            proxy_buffers 8 4k;

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

    # HTTPS server (port 8444)
    server {
        listen 8444 ssl;
        http2 on;
        server_name localhost;

        ssl_certificate /certs/server.crt;
        ssl_certificate_key /certs/server.key;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # HTTP/2 settings
        http2_max_concurrent_streams 128;

        # Increase body size limit
        client_max_body_size 100m;

        # Health check endpoint
        location /health {
            return 200 'OK';
            add_header Content-Type text/plain;
        }

        # WebSocket locations (over HTTPS)
        location /ws/ {
            proxy_pass http://gunicorn_asgi;
            proxy_http_version 1.1;

            # WebSocket upgrade headers
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

            # Standard proxy headers
            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 timeouts
            proxy_connect_timeout 60s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;
        }

        # Streaming locations - disable buffering
        location /stream/ {
            proxy_pass http://gunicorn_asgi;
            proxy_http_version 1.1;

            # Disable buffering for streaming
            proxy_buffering off;
            proxy_cache off;

            # SSE specific
            proxy_set_header Connection '';
            chunked_transfer_encoding on;

            # Headers
            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-Accel-Buffering no;

            # Longer timeouts for streaming
            proxy_connect_timeout 60s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;
        }

        # Default location
        location / {
            proxy_pass http://gunicorn_asgi;
            proxy_http_version 1.1;

            # Retry on connection errors
            proxy_next_upstream error timeout http_502;
            proxy_next_upstream_tries 2;

            # Headers
            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;

            # Support WebSocket upgrade if requested
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

            # Buffering settings
            proxy_buffering on;
            proxy_buffer_size 4k;
            proxy_buffers 8 4k;

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