server {
    listen 80;
    server_name _;

    root /usr/share/nginx/html;
    index index.html;

    # ---------- 前端静态资源 ----------
    location / {
        try_files $uri $uri/ /index.html;
        # HTML 文件不缓存
        add_header Cache-Control "no-store, no-cache, must-revalidate" always;
        add_header Pragma "no-cache" always;
    }

    # ---------- SSE 长连接专用（AI 复盘报告等）----------
    location /api/reports/generate {
        proxy_pass         http://backend:8000/api/reports/generate;
        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_set_header   Connection        '';

        # SSE 必须: 禁用缓冲
        proxy_buffering    off;
        proxy_cache        off;
        chunked_transfer_encoding on;

        # LLM 生成可能需要 5-10 分钟
        proxy_read_timeout    600s;
        proxy_send_timeout    600s;
    }

    # ---------- 后端 API 反向代理 ----------
    location /api/ {
        proxy_pass         http://backend:8000/api/;
        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 300s;
        proxy_send_timeout 120s;

        # 文件上传大小限制
        client_max_body_size 50m;
    }

    # ---------- 静态资源缓存（带 hash 的 JS/CSS 长缓存） ----------
    location /assets/ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
}
