from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.admin import Permission
from src.db.session import SessionDep
from src.schema.user.permission import PermissionResponse, to_response

router = APIRouter()

@router.get("/permissions", response_model=List[PermissionResponse])
def get_all_permissions(db: SessionDep) -> List[PermissionResponse]:
    results: List[PermissionResponse] = []
    permissions = db.query(Permission).all()
    for permission in permissions:
        response = to_response(permission)
        results.append(response)
    return results

@router.get("/permissions/{permission_id}", response_model=PermissionResponse)
def get_permission(permission_id: str, db: SessionDep) -> PermissionResponse:
    permission = db.get(Permission, permission_id)
    if permission is None:
        raise HTTPException(status_code=404, detail="Permission could not be found")
    response = to_response(permission)
    return response
