#!/bin/bash

set -e

echo "=========================================="
echo " 配置一致性检查脚本"
echo " 检查Vite base配置与Nginx location配置是否一致"
echo "=========================================="

# 定义检查的应用列表
APPS=(
    "platform:/platform/"
    "merchant:/merchant/"
    "official:/official/"
    "h5:/h5/"
    "rider:/rider/"
)

NGINX_CONF="infrastructure/nginx/conf/aliyun-dev.conf"
VITE_CONFIG_DIR="frontend"

echo ""
echo "[步骤1] 检查Vite配置文件"
echo "------------------------"

ERROR_COUNT=0

for app in "${APPS[@]}"; do
    APP_NAME=$(echo "$app" | cut -d':' -f1)
    EXPECTED_BASE=$(echo "$app" | cut -d':' -f2)
    
    # 查找Vite配置文件
    VITE_CONFIG=$(find "$VITE_CONFIG_DIR" -name "vite.config.ts" -path "*/${APP_NAME}*" 2>/dev/null | head -1)
    
    if [ -f "$VITE_CONFIG" ]; then
        ACTUAL_BASE=$(grep -E "base:\s*['\"]" "$VITE_CONFIG" | sed "s/.*base:\s*['\"]\([^'\"]*\)['\"].*/\1/")
        
        if [ "$ACTUAL_BASE" == "$EXPECTED_BASE" ]; then
            echo "✅ $APP_NAME: Vite base配置正确 - '$ACTUAL_BASE'"
        else
            echo "❌ $APP_NAME: Vite base配置不一致"
            echo "   期望: '$EXPECTED_BASE'"
            echo "   实际: '$ACTUAL_BASE'"
            echo "   文件: $VITE_CONFIG"
            ERROR_COUNT=$((ERROR_COUNT + 1))
        fi
    else
        echo "⚠️ $APP_NAME: 未找到Vite配置文件"
    fi
done

echo ""
echo "[步骤2] 检查Nginx配置"
echo "----------------------"

for app in "${APPS[@]}"; do
    APP_NAME=$(echo "$app" | cut -d':' -f1)
    EXPECTED_BASE=$(echo "$app" | cut -d':' -f2)
    
    # 检查Nginx中是否存在对应的location配置
    if grep -q "location $EXPECTED_BASE" "$NGINX_CONF"; then
        echo "✅ $APP_NAME: Nginx location配置存在 - '$EXPECTED_BASE'"
    else
        echo "❌ $APP_NAME: Nginx location配置缺失 - '$EXPECTED_BASE'"
        ERROR_COUNT=$((ERROR_COUNT + 1))
    fi
    
    # 检查try_files配置
    if grep -A2 "location $EXPECTED_BASE" "$NGINX_CONF" | grep -q "try_files"; then
        echo "   ✅ try_files配置存在"
    else
        echo "   ❌ try_files配置缺失"
        ERROR_COUNT=$((ERROR_COUNT + 1))
    fi
done

echo ""
echo "[步骤3] 检查Nginx API代理配置"
echo "------------------------------"

# 检查proxy_pass是否正确（不应包含/api/后缀）
if grep -E "proxy_pass.*api-gateway.*api/" "$NGINX_CONF" | grep -v "#"; then
    echo "❌ 发现错误的proxy_pass配置（包含/api/后缀）"
    grep -n "proxy_pass.*api-gateway.*api/" "$NGINX_CONF"
    ERROR_COUNT=$((ERROR_COUNT + 1))
else
    echo "✅ proxy_pass配置正确（未包含/api/后缀）"
fi

# 检查超时配置
if grep -q "proxy_connect_timeout" "$NGINX_CONF"; then
    echo "✅ 超时配置已添加"
else
    echo "⚠️ 建议添加超时配置"
fi

echo ""
echo "[步骤4] 检查环境变量配置"
echo "------------------------"

# 检查生产环境配置
for env_file in $(find "$VITE_CONFIG_DIR" -name ".env.production"); do
    if grep -q "VITE_API_BASE_URL=http" "$env_file"; then
        echo "❌ $env_file: 生产环境配置了硬编码的URL"
        grep "VITE_API_BASE_URL" "$env_file"
        ERROR_COUNT=$((ERROR_COUNT + 1))
    elif grep -q "VITE_API_BASE_URL=/api" "$env_file"; then
        echo "✅ $env_file: 生产环境配置正确 - '/api'"
    else
        echo "⚠️ $env_file: 未设置VITE_API_BASE_URL（将使用默认值）"
    fi
done

echo ""
echo "=========================================="
echo " 检查完成"
echo " 错误数: $ERROR_COUNT"
echo "=========================================="

if [ $ERROR_COUNT -gt 0 ]; then
    exit 1
else
    echo "✅ 所有配置检查通过！"
    exit 0
fi