import sqlite3
from database import DB_NAME

print(f"Connecting to database: {DB_NAME}")
conn = sqlite3.connect(DB_NAME)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()

try:
    # 1. Ensure the role column exists (safely skipping if already altered)
    cursor.execute("ALTER TABLE users ADD COLUMN role TEXT DEFAULT 'user'")
    print("✅ Schema checked: 'role' column confirmed.")
except sqlite3.OperationalError:
    pass

# 2. Force upgrade 'tabby' to administrator status
cursor.execute("UPDATE users SET role = 'admin' WHERE username = 'tabby'")
conn.commit()
print("⚡ Authorization Matrix Updated: 'tabby' has been granted root ADMIN privileges.")

# 3. Double-check and list registered profiles to confirm the change saved
cursor.execute("SELECT id, username, role FROM users")
print("\n--- Current Registered Profiles Vault ---")
profiles = cursor.fetchall()
for row in profiles:
    status_marker = "👑 [ADMIN]" if row["role"] == "admin" else "👤 [USER]"
    print(f"ID: {row['id']} | Username: {row['username']} | Role: {status_marker}")

conn.close()
print("\n🎉 Database sync completed perfectly!")