#!/bin/bash

echo "========================================="
echo "  学生知识图谱测试套件运行器"
echo "========================================="
echo ""

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

# 切换到项目目录
cd /Volumes/T9/code/math/apis/FilamentAdmin

echo -e "${BLUE}[1/5] 清理缓存...${NC}"
php artisan optimize:clear
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ 缓存清理完成${NC}"
else
    echo -e "${RED}✗ 缓存清理失败${NC}"
    exit 1
fi
echo ""

echo -e "${BLUE}[2/5] 生成自动加载文件...${NC}"
composer dump-autoload
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ 自动加载文件生成完成${NC}"
else
    echo -e "${RED}✗ 自动加载文件生成失败${NC}"
    exit 1
fi
echo ""

echo -e "${BLUE}[3/5] 运行单元测试...${NC}"
echo -e "${YELLOW}测试文件: tests/Unit/StudentKnowledgeGraphTest.php${NC}"
php artisan test --testsuite=Unit --filter=StudentKnowledgeGraphTest
UNIT_TEST_RESULT=$?
echo ""

echo -e "${BLUE}[4/5] 运行服务测试...${NC}"
echo -e "${YELLOW}测试文件: tests/Unit/Services/KnowledgeGraphServiceTest.php${NC}"
php artisan test --testsuite=Unit --filter=KnowledgeGraphServiceTest
SERVICE_TEST_RESULT=$?
echo ""

echo -e "${BLUE}[5/5] 运行集成测试...${NC}"
echo -e "${YELLOW}测试文件: tests/Feature/StudentKnowledgeGraphIntegrationTest.php${NC}"
php artisan test --testsuite=Feature --filter=StudentKnowledgeGraphIntegrationTest
INTEGRATION_TEST_RESULT=$?
echo ""

# 汇总结果
echo "========================================="
echo -e "${YELLOW}测试结果汇总${NC}"
echo "========================================="

if [ $UNIT_TEST_RESULT -eq 0 ]; then
    echo -e "${GREEN}✓ 单元测试 (StudentKnowledgeGraphTest): 通过${NC}"
else
    echo -e "${RED}✗ 单元测试 (StudentKnowledgeGraphTest): 失败${NC}"
fi

if [ $SERVICE_TEST_RESULT -eq 0 ]; then
    echo -e "${GREEN}✓ 服务测试 (KnowledgeGraphServiceTest): 通过${NC}"
else
    echo -e "${RED}✗ 服务测试 (KnowledgeGraphServiceTest): 失败${NC}"
fi

if [ $INTEGRATION_TEST_RESULT -eq 0 ]; then
    echo -e "${GREEN}✓ 集成测试 (StudentKnowledgeGraphIntegrationTest): 通过${NC}"
else
    echo -e "${RED}✗ 集成测试 (StudentKnowledgeGraphIntegrationTest): 失败${NC}"
fi
echo ""

# 总体结果
if [ $UNIT_TEST_RESULT -eq 0 ] && [ $SERVICE_TEST_RESULT -eq 0 ] && [ $INTEGRATION_TEST_RESULT -eq 0 ]; then
    echo -e "${GREEN}=========================================${NC}"
    echo -e "${GREEN}  🎉 所有测试通过！${NC}"
    echo -e "${GREEN}=========================================${NC}"
    exit 0
else
    echo -e "${RED}=========================================${NC}"
    echo -e "${RED}  ❌ 部分测试失败，请查看上方日志${NC}"
    echo -e "${RED}=========================================${NC}"
    exit 1
fi
