# 开发环境统一入口配置

# 上游服务定义
upstream gateway {
    server gateway:8080;
    keepalive 32;
}

upstream portal {
    server portal:3000;
    keepalive 16;
}

upstream app-bidding {
    server app-bidding:3001;
    keepalive 16;
}

upstream app-customer-service {
    server app-customer-service:3002;
    keepalive 16;
}

upstream shared {
    server shared:5000;
    keepalive 16;
}

# WebSocket 升级配置
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

# 主服务器配置
server {
    listen 80;
    server_name localhost;

    # 安全头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # 开发环境允许所有跨域
    add_header Access-Control-Allow-Origin "*" always;
    add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
    add_header Access-Control-Allow-Headers "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization" always;

    # 健康检查
    location /health {
        access_log off;
        return 200 "healthy\n";
        add_header Content-Type text/plain;
    }

    # ==================== 共享包服务（ES Module + Module Federation） ====================
    location /shared/ {
        proxy_pass http://shared:5000/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $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;
        
        # CORS（ES Module / Module Federation 必需）
        add_header Access-Control-Allow-Origin "*" always;
        add_header Access-Control-Allow-Methods "GET, OPTIONS" always;
        add_header Access-Control-Allow-Headers "Content-Type" always;
        
        # ES Module MIME 类型
        if ($request_filename ~* \.(js|mjs)$) {
            add_header Content-Type "application/javascript; charset=utf-8";
        }
        
        # 开发环境不缓存
        add_header Cache-Control "no-cache, no-store, must-revalidate";
        
        # HMR 支持
        proxy_read_timeout 86400;
        proxy_send_timeout 86400;
    }

    # ==================== API 网关代理 ====================
    location /api/ {
        proxy_pass http://gateway/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $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;
        
        # 超时设置
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    # WebSocket 代理
    location /ws/ {
        proxy_pass http://gateway/ws/;
        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;
        
        # WebSocket 超时设置
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }

    # ==================== 主应用（Portal） ====================
    location / {
        proxy_pass http://portal/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        
        # Vite HMR 支持
        proxy_read_timeout 86400;
    }

    # ==================== 招投标应用 ====================
    location /bidding {
        rewrite ^/bidding/(.*)$ /$1 break;
        proxy_pass http://app-bidding/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        
        # Vite HMR 支持
        proxy_read_timeout 86400;
    }

    # 招投标应用静态资源
    location /bidding/ {
        proxy_pass http://app-bidding/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        
        proxy_read_timeout 86400;
    }

    # ==================== 智能客服应用 ====================
    location /customer-service {
        rewrite ^/customer-service/(.*)$ /$1 break;
        proxy_pass http://app-customer-service/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        
        proxy_read_timeout 86400;
    }

    location /customer-service/ {
        proxy_pass http://app-customer-service/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        
        proxy_read_timeout 86400;
    }

    # ==================== Nacos 管理界面（开发用） ====================
    location /nacos/ {
        proxy_pass http://nacos:8848/nacos/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

# 可选：HTTPS 配置（开发环境使用自签名证书）
# server {
#     listen 443 ssl http2;
#     server_name localhost;
#
#     ssl_certificate /etc/nginx/ssl/cert.pem;
#     ssl_certificate_key /etc/nginx/ssl/key.pem;
#     ssl_protocols TLSv1.2 TLSv1.3;
#     ssl_ciphers HIGH:!aNULL:!MD5;
#
#     # 其他配置同上...
# }
