worker_processes auto;
events {
    worker_connections 1024;
}

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

    sendfile        on;
    keepalive_timeout  65;
    client_max_body_size 100m;

    # Gzip compression
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml;
    gzip_min_length 1024;

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

    # Upstream servers
    upstream python_ai {
        server 127.0.0.1:8900;
    }

    upstream java_biz {
        server 127.0.0.1:8080;
    }

    server {
        listen 80;
        server_name localhost;

        # Vue frontend static files
        location / {
            root /opt/zm-rag/frontend/dist;
            index index.html;
            try_files $uri $uri/ /index.html;
        }

        # Python AI service
        location /api/ai/ {
            limit_req zone=api_limit burst=20 nodelay;
            proxy_pass http://python_ai/api/ai/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            # SSE support for research endpoint
            proxy_buffering off;
            proxy_cache off;
            proxy_read_timeout 300s;
        }

        # Java business service
        location /api/biz/ {
            limit_req zone=api_limit burst=20 nodelay;
            proxy_pass http://java_biz/api/biz/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        # PDF file access (proxied to Java service)
        location /files/ {
            proxy_pass http://java_biz/files/;
            proxy_set_header Host $host;
        }
    }
}
