#===============================================================================
# iConsulting Docker Nginx 配置
#
# 注意: 此 Nginx 运行在 Docker 容器内，监听 80 端口
#       系统 Nginx 负责 SSL 终止，然后反向代理到此处
#
# 路由规则:
#   /              -> web-client (用户前端)
#   /admin         -> admin-client (管理后台)
#   /api/v1/*      -> Kong API Gateway
#   /ws/*          -> WebSocket (conversation-service)
#   /storage/*     -> MinIO (文件存储)
#
#===============================================================================

server {
    listen 80;
    server_name _;

    # 健康检查端点
    location /health {
        access_log off;
        return 200 'OK';
        add_header Content-Type text/plain;
    }

    # 用户前端 (web-client)
    location / {
        root /usr/share/nginx/html/web;
        index index.html;
        try_files $uri $uri/ /index.html;

        # 缓存静态资源
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }
    }

    # 管理后台 (admin-client)
    location /admin {
        alias /usr/share/nginx/html/admin;
        index index.html;
        try_files $uri $uri/ /index.html;

        # 缓存静态资源
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }
    }

    # API 请求代理到 Kong
    location /api/ {
        # 使用变量实现动态解析，避免启动时服务不可用导致失败
        set $kong_upstream kong:8000;
        # 保留完整路径传递给 Kong (包括 /api/ 前缀)
        proxy_pass http://$kong_upstream;
        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;

        # 超时设置 (增加以支持 AI 流式响应)
        proxy_connect_timeout 120s;
        proxy_send_timeout 120s;
        proxy_read_timeout 120s;

        # 缓冲设置
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
    }

    # WebSocket/Socket.IO 代理 - 保留完整路径
    location /ws/ {
        set $ws_upstream conversation-service:3004;
        proxy_pass http://$ws_upstream;
        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_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
    }

    # MinIO 文件存储代理 (用于访问预签名 URL)
    location /storage/ {
        set $minio_upstream minio:9000;
        rewrite ^/storage/(.*)$ /$1 break;
        proxy_pass http://$minio_upstream;
        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_connect_timeout 300s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;

        # 允许大文件上传
        client_max_body_size 100M;

        # 缓冲设置
        proxy_buffering off;
        proxy_request_buffering off;
    }

    # 禁止访问隐藏文件
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}
