from typing import Union

from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from api_validations.core_response import AlchemyJsonResponse
from api_validations.validations_request import (
    ListOptions,
)
from databases.sql_models.building.build import BuildTypes


class BuildTypesListEventMethods(MethodToEvent):

    event_type = "SELECT"
    __event_keys__ = {
        "5344d03c-fc47-43ec-8c44-6c2acd7e5d9f": "build_types_list",
    }

    @classmethod
    def build_types_list(
        cls,
        list_options: ListOptions,
        token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
    ):
        from fastapi.exceptions import HTTPException

        if isinstance(token_dict, EmployeeTokenObject):
            BuildTypes.filter_attr = list_options
            results = BuildTypes.filter_all()
            return AlchemyJsonResponse(
                completed=True,
                result=results,
                message="Build Types are listed successfully",
            )
        elif isinstance(token_dict, OccupantTokenObject):
            raise HTTPException(
                status_code=403, detail="You are not authorized to access this endpoint"
            )
        else:
            raise HTTPException(
                status_code=403, detail="You are not authorized to access this endpoint"
            )


BuildTypesListEventMethod = BuildTypesListEventMethods(
    action=ActionsSchema(endpoint="/building/types/list")
)
