"""
测试杯赛负责人访问教师申请审批页面
"""
import sqlite3

db_path = 'instance/database.db'
conn = sqlite3.connect(db_path)
cursor = conn.cursor()

print("模拟杯赛负责人访问教师申请审批页面...\n")

# 获取一个杯赛负责人
cursor.execute("""
    SELECT cm.user_id, u.name, u.role, cm.contest_id, c.name as contest_name
    FROM contest_membership cm
    JOIN user u ON cm.user_id = u.id
    JOIN contest c ON cm.contest_id = c.id
    WHERE cm.role = 'owner'
    LIMIT 1
""")
owner = cursor.fetchone()

if not owner:
    print("没有找到杯赛负责人")
    conn.close()
    exit()

user_id, user_name, user_role, contest_id, contest_name = owner
print(f"杯赛负责人信息:")
print(f"  用户ID: {user_id}")
print(f"  用户名: {user_name}")
print(f"  角色: {user_role}")
print(f"  负责杯赛ID: {contest_id}")
print(f"  杯赛名称: {contest_name}")

# 模拟查询该负责人能看到的申请
cursor.execute("""
    SELECT cm.contest_id
    FROM contest_membership cm
    WHERE cm.user_id = ? AND cm.role = 'owner'
""", (user_id,))
owned_contests = [row[0] for row in cursor.fetchall()]

print(f"\n该用户负责的所有杯赛ID: {owned_contests}")

if owned_contests:
    placeholders = ','.join('?' * len(owned_contests))
    cursor.execute(f"""
        SELECT ta.id, ta.user_id, ta.contest_id, ta.status, u.name, c.name
        FROM teacher_application ta
        JOIN user u ON ta.user_id = u.id
        JOIN contest c ON ta.contest_id = c.id
        WHERE ta.status = 'pending' AND ta.contest_id IN ({placeholders})
        ORDER BY ta.applied_at DESC
    """, owned_contests)
    apps = cursor.fetchall()

    print(f"\n该负责人能看到的待审批申请数: {len(apps)}")
    if apps:
        for app in apps:
            print(f"  申请ID: {app[0]}, 申请人: {app[4]}, 杯赛: {app[5]}")
    else:
        print("  暂无待审核的教师申请")
else:
    print("\n该用户不负责任何杯赛")

conn.close()
print("\n测试完成")
