#!/bin/bash
# 测试与课程对话功能 - Dify 集成

echo "============================================================"
echo "🧪 与课程对话功能测试 - Dify 集成"
echo "============================================================"

API_BASE="http://localhost:8000"

# 1. 登录获取 token
echo ""
echo "🔑 正在登录..."
LOGIN_RESPONSE=$(curl -s -X POST "${API_BASE}/api/v1/auth/login" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=test_user&password=123456")

TOKEN=$(echo $LOGIN_RESPONSE | python3 -c "import sys, json; print(json.load(sys.stdin).get('access_token', ''))")

if [ -z "$TOKEN" ]; then
  echo "❌ 登录失败"
  echo "响应: $LOGIN_RESPONSE"
  exit 1
fi

echo "✅ 登录成功"
echo "Token: ${TOKEN:0:20}..."

# 2. 测试首次对话
echo ""
echo "============================================================"
echo "测试场景 1: 首次对话（创建新会话）"
echo "============================================================"
echo ""
echo "💬 测试与课程 1 对话"
echo "问题: 这门课程讲什么？"
echo ""
echo "📡 SSE 事件流:"
echo "------------------------------------------------------------"

curl -N -X POST "${API_BASE}/api/v1/course/chat" \
  -H "Authorization: Bearer ${TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "course_id": 1,
    "query": "这门课程讲什么？"
  }'

echo ""
echo "------------------------------------------------------------"
echo ""
echo "✅ 测试完成！"
echo ""
echo "如需测试续接对话，请复制上面输出的 conversation_id，然后运行："
echo "  curl -N -X POST '${API_BASE}/api/v1/course/chat' \\"
echo "    -H 'Authorization: Bearer ${TOKEN}' \\"
echo "    -H 'Content-Type: application/json' \\"
echo "    -d '{\"course_id\": 1, \"query\": \"能详细说说吗？\", \"conversation_id\": \"你的conversation_id\"}'"

