"""
创建测试用户
"""
import sqlite3
from passlib.context import CryptContext

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

# 连接数据库
conn = sqlite3.connect('qa_system.db')
cursor = conn.cursor()

# 测试用户列表
test_users = [
    {'username': 'lisi', 'email': 'lisi@qq.com', 'password': '123456'},
    {'username': 'wangwu', 'email': 'wangwu@qq.com', 'password': '123456'},
    {'username': 'zhaoliu', 'email': 'zhaoliu@qq.com', 'password': '123456'},
]

print("创建测试用户...")
print("="*60)

for user_data in test_users:
    # 检查用户是否已存在
    cursor.execute("SELECT id FROM users WHERE username = ?", (user_data['username'],))
    existing = cursor.fetchone()
    
    if existing:
        print(f"✓ 用户 '{user_data['username']}' 已存在，跳过")
        continue
    
    # 创建新用户
    hashed_password = pwd_context.hash(user_data['password'])
    cursor.execute(
        "INSERT INTO users (username, email, hashed_password, is_active, is_admin) VALUES (?, ?, ?, 1, 0)",
        (user_data['username'], user_data['email'], hashed_password)
    )
    print(f"✓ 创建用户 '{user_data['username']}' (邮箱: {user_data['email']}, 密码: {user_data['password']})")

conn.commit()

# 显示所有用户
print("\n" + "="*60)
print("当前所有用户：")
print("="*60)

cursor.execute("SELECT id, username, email, is_admin FROM users")
users = cursor.fetchall()

for user in users:
    role = "管理员" if user[3] else "普通用户"
    print(f"{user[0]}. {user[1]} ({user[2]}) - {role}")

conn.close()

print("\n" + "="*60)
print("测试用户创建完成！")
print("所有用户的密码都是: 123456")
print("="*60)
