import sys
import os

# Add the parent directory to sys.path to import app modules
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app import database, crud, schemas, auth, models

def reset_admin():
    db = database.SessionLocal()
    try:
        username = "admin"
        password = "admin123"
        
        user = crud.get_user_by_username(db, username)
        if user:
            print(f"Updating password for {username}...")
            user.hashed_password = auth.get_password_hash(password)
            db.commit()
            print("Password updated successfully.")
        else:
            print(f"Creating user {username}...")
            user_in = schemas.UserCreate(username=username, password=password, role=models.UserRole.admin)
            hashed_password = auth.get_password_hash(password)
            crud.create_user(db, user_in, hashed_password)
            print("User created successfully.")
            
    except Exception as e:
        print(f"Error: {e}")
    finally:
        db.close()

if __name__ == "__main__":
    reset_admin()
