# modules/admin/api.py
from typing import Annotated
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from core.database import Base, get_db
from modules.auth.models import User, UserRole
from modules.auth.dependencies import admin_only


router = APIRouter(prefix="/admin", tags=["admin"], dependencies=[Depends(admin_only)])

@router.get("/")
def read_admin():
    return {"message": "Admin route"}

@router.get("/cleardb")
def clear_db(db: Annotated[Session, Depends(get_db)], hard: bool):
    """
    Clear the database.
    'hard' parameter determines if the database should be completely reset.
    """
    if hard:
        Base.metadata.drop_all(bind=db.get_bind())
        Base.metadata.create_all(bind=db.get_bind())
        return {"message": "Database reset (HARD)"}
    else:
        tables = Base.metadata.tables.keys()
        for table_name in tables:
            # delete all tables that isn't the users table
            if table_name != "users":
                table = Base.metadata.tables[table_name]
                db.execute(table.delete())

        # delete all non-admin accounts
        db.query(User).filter(User.role != UserRole.ADMIN).delete()
        db.commit()
        return {"message": "Database cleared"}