#!/usr/bin/env python3
"""
测试 onmyoji_gacha Web API
"""
import requests
import json

# API 配置
BASE_URL = "http://localhost:8080"
API_BASE = f"{BASE_URL}/onmyoji_gacha/api"
ADMIN_TOKEN = "onmyoji_admin_token_2024"

# 请求头
headers = {
    "Authorization": f"Bearer {ADMIN_TOKEN}",
    "Content-Type": "application/json"
}

def test_api():
    print("🧪 测试 onmyoji_gacha Web API")
    print("=" * 50)
    
    # 测试每日统计
    print("\n📊 测试每日统计 API...")
    try:
        response = requests.get(f"{API_BASE}/stats/daily", headers=headers)
        print(f"状态码: {response.status_code}")
        if response.status_code == 200:
            data = response.json()
            print(f"响应: {json.dumps(data, indent=2, ensure_ascii=False)}")
        else:
            print(f"错误: {response.text}")
    except Exception as e:
        print(f"请求失败: {e}")
    
    # 测试排行榜
    print("\n🏆 测试排行榜 API...")
    try:
        response = requests.get(f"{API_BASE}/stats/rank", headers=headers)
        print(f"状态码: {response.status_code}")
        if response.status_code == 200:
            data = response.json()
            print(f"响应: {json.dumps(data, indent=2, ensure_ascii=False)}")
        else:
            print(f"错误: {response.text}")
    except Exception as e:
        print(f"请求失败: {e}")
    
    # 测试用户统计（使用示例用户ID）
    print("\n👤 测试用户统计 API...")
    try:
        test_user_id = "123456789"  # 示例用户ID
        response = requests.get(f"{API_BASE}/stats/user/{test_user_id}", headers=headers)
        print(f"状态码: {response.status_code}")
        if response.status_code == 200:
            data = response.json()
            print(f"响应: {json.dumps(data, indent=2, ensure_ascii=False)}")
        else:
            print(f"错误: {response.text}")
    except Exception as e:
        print(f"请求失败: {e}")
    
    # 测试无令牌访问
    print("\n🔒 测试无令牌访问...")
    try:
        response = requests.get(f"{API_BASE}/stats/daily")
        print(f"状态码: {response.status_code}")
        print(f"响应: {response.text}")
    except Exception as e:
        print(f"请求失败: {e}")

if __name__ == "__main__":
    test_api()