#!/bin/bash

# 🦟 蚊子项目 E2E测试启动脚本
# 同时启动前后端服务并运行E2E测试

set -e

echo "🚀 蚊子项目 E2E测试启动器"
echo ""

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查端口占用
check_port() {
    local port=$1
    if lsof -i :$port > /dev/null 2>&1; then
        echo -e "${RED}❌ 端口 $port 已被占用${NC}"
        return 1
    fi
    return 0
}

# 清理函数
cleanup() {
    echo ""
    echo -e "${YELLOW}🧹 清理进程中...${NC}"
    
    # 杀死后端进程
    if [ -n "$BACKEND_PID" ]; then
        kill $BACKEND_PID 2>/dev/null || true
        echo "   后端进程已停止"
    fi
    
    # 杀死前端进程
    if [ -n "$FRONTEND_PID" ]; then
        kill $FRONTEND_PID 2>/dev/null || true
        echo "   前端进程已停止"
    fi
    
    echo -e "${GREEN}✅ 清理完成${NC}"
}

# 注册清理函数
trap cleanup EXIT INT TERM

# 检查必要命令
command -v mvn >/dev/null 2>&1 || { echo -e "${RED}❌ 需要Maven (mvn)${NC}"; exit 1; }
command -v node >/dev/null 2>&1 || { echo -e "${RED}❌ 需要Node.js${NC}"; exit 1; }
command -v npm >/dev/null 2>&1 || { echo -e "${RED}❌ 需要npm${NC}"; exit 1; }

# 检查端口
echo "🔍 检查端口..."
check_port 8080 || exit 1
check_port 5173 || exit 1
echo -e "${GREEN}   ✅ 端口可用${NC}"
echo ""

# 获取项目根目录
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
FRONTEND_DIR="$PROJECT_ROOT/frontend"

echo "📂 项目目录: $PROJECT_ROOT"
echo ""

# 步骤1: 编译后端
echo "📦 步骤1: 编译后端..."
cd "$PROJECT_ROOT"
mvn clean compile -q -DskipTests
if [ $? -eq 0 ]; then
    echo -e "${GREEN}   ✅ 后端编译成功${NC}"
else
    echo -e "${RED}   ❌ 后端编译失败${NC}"
    exit 1
fi
echo ""

# 步骤2: 启动后端服务
echo "🚀 步骤2: 启动后端服务..."
cd "$PROJECT_ROOT"
mvn spring-boot:run -Dspring-boot.run.profiles=e2e -Dspring-boot.run.jvmArguments="-Xmx512m" > /tmp/mosquito-backend.log 2>&1 &
BACKEND_PID=$!
echo "   后端PID: $BACKEND_PID"
echo "   日志: /tmp/mosquito-backend.log"
echo ""

# 等待后端启动
echo "⏳ 等待后端服务就绪..."
for i in {1..60}; do
    if curl -s http://localhost:8080/api/v1/activities > /dev/null 2>&1; then
        echo -e "${GREEN}   ✅ 后端服务已就绪${NC}"
        break
    fi
    if [ $i -eq 60 ]; then
        echo -e "${RED}   ❌ 后端服务启动超时${NC}"
        exit 1
    fi
    echo -n "."
    sleep 2
done
echo ""

# 步骤3: 安装前端依赖
echo "📦 步骤3: 安装前端依赖..."
cd "$FRONTEND_DIR"
npm install --silent
echo -e "${GREEN}   ✅ 前端依赖已安装${NC}"
echo ""

# 步骤4: 启动前端服务
echo "🚀 步骤4: 启动前端服务..."
cd "$FRONTEND_DIR"
npm run dev -- --port 5173 > /tmp/mosquito-frontend.log 2>&1 &
FRONTEND_PID=$!
echo "   前端PID: $FRONTEND_PID"
echo "   日志: /tmp/mosquito-frontend.log"
echo ""

# 等待前端启动
echo "⏳ 等待前端服务就绪..."
for i in {1..30}; do
    if curl -s http://localhost:5173 > /dev/null 2>&1; then
        echo -e "${GREEN}   ✅ 前端服务已就绪${NC}"
        break
    fi
    if [ $i -eq 30 ]; then
        echo -e "${RED}   ❌ 前端服务启动超时${NC}"
        exit 1
    fi
    echo -n "."
    sleep 1
done
echo ""

# 步骤5: 运行E2E测试
echo "🎭 步骤5: 运行E2E测试..."
echo "   后端地址: http://localhost:8080"
echo "   前端地址: http://localhost:5173"
echo ""

cd "$FRONTEND_DIR"

# 设置环境变量
export API_BASE_URL=http://localhost:8080
export PLAYWRIGHT_BASE_URL=http://localhost:5173

# 运行Playwright测试
echo "   正在运行Playwright E2E测试..."
npx playwright test --reporter=list "$@"
TEST_EXIT_CODE=$?

echo ""
if [ $TEST_EXIT_CODE -eq 0 ]; then
    echo -e "${GREEN}✅ E2E测试通过！${NC}"
else
    echo -e "${RED}❌ E2E测试失败${NC}"
fi

echo ""
echo "📊 查看报告:"
echo "   Playwright报告: npx playwright show-report e2e-report"
echo "   后端日志: tail -100 /tmp/mosquito-backend.log"
echo "   前端日志: tail -100 /tmp/mosquito-frontend.log"

exit $TEST_EXIT_CODE
