"""Script to update admin user role."""

import sys
from pathlib import Path

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from admin.backend.app.models import AdminUser

def update_role(email: str, new_role: str, db_url: str):
    """Update role for an admin user."""
    engine = create_engine(db_url)
    SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    session = SessionLocal()
    
    try:
        admin = session.query(AdminUser).filter(AdminUser.email == email).first()
        if not admin:
            print(f"❌ User {email} not found!")
            return
        
        old_role = admin.role
        admin.role = new_role
        session.commit()
        
        print(f"✅ Role updated for {email}!")
        print(f"   Old role: {old_role}")
        print(f"   New role: {new_role}")
        
    except Exception as e:
        session.rollback()
        print(f"❌ Error: {e}")
    finally:
        session.close()


if __name__ == "__main__":
    EMAIL = "bruno@taaazzz-prog.fr"
    NEW_ROLE = "super-admin"  # super-admin has full access
    DB_URL = "sqlite:///./admin_dev.db"
    
    print("=" * 60)
    print("🛡️  Web Sentinel - Update Admin Role")
    print("=" * 60)
    print(f"Updating role for: {EMAIL}")
    print(f"New role: {NEW_ROLE}")
    print()
    
    update_role(EMAIL, NEW_ROLE, DB_URL)
