#!/usr/bin/env python3
"""
测试陪练记录列表API
"""
import requests
import json

# API基础地址
BASE_URL = "http://120.79.247.16"

def login(username: str, password: str):
    """登录获取token"""
    url = f"https://aiedu.ireborn.com.cn/api/v1/auth/login"
    data = {
        "username": username,
        "password": password
    }
    print(f"正在登录: {username}")
    response = requests.post(url, json=data, verify=False)
    print(f"登录响应: {response.status_code}")
    result = response.json()
    print(f"登录结果: {json.dumps(result, ensure_ascii=False, indent=2)}")
    
    if response.status_code == 200 and result.get('code') == 200:
        token = result['data'].get('access_token')
        print(f"✓ 登录成功，Token: {token[:20]}...")
        return token
    else:
        print(f"✗ 登录失败")
        return None

def get_practice_sessions(token: str):
    """获取陪练记录列表"""
    url = f"https://aiedu.ireborn.com.cn/api/v1/practice/sessions/list"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    params = {
        "page": 1,
        "size": 20
    }
    
    print(f"\n正在获取陪练记录列表...")
    print(f"请求URL: {url}")
    print(f"请求参数: {params}")
    
    response = requests.get(url, headers=headers, params=params, verify=False)
    print(f"响应状态码: {response.status_code}")
    
    result = response.json()
    print(f"响应结果: {json.dumps(result, ensure_ascii=False, indent=2)}")
    
    if response.status_code == 200 and result.get('code') == 200:
        data = result['data']
        print(f"\n✓ 获取成功")
        print(f"总记录数: {data.get('total', 0)}")
        print(f"当前页记录数: {len(data.get('items', []))}")
        
        # 打印前5条记录
        items = data.get('items', [])
        if items:
            print(f"\n前{min(5, len(items))}条记录:")
            for i, item in enumerate(items[:5], 1):
                print(f"{i}. {item.get('session_id')} - {item.get('scene_name')} - {item.get('start_time')}")
        else:
            print("\n⚠️  记录列表为空!")
    else:
        print(f"\n✗ 获取失败")

def get_practice_stats(token: str):
    """获取陪练统计数据"""
    url = f"https://aiedu.ireborn.com.cn/api/v1/practice/stats"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    
    print(f"\n正在获取陪练统计数据...")
    response = requests.get(url, headers=headers, verify=False)
    print(f"响应状态码: {response.status_code}")
    
    result = response.json()
    print(f"响应结果: {json.dumps(result, ensure_ascii=False, indent=2)}")

def main():
    print("=" * 60)
    print("测试陪练记录列表API")
    print("=" * 60)
    
    # 测试多个用户
    test_users = [
        ("admin", "admin123"),  # admin
        ("consultant_001", "admin123"),  # consultant_001
        ("consultant_002", "admin123"),  # consultant_002
    ]
    
    for username, password in test_users:
        print(f"\n{'=' * 60}")
        print(f"测试用户: {username}")
        print(f"{'=' * 60}")
        
        # 登录
        token = login(username, password)
        if not token:
            print(f"用户 {username} 登录失败，跳过")
            continue
        
        # 获取陪练记录
        get_practice_sessions(token)
        
        # 获取统计数据
        get_practice_stats(token)
        
        print("\n")

if __name__ == "__main__":
    main()

