#!/bin/bash

# Phase 6 校本课程包功能验证脚本

echo "=========================================="
echo "Phase 6 校本课程包功能验证"
echo "=========================================="
echo ""

FRONTEND_URL="http://localhost:5173"
BACKEND_URL="http://localhost:3000"

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

# 测试计数器
PASS=0
FAIL=0

# 测试函数
test_service() {
  local name=$1
  local url=$2
  local keyword=$3

  echo -n "测试 $name... "
  response=$(curl -s "$url" | head -c 1000)

  if echo "$response" | grep -q "$keyword"; then
    echo -e "${GREEN}✅ 通过${NC}"
    ((PASS++))
    return 0
  else
    echo -e "${RED}❌ 失败${NC}"
    ((FAIL++))
    return 1
  fi
}

# 检查服务是否运行
echo "1. 检查服务状态"
echo "----------------"

# 检查前端
if curl -s -f "$FRONTEND_URL" > /dev/null 2>&1; then
  echo -e "${GREEN}✅ 前端服务运行正常${NC} ($FRONTEND_URL)"
else
  echo -e "${RED}❌ 前端服务未运行${NC}"
  exit 1
fi

# 检查后端
if curl -s -f "$BACKEND_URL/api/v1/courses" > /dev/null 2>&1; then
  echo -e "${GREEN}✅ 后端服务运行正常${NC} ($BACKEND_URL)"
else
  echo -e "${YELLOW}⚠️  后端服务可能需要认证${NC}"
fi

echo ""
echo "2. 前端路由测试"
echo "----------------"

# 测试前端页面
test_service "登录页面" "$FRONTEND_URL/login" "登录"
test_service "前端首页" "$FRONTEND_URL/" "app"

echo ""
echo "3. 后端 API 测试"
echo "----------------"

# 登录获取token
echo -n "登录测试... "
LOGIN_RESPONSE=$(curl -s -X POST "$BACKEND_URL/api/v1/auth/login" \
  -H "Content-Type: application/json" \
  -d '{"account":"teacher1","role":"teacher","password":"123456"}')

if echo "$LOGIN_RESPONSE" | grep -q "token"; then
  echo -e "${GREEN}✅ 通过${NC}"
  ((PASS++))

  TOKEN=$(echo "$LOGIN_RESPONSE" | grep -o '"token":"[^"]*"' | cut -d'"' -f4)
  echo "Token 获取成功: ${TOKEN:0:50}..."

  echo ""
  echo "4. 校本课程 API 测试"
  echo "----------------"

  # 测试获取源课程列表
  echo -n "获取源课程列表... "
  SOURCE_RESPONSE=$(curl -s -X GET "$BACKEND_URL/api/v1/teacher/school-courses/source-courses" \
    -H "Authorization: Bearer $TOKEN")

  if echo "$SOURCE_RESPONSE" | grep -q "id"; then
    echo -e "${GREEN}✅ 通过${NC}"
    ((PASS++))

    # 提取第一个课程ID
    COURSE_ID=$(echo "$SOURCE_RESPONSE" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
    echo "  找到源课程 ID: $COURSE_ID"

    # 测试获取校本课程列表
    echo -n "获取校本课程列表... "
    LIST_RESPONSE=$(curl -s -X GET "$BACKEND_URL/api/v1/teacher/school-courses" \
      -H "Authorization: Bearer $TOKEN")

    if [ $? -eq 0 ]; then
      echo -e "${GREEN}✅ 通过${NC}"
      ((PASS++))

      # 显示课程数量
      COUNT=$(echo "$LIST_RESPONSE" | grep -o '"id"' | wc -l | tr -d ' ')
      echo "  当前校本课程数量: $COUNT"
    else
      echo -e "${RED}❌ 失败${NC}"
      ((FAIL++))
    fi

  else
    echo -e "${RED}❌ 失败${NC}"
    ((FAIL++))
  fi

else
  echo -e "${RED}❌ 失败${NC}"
  ((FAIL++))
  echo "  错误: $LOGIN_RESPONSE"
fi

echo ""
echo "5. 数据库验证"
echo "----------------"

# 检查数据库文件
if [ -f "/Users/retirado/Program/ccProgram/reading-platform-backend/dev.db" ]; then
  echo -e "${GREEN}✅ 数据库文件存在${NC}"
  ((PASS++))

  # 检查表是否存在
  echo -n "检查 school_courses 表... "
  if sqlite3 /Users/retirado/Program/ccProgram/reading-platform-backend/dev.db \
    "SELECT COUNT(*) FROM school_courses" > /dev/null 2>&1; then
    echo -e "${GREEN}✅ 通过${NC}"
    ((PASS++))

    # 查询数据
    COUNT=$(sqlite3 /Users/retirado/Program/ccProgram/reading-platform-backend/dev.db \
      "SELECT COUNT(*) FROM school_courses" 2>/dev/null)
    echo "  school_courses 记录数: $COUNT"
  else
    echo -e "${RED}❌ 表不存在${NC}"
    ((FAIL++))
  fi

  echo -n "检查 school_course_lessons 表... "
  if sqlite3 /Users/retirado/Program/ccProgram/reading-platform-backend/dev.db \
    "SELECT COUNT(*) FROM school_course_lessons" > /dev/null 2>&1; then
    echo -e "${GREEN}✅ 通过${NC}"
    ((PASS++))

    # 查询数据
    COUNT=$(sqlite3 /Users/retirado/Program/ccProgram/reading-platform-backend/dev.db \
      "SELECT COUNT(*) FROM school_course_lessons" 2>/dev/null)
    echo "  school_course_lessons 记录数: $COUNT"
  else
    echo -e "${RED}❌ 表不存在${NC}"
    ((FAIL++))
  fi
else
  echo -e "${RED}❌ 数据库文件不存在${NC}"
  ((FAIL++))
fi

echo ""
echo "=========================================="
echo "测试汇总"
echo "=========================================="
echo -e "通过: ${GREEN}$PASS${NC}"
echo -e "失败: ${RED}$FAIL${NC}"
echo "总计: $((PASS + FAIL))"
echo ""

if [ $FAIL -eq 0 ]; then
  echo -e "${GREEN}🎉 所有测试通过!${NC}"
  exit 0
else
  echo -e "${YELLOW}⚠️  存在失败的测试,请检查${NC}"
  exit 1
fi
