"""Fix: Update NULL password_hash for test user."""
import asyncio, sys
sys.path.insert(0, '.')
import aiosqlite
from common.jwt_auth import get_password_hash, verify_password

async def fix():
    async with aiosqlite.connect('db/memos.db') as db:
        db.row_factory = aiosqlite.Row
        async with db.execute('SELECT id, email, username, password_hash FROM cuccu_users') as c:
            rows = await c.fetchall()
            print("=== USERS IN DB ===")
            for r in rows:
                d = dict(r)
                ph = d['password_hash']
                print(f"id={d['id']} email={d['email']} username={d['username']} hash={'NULL' if ph is None else ph[:25]}")
        
        new_hash = get_password_hash("Password123!")
        await db.execute("UPDATE cuccu_users SET password_hash = ? WHERE email = ?", (new_hash, "test@example.com"))
        await db.commit()
        print("Updated password_hash for test@example.com")
        
        async with db.execute('SELECT id, password_hash FROM cuccu_users WHERE email = ?', ("test@example.com",)) as c:
            row = await c.fetchone()
            if row:
                ok = verify_password("Password123!", row['password_hash'])
                print(f"verify_password OK: {ok}")

asyncio.run(fix())
