from fastapi import status
from fastapi.responses import JSONResponse

from api_validations.validations_request import (
    InsertCompanyDuty,
    PatchRecord,
    ListOptions,
)

from databases import Duty

from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject
from ApiServices.api_handlers import AlchemyJsonResponse


class DutyListEventMethods(MethodToEvent):

    event_type = "SELECT"
    __event_keys__ = {
        "23231c7d-4ff2-4b39-b71b-ea350d31fadf": "duty_list",
    }
    __event_validation__ = {"23231c7d-4ff2-4b39-b71b-ea350d31fadf": None}

    @classmethod
    def duty_list(
        cls,
        list_options: ListOptions,
        token_dict: EmployeeTokenObject,
    ):
        Duty.filter_attr = list_options
        records = Duty.filter_all(system=True)
        return AlchemyJsonResponse(
            completed=True,
            message="Duty list is brought successfully",
            result=records,
        )


class DutyCreateEventMethods(MethodToEvent):

    event_type = "CREATE"
    __event_keys__ = {
        "c6ea200e-fa17-4393-b390-37f5337c9c65": "duty_create",
    }
    __event_validation__ = {"c6ea200e-fa17-4393-b390-37f5337c9c65": InsertCompanyDuty}

    @classmethod
    def duty_create(
        cls,
        data: InsertCompanyDuty,
        token_dict: EmployeeTokenObject,
    ):
        created_duty = Duty.find_or_create(**data.excluded_dump())
        Duty.save()
        return JSONResponse(
            content={
                "completed": True,
                "message": "Create Company record",
                "data": created_duty.get_dict(),
            },
            status_code=status.HTTP_200_OK,
        )


class DutyUpdateEventMethods(MethodToEvent):

    event_type = "UPDATE"
    __event_keys__ = {
        "ad952647-bcf8-482d-9e05-b2ee8086483f": "duty_update",
    }
    __event_validation__ = {"ad952647-bcf8-482d-9e05-b2ee8086483f": None}

    @classmethod
    def duty_update(
        cls,
        company_uu_id: str,
        data,
        token_dict: EmployeeTokenObject,
    ):
        find_one_company = Duty.find_one_or_abort(uu_id=company_uu_id)
        access_authorized_company = Duty.select_action(
            duty_id=getattr(token_dict, "duty_id", 5),  # ?
            filter_expr=[Duty.id == token_dict.get("")],
        )
        if access_authorized_company.count:
            data_dict = data.excluded_dump()
            updated_company = find_one_company.update(**data_dict)
            Duty.save()
            return JSONResponse(
                content={
                    "completed": True,
                    "message": "Update Company record",
                    "data": updated_company,
                },
                status_code=status.HTTP_200_OK,
            )
        return JSONResponse(
            content={"completed": True, "message": "Update Company record", "data": {}},
            status_code=status.HTTP_200_OK,
        )


class DutyPatchEventMethods(MethodToEvent):

    event_type = "PATCH"
    __event_keys__ = {
        "d5c7b5c4-7b4e-4d5b-8e3b-2b9c5f5d0c0b": "duty_patch",
    }
    __event_validation__ = {"d5c7b5c4-7b4e-4d5b-8e3b-2b9c5f5d0c0b": None}

    @classmethod
    def duty_patch(
        cls,
        company_uu_id: str,
        data: PatchRecord,
        token_dict: EmployeeTokenObject,
    ):
        find_one_company = Duty.find_one_or_abort(uu_id=company_uu_id)
        access_authorized_company = Duty.select_action(
            duty_id=getattr(token_dict, "duty_id", 5),
            filter_expr=[Duty.id == find_one_company.id],
        )
        if access_authorized_company.count:
            action = data.excluded_dump()
            find_one_company.active = bool(
                action.get("active", find_one_company.active)
            )
            find_one_company.is_confirmed = bool(
                action.get("confirm", find_one_company.is_confirmed)
            )
            find_one_company.deleted = bool(
                action.get("delete", find_one_company.deleted)
            )
            find_one_company.save()
            return JSONResponse(
                content={
                    "completed": True,
                    "message": "Patch Company record completed",
                    "data": find_one_company.get_dict(),
                },
                status_code=status.HTTP_200_OK,
            )
        return JSONResponse(
            content={
                "completed": False,
                "message": "Patch Company record failed",
                "data": {},
            },
            status_code=status.HTTP_200_OK,
        )


DutyListEventMethod = DutyListEventMethods(action=ActionsSchema(endpoint="/duty/list"))
DutyCreateEventMethod = DutyCreateEventMethods(
    action=ActionsSchema(endpoint="/duty/create")
)
DutyUpdateEventMethod = DutyUpdateEventMethods(
    action=ActionsSchema(endpoint="/duty/update")
)
DutyPatchEventMethod = DutyPatchEventMethods(
    action=ActionsSchema(endpoint="/duty/patch")
)
