#!/bin/bash

set -e

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

show_usage() {
    echo "========================================="
    echo "Novalon 管理系统 - 统一测试脚本"
    echo "========================================="
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  all       运行所有测试（前端+后端+E2E）"
    echo "  unit      运行单元测试（前端+后端）"
    echo "  e2e       运行E2E测试"
    echo "  api       运行API集成测试"
    echo "  perf      运行性能测试"
    echo "  coverage  生成测试覆盖率报告"
    echo "  help      显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 all      # 运行所有测试"
    echo "  $0 unit     # 仅运行单元测试"
    echo "  $0 e2e      # 仅运行E2E测试"
    echo ""
}

run_frontend_unit_tests() {
    echo -e "${YELLOW}[前端] 运行单元测试...${NC}"
    cd novalon-manage-web
    if npm run test -- src/test; then
        echo -e "${GREEN}✓ 前端单元测试通过${NC}"
        FRONTEND_UNIT_STATUS="PASS"
    else
        echo -e "${RED}✗ 前端单元测试失败${NC}"
        FRONTEND_UNIT_STATUS="FAIL"
    fi
    cd ..
}

run_backend_unit_tests() {
    echo -e "${YELLOW}[后端] 运行单元测试...${NC}"
    cd novalon-manage-api/manage-sys
    if mvn test -Dtest="*ServiceTest,*HandlerTest" -q; then
        echo -e "${GREEN}✓ 后端单元测试通过${NC}"
        BACKEND_UNIT_STATUS="PASS"
    else
        echo -e "${RED}✗ 后端单元测试失败${NC}"
        BACKEND_UNIT_STATUS="FAIL"
    fi
    cd ../..
}

run_e2e_tests() {
    echo -e "${YELLOW}[E2E] 运行端到端测试...${NC}"
    cd novalon-manage-web
    if npx playwright test; then
        echo -e "${GREEN}✓ E2E测试通过${NC}"
        E2E_STATUS="PASS"
    else
        echo -e "${RED}✗ E2E测试失败${NC}"
        E2E_STATUS="FAIL"
    fi
    cd ..
}

run_api_tests() {
    echo -e "${YELLOW}[API] 运行API集成测试...${NC}"
    cd test-suite
    if python -m pytest tests/integration tests/security -v --tb=short; then
        echo -e "${GREEN}✓ API集成测试通过${NC}"
        API_STATUS="PASS"
    else
        echo -e "${RED}✗ API集成测试失败${NC}"
        API_STATUS="FAIL"
    fi
    cd ..
}

run_performance_tests() {
    echo -e "${YELLOW}[性能] 运行性能测试...${NC}"
    cd test-suite
    if python -m pytest tests/performance -v --tb=short; then
        echo -e "${GREEN}✓ 性能测试通过${NC}"
        PERF_STATUS="PASS"
    else
        echo -e "${RED}✗ 性能测试失败${NC}"
        PERF_STATUS="FAIL"
    fi
    cd ..
}

generate_coverage_report() {
    echo -e "${YELLOW}[覆盖率] 生成测试覆盖率报告...${NC}"
    
    echo "生成前端覆盖率报告..."
    cd novalon-manage-web
    npm run test:coverage -- src/test
    cd ..
    
    echo "生成后端覆盖率报告..."
    cd novalon-manage-api/manage-sys
    mvn jacoco:report -q
    cd ../..
    
    echo -e "${GREEN}✓ 覆盖率报告生成完成${NC}"
    echo "  - 前端: novalon-manage-web/coverage/"
    echo "  - 后端: novalon-manage-api/manage-sys/target/site/jacoco/index.html"
}

run_all_tests() {
    START_TIME=$(date +%s)
    
    run_frontend_unit_tests
    echo ""
    run_backend_unit_tests
    echo ""
    run_api_tests
    echo ""
    run_e2e_tests
    echo ""
    generate_coverage_report
    
    END_TIME=$(date +%s)
    DURATION=$((END_TIME - START_TIME))
    
    echo ""
    echo "========================================="
    echo "测试执行汇总报告"
    echo "========================================="
    echo "执行时间: ${DURATION}秒"
    echo ""
    echo "前端单元测试: ${FRONTEND_UNIT_STATUS:-SKIP}"
    echo "后端单元测试: ${BACKEND_UNIT_STATUS:-SKIP}"
    echo "API集成测试: ${API_STATUS:-SKIP}"
    echo "E2E测试: ${E2E_STATUS:-SKIP}"
    echo "========================================="
    
    if [ "${FRONTEND_UNIT_STATUS}" = "PASS" ] && \
       [ "${BACKEND_UNIT_STATUS}" = "PASS" ] && \
       [ "${API_STATUS}" = "PASS" ] && \
       [ "${E2E_STATUS}" = "PASS" ]; then
        echo -e "${GREEN}所有测试通过！${NC}"
        exit 0
    else
        echo -e "${RED}部分测试失败，请查看详细日志${NC}"
        exit 1
    fi
}

case "${1:-help}" in
    all)
        run_all_tests
        ;;
    unit)
        run_frontend_unit_tests
        echo ""
        run_backend_unit_tests
        ;;
    e2e)
        run_e2e_tests
        ;;
    api)
        run_api_tests
        ;;
    perf)
        run_performance_tests
        ;;
    coverage)
        generate_coverage_report
        ;;
    help|*)
        show_usage
        ;;
esac
