import typing

from databases import (
    BuildDecisionBookItems,
)

from api_events.events.abstract_class import MethodToEvent
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from ApiServices.api_handlers import AlchemyJsonResponse
from api_library.date_time_actions.date_functions import system_arrow


class DecisionBookDecisionBookItemsDebitsListEventMethods(MethodToEvent):

    event_type = "SELECT"
    __event_keys__ = {
        "a1d2b1f6-9b8d-4f6b-8f4d-6b1f6a9d8b1a": "decision_book_decision_book_items_debits_list",
    }
    __event_validation__ = {"a1d2b1f6-9b8d-4f6b-8f4d-6b1f6a9d8b1a": None}

    @classmethod
    def decision_book_decision_book_items_debits_list(
        cls,
        decision_book_id: str,
        token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
    ):
        BuildDecisionBookItems.pre_query = BuildDecisionBookItems.filter_all(
            BuildDecisionBookItems.decision_book_id == decision_book_id,
        ).query
        BuildDecisionBookItems.filter_attr = None
        records = BuildDecisionBookItems.filter_all(
            BuildDecisionBookItems.decision_book_id == decision_book_id,
        )
        return AlchemyJsonResponse(
            completed=True,
            message="Decision Book Items Debits are listed",
            result=records,
        )


class DecisionBookDecisionBookItemsDebitsCreateEventMethods(MethodToEvent):

    event_type = "CREATE"
    __event_keys__ = {
        "a1d2b1f6-9b8d-4f6b-8f4d-6b1f6a9d8b1a": "decision_book_decision_book_items_debits_create",
    }
    __event_validation__ = {"a1d2b1f6-9b8d-4f6b-8f4d-6b1f6a9d8b1a": None}

    @classmethod
    def decision_book_decision_book_items_debits_create(
        cls,
        decision_book_id: str,
        token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
        decision_book_items_debits: dict,
    ):
        if isinstance(token_dict, EmployeeTokenObject):

            raise HTTPException(
                status_code=status.HTTP_406_NOT_ACCEPTABLE,
                detail="No employee can reach this event. An notification is send to admin about event registration",
            )

        decision_book_items_debits["decision_book_id"] = decision_book_id
        decision_book_items_debits["created_at"] = system_arrow().datetime
        decision_book_items_debits["created_by"] = token_dict.employee_id
        decision_book_items_debits["updated_at"] = system_arrow().datetime
        decision_book_items_debits["updated_by"] = token_dict.employee_id
        decision_book_items_debits["is_active"] = True
        decision_book_items_debits["is_confirmed"] = False
        decision_book_items_debits["is_deleted"] = False
        decision_book_items_debits["confirmed_at"] = None
        decision_book_items_debits["confirmed_by"] = None
        decision_book_items_debits["deleted_at"] = None
        decision_book_items_debits["deleted_by"] = None
        decision_book_items_debits["confirmed_at"] = None
        decision_book_items_debits["confirmed_by"] = None
        decision_book_items_debits["deleted_at"] = None
        decision_book_items_debits["deleted_by"] = None
        decision_book_items_debits["confirmed_at"] = None
        decision_book_items_debits["confirmed_by"] = None
        decision_book_items_debits["deleted_at"] = None
        decision_book_items_debits["deleted_by"] = None
        BuildDecisionBookItems.pre_query = BuildDecisionBookItems.filter_all(
            BuildDecisionBookItems.decision_book_id == decision_book_id,
        ).query
        BuildDecisionBookItems.filter_attr = None
        records = BuildDecisionBookItems.filter_all(
            BuildDecisionBookItems.decision_book_id == decision_book_id,
        )
        return AlchemyJsonResponse(
            completed=True,
            message="Decision Book Items Debits are listed",
            result=records,
        )
