#!/bin/bash
# 快速测试成绩报告和错题本功能
# 使用方法：./quick_test_score_mistakes.sh

set -e

echo "=========================================="
echo "成绩报告与错题本功能快速测试"
echo "=========================================="

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

# 1. 检查MySQL容器
echo -e "\n${YELLOW}1. 检查MySQL容器状态...${NC}"
if docker ps | grep -q kaopeilian-mysql-dev; then
    echo -e "${GREEN}✓ MySQL容器正在运行${NC}"
else
    echo -e "${RED}✗ MySQL容器未运行，请先启动${NC}"
    echo "启动命令: docker-compose -f docker-compose.dev.yml up -d mysql-dev"
    exit 1
fi

# 2. 检查数据库字段
echo -e "\n${YELLOW}2. 验证数据库字段...${NC}"
echo "检查exams表的round字段..."
docker exec -i kaopeilian-mysql-dev mysql -u root -pnj861021 kaopeilian -e "
    SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_COMMENT 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA='kaopeilian' 
    AND TABLE_NAME='exams' 
    AND COLUMN_NAME LIKE 'round%';" 2>/dev/null | grep -v "Warning"

echo "检查exam_mistakes表的question_type字段..."
docker exec -i kaopeilian-mysql-dev mysql -u root -pnj861021 kaopeilian -e "
    SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_COMMENT 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA='kaopeilian' 
    AND TABLE_NAME='exam_mistakes' 
    AND COLUMN_NAME='question_type';" 2>/dev/null | grep -v "Warning"

# 3. 检查后端服务
echo -e "\n${YELLOW}3. 检查后端服务状态...${NC}"
if curl -s http://localhost:8000/health > /dev/null 2>&1; then
    echo -e "${GREEN}✓ 后端服务正在运行${NC}"
else
    echo -e "${RED}✗ 后端服务未运行${NC}"
    echo "请在另一个终端启动后端："
    echo "  cd kaopeilian-backend"
    echo "  uvicorn app.main:app --reload --host 0.0.0.0 --port 8000"
    exit 1
fi

# 4. 测试API（需要token）
echo -e "\n${YELLOW}4. 测试API接口...${NC}"
echo "提示：需要先登录获取token"
echo "运行Python测试脚本："
echo "  python3 test_score_report_api.py"

# 5. 前端服务检查
echo -e "\n${YELLOW}5. 检查前端服务状态...${NC}"
if curl -s http://localhost:3001 > /dev/null 2>&1; then
    echo -e "${GREEN}✓ 前端服务正在运行${NC}"
    echo ""
    echo "📊 成绩报告页面："
    echo "   http://localhost:3001/trainee/score-report"
    echo ""
    echo "📝 错题本页面："
    echo "   http://localhost:3001/trainee/mistakes"
else
    echo -e "${RED}✗ 前端服务未运行${NC}"
    echo "请在另一个终端启动前端："
    echo "  cd kaopeilian-frontend"
    echo "  npm run dev"
fi

echo ""
echo "=========================================="
echo "检查完成！请根据上述提示进行测试。"
echo "=========================================="

