#!/usr/bin/env python3
"""
创建测试用户的脚本
"""

import requests
import hashlib
import mysql.connector

# API基础URL
BASE_URL = "http://localhost:8000/api/v1"

def create_user_directly():
    """直接在数据库中创建用户"""
    try:
        # 连接数据库
        conn = mysql.connector.connect(
            host="101.126.85.76",
            user="mytest_db",
            password="mytest_db",
            database="mytest_db"
        )
        cursor = conn.cursor()

        # 检查用户是否已存在
        cursor.execute("SELECT id FROM users WHERE username = %s", ("testuser",))
        user = cursor.fetchone()

        if user:
            print(f"用户已存在，ID: {user[0]}")
            return user[0]

        # 创建密码哈希
        password = "TestPass123!"
        password_hash = hashlib.sha256(password.encode()).hexdigest()

        # 插入用户
        insert_query = """
        INSERT INTO users (username, email, password_hash, storage_quota, storage_used, is_active, is_verified)
        VALUES (%s, %s, %s, %s, %s, %s, %s)
        """
        cursor.execute(insert_query, (
            "testuser",
            "test@example.com",
            password_hash,
            104857600,  # 100MB
            0,
            True,
            True
        ))

        user_id = cursor.lastrowid
        conn.commit()

        print(f"用户创建成功，ID: {user_id}")
        return user_id

    except Exception as e:
        print(f"创建用户出错: {e}")
        return None
    finally:
        if 'conn' in locals() and conn.is_connected():
            cursor.close()
            conn.close()

if __name__ == "__main__":
    user_id = create_user_directly()
    if user_id:
        print(f"测试用户ID: {user_id}")
    else:
        print("创建用户失败")