"""检查数据库连接和账号状态"""
import os

def check_db():
    try:
        import mysql.connector
        conn = mysql.connector.connect(
            host="mysql.eazygame.cn",
            port=14013,
            user="root",
            password="wx1998WX",
            database="next_edu"
        )
        cursor = conn.cursor()

        # 检查用户表
        cursor.execute("SELECT id, email, name FROM users WHERE email LIKE '%@xiaoxue.edu.cn' LIMIT 20")
        users = cursor.fetchall()
        print("=== 用户列表 ===")
        for u in users:
            print(f"  ID: {u[0]}, Email: {u[1]}, Name: {u[2]}")

        # 检查用户角色关联
        cursor.execute("""
            SELECT u.email, r.name as role_name
            FROM users u
            JOIN users_to_roles utr ON u.id = utr.user_id
            JOIN roles r ON utr.role_id = r.id
            WHERE u.email LIKE '%@xiaoxue.edu.cn'
            LIMIT 30
        """)
        roles = cursor.fetchall()
        print("\n=== 用户角色 ===")
        for r in roles:
            print(f"  {r[0]} -> {r[1]}")

        # 检查课案表
        cursor.execute("SELECT COUNT(*) FROM lesson_plans")
        count = cursor.fetchone()[0]
        print(f"\n=== 课案数量: {count} ===")

        # 检查课案模板表
        cursor.execute("SELECT COUNT(*) FROM lesson_plan_templates")
        tpl_count = cursor.fetchone()[0]
        print(f"=== 课案模板数量: {tpl_count} ===")

        # 检查 onboarding 状态
        cursor.execute("SELECT email, onboarded FROM users WHERE email IN ('admin@xiaoxue.edu.cn', 't_chinese_1@xiaoxue.edu.cn', 'student_g1c1_1@xiaoxue.edu.cn', 'parent_g1c1_1@xiaoxue.edu.cn')")
        onboarded = cursor.fetchall()
        print("\n=== Onboarding 状态 ===")
        for o in onboarded:
            print(f"  {o[0]}: onboarded={o[1]}")

        cursor.close()
        conn.close()
    except Exception as e:
        print(f"❌ 数据库错误: {e}")

check_db()
