import sqlite3
db = sqlite3.connect("db/memos.db")
db.row_factory = sqlite3.Row
cur = db.cursor()

# Check e2etest user
cur.execute("SELECT id, username, email, role FROM cuccu_users WHERE username = 'e2etest'")
row = cur.fetchone()
if row:
    print("EXISTS:", dict(row))
else:
    print("NOT FOUND — will create")
    # Check schema
    cur.execute("PRAGMA table_info(cuccu_users)")
    cols = [r["name"] for r in cur.fetchall()]
    print("Columns:", cols)
    
    import hashlib, uuid
    from datetime import datetime
    
    # Try bcrypt first
    try:
        import bcrypt
        hashed = bcrypt.hashpw(b"Test12345!", bcrypt.gensalt()).decode()
    except Exception:
        hashed = hashlib.sha256(b"Test12345!").hexdigest()
    
    user_id = str(uuid.uuid4())
    now = datetime.utcnow().isoformat()
    
    cur.execute("""
        INSERT OR IGNORE INTO cuccu_users (username, email, password_hash, role, created_at)
        VALUES (?, ?, ?, ?, ?)
    """, ("e2etest", "e2etest@test.local", hashed, "USER", now))
    db.commit()
    
    cur.execute("SELECT id, username, email, role FROM cuccu_users WHERE username = 'e2etest'")
    row = cur.fetchone()
    print("CREATED:", dict(row))

db.close()
