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;

    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;
    client_max_body_size 50m;

    # Gzip compression
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # Rate limiting
    limit_req_zone $binary_remote_addr zone=api:10m rate=30r/s;
    limit_req_zone $binary_remote_addr zone=web:10m rate=50r/s;

    # Upstream definitions
    upstream api_gateway {
        server api-gateway:8000;
    }

    upstream web_admin {
        server web-admin:3000;
    }

    # =========================================================================
    # HTTP -> HTTPS redirect + ACME challenge
    # =========================================================================
    server {
        listen 80;
        server_name it0api.szaiai.com it0.szaiai.com;

        # Let's Encrypt ACME challenge
        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }

        # Redirect all other HTTP to HTTPS
        location / {
            return 301 https://$host$request_uri;
        }
    }

    # =========================================================================
    # it0api.szaiai.com — API Gateway (HTTPS)
    # =========================================================================
    server {
        listen 443 ssl;
        http2 on;
        server_name it0api.szaiai.com;

        ssl_certificate /etc/letsencrypt/live/it0api.szaiai.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/it0api.szaiai.com/privkey.pem;
        include /etc/nginx/ssl-params.conf;

        # API proxy
        location / {
            limit_req zone=api burst=60 nodelay;

            proxy_pass http://api_gateway;
            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;

            # WebSocket support
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 86400;
        }

        # Health check (no rate limit)
        location /status {
            proxy_pass http://api_gateway;
            proxy_set_header Host $host;
        }
    }

    # =========================================================================
    # it0.szaiai.com — Web Admin (HTTPS)
    # =========================================================================
    server {
        listen 443 ssl;
        http2 on;
        server_name it0.szaiai.com;

        ssl_certificate /etc/letsencrypt/live/it0.szaiai.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/it0.szaiai.com/privkey.pem;
        include /etc/nginx/ssl-params.conf;

        location / {
            limit_req zone=web burst=100 nodelay;

            proxy_pass http://web_admin;
            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;

            # Next.js HMR / WebSocket
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}
