import sqlite3
from passlib.context import CryptContext

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

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

# 查询admin用户
cursor.execute("SELECT id, username, email, hashed_password FROM users WHERE username = 'admin'")
user = cursor.fetchone()

if user:
    print(f"用户ID: {user[0]}")
    print(f"用户名: {user[1]}")
    print(f"邮箱: {user[2]}")
    print(f"密码哈希: {user[3]}")
    print("\n" + "="*60)
    
    # 测试密码验证
    test_passwords = ["123456", "admin", "admin123", "password"]
    
    for pwd in test_passwords:
        result = pwd_context.verify(pwd, user[3])
        print(f"测试密码 '{pwd}': {'✓ 正确' if result else '✗ 错误'}")
    
    print("\n" + "="*60)
    
    # 生成新的密码哈希用于对比
    new_hash = pwd_context.hash("123456")
    print(f"\n新生成的 '123456' 哈希值:")
    print(new_hash)
    
    # 验证新哈希
    verify_new = pwd_context.verify("123456", new_hash)
    print(f"\n验证新哈希: {'✓ 成功' if verify_new else '✗ 失败'}")
    
else:
    print("未找到admin用户")

conn.close()
