from app.models import init_db, SessionLocal
from app.models.user import User, UserRole
from app.utils.security import get_password_hash

def create_admin_user():
    init_db()
    db = SessionLocal()
    
    email = "admin@example.com"
    password = "admin"
    
    # Check if user exists
    user = db.query(User).filter(User.email == email).first()
    if user:
        print(f"User {email} already exists. Updating password...")
        user.password_hash = get_password_hash(password)
        db.commit()
        print(f"Password updated for {email} to: {password}")
        return

    # Create new admin user
    new_user = User(
        email=email,
        password_hash=get_password_hash(password),
        role="admin", # String type now
        display_name="Administrator",
        ad_username="admin_local",
        department="IT"
    )
    
    db.add(new_user)
    db.commit()
    print(f"Admin user created successfully.\nEmail: {email}\nPassword: {password}")
    db.close()

if __name__ == "__main__":
    create_admin_user()
