#!/bin/bash
# 完整的E2E/UAT测试启动脚本

set -e

echo "================================================"
echo "🔧 E2E/UAT 测试完整启动脚本"
echo "================================================"

# 解析参数
ENV=${1:-dev}
DATABASE=${2:-h2}
BACKEND_URL=${3:-http://localhost:8084}
FRONTEND_URL=${4:-http://localhost:3000}

echo "📋 配置参数:"
echo "   环境: $ENV"
echo "   数据库: $DATABASE"
echo "   后端地址: $BACKEND_URL"
echo "   前端地址: $FRONTEND_URL"
echo ""

# 步骤1: 检查依赖
echo "📦 步骤1: 检查依赖..."

if ! command -v python3 &> /dev/null; then
    echo "❌ 未找到Python3，请安装Python 3.10+"
    exit 1
fi

python3 --version

if ! command -v mvn &> /dev/null; then
    echo "❌ 未找到Maven，请安装Maven"
    exit 1
fi

mvn -version

echo "✅ 依赖检查通过"
echo ""

# 步骤2: 启动后端服务
echo "🚀 步骤2: 启动后端服务..."

cd "$(dirname "$0")"

# 检查后端是否已启动
if curl -s "$BACKEND_URL/actuator/health" | grep -q '"status":"UP"'; then
    echo "✅ 后端服务已在运行: $BACKEND_URL"
else
    echo "⚙️  启动后端服务（后台运行）..."
    nohup mvn spring-boot:run \
        -pl ../novalon-manage-api/manage-app \
        -Dspring-boot.run.profiles=$ENV \
        -Dspring.r2dbc.url="r2dbc:h2:mem:///testdb" \
        -Dspring.datasource.url="jdbc:h2:mem:testdb" \
        -Dflyway.enabled=false \
        > /tmp/backend.log 2>&1 &
    
    BACKEND_PID=$!
    echo "   后端服务PID: $BACKEND_PID"
    
    echo "⏳ 等待后端服务启动..."
    for i in {1..30}; do
        if curl -s "$BACKEND_URL/actuator/health" | grep -q '"status":"UP"'; then
            echo "✅ 后端服务启动成功"
            break
        fi
        sleep 2
    done
fi
echo ""

# 步骤3: 启动前端服务
echo "🚀 步骤3: 启动前端服务..."

cd novalon-manage-web

if curl -s "$FRONTEND_URL" | grep -q "Novalon"; then
    echo "✅ 前端服务已在运行: $FRONTEND_URL"
else
    echo "⚙️  启动前端服务（后台运行）..."
    nohup npm run dev > /tmp/frontend.log 2>&1 &
    
    FRONTEND_PID=$!
    echo "   前端服务PID: $FRONTEND_PID"
    
    echo "⏳ 等待前端服务启动..."
    sleep 10
    
    if curl -s "$FRONTEND_URL" | grep -q "Novalon"; then
        echo "✅ 前端服务启动成功"
    else
        echo "⚠️  前端服务启动可能失败，请检查日志: /tmp/frontend.log"
    fi
fi
echo ""

# 步骤4: 运行测试
echo "🧪 步骤4: 运行测试..."

cd ../test-suite

# 安装测试依赖
echo "📦 安装测试依赖..."
pip install -r requirements.txt

# 设置环境变量
export BASE_URL=$BACKEND_URL
export FRONTEND_URL=$FRONTEND_URL
export ENV=$ENV
export DATABASE=$DATABASE

# 运行测试
echo "🚀 运行E2E/UAT测试..."
python3 run_tests.py \
    --env $ENV \
    --database $DATABASE \
    --backend-url $BACKEND_URL \
    --frontend-url $FRONTEND_URL \
    --test-dir tests \
    --parallel \
    --reruns 2 \
    --html-report reports/report.html \
    --junit-report reports/junit.xml \
    --coverage

TEST_RESULT=$?

echo ""

# 步骤5: 输出报告
echo "================================================"
echo "📊 测试报告"
echo "================================================"

if [ $TEST_RESULT -eq 0 ]; then
    echo "✅ 测试全部通过!"
else
    echo "❌ 测试失败，请检查报告"
fi

echo ""
echo "📄 报告文件:"
echo "   HTML: file://$(pwd)/reports/report.html"
echo "   JUnit: $(pwd)/reports/junit.xml"
echo "   Coverage: file://$(pwd)/reports/coverage/index.html"
echo ""

# 步骤6: 清理
echo "🧹 步骤6: 清理..."

echo "✅ 清理完成"
echo ""
echo "================================================"
echo "🔧 E2E/UAT 测试完成"
echo "================================================"

exit $TEST_RESULT
