import typing

from databases import (
    Modules,
    BuildLivingSpace,
)
from api_validations.validations_request import (
    RegisterModules2Occupant,
    RegisterModules2Employee,
)
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from api_events.events.events.events_bind_services import (
    ServiceBindOccupantEventMethods,
)
from api_library.date_time_actions.date_functions import system_arrow
from api_validations.core_response import AlchemyJsonResponse


class ModulesBindOccupantEventMethods(MethodToEvent):

    event_type = "UPDATE"
    __event_keys__ = {
        "": "modules_bind_occupant",
    }

    @classmethod
    def modules_bind_occupant_system(
        cls, build_living_space_id: int, modules_id: int, expires_at: str = None
    ):

        living_space = BuildLivingSpace.filter_one(
            Modules.id == build_living_space_id,
            Modules.active == True,
        ).data
        modules = Modules.filter_one(
            Modules.id == modules_id, Modules.active == True
        ).data

        service_build_dict = dict(build_living_space_id=living_space.id)
        service_build_dict["expires_at"] = str(
            system_arrow.get(living_space.expiry_ends)
        )
        if not expires_at:
            service_build_dict["expires_at"] = str(system_arrow.get(expires_at))

        for service in modules.retrieve_services():
            ServiceBindOccupantEventMethods.bind_services_occupant_system(
                **service_build_dict,
                service_id=service.id,
            )
        BuildLivingSpace.save()
        return True

    @classmethod
    def modules_bind_occupant(
        cls,
        data: RegisterModules2Occupant,
        token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
    ):

        return


class ModulesBindEmployeeEventMethods(MethodToEvent):

    event_type = "UPDATE"
    __event_keys__ = {
        "": "modules_bind_employee",
    }

    @classmethod
    def modules_bind_employee(
        cls,
        data: RegisterModules2Employee,
        token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
    ):
        return


ModulesBindOccupantEventMethod = ModulesBindOccupantEventMethods(
    action=ActionsSchema(endpoint="/bind/modules/occupant")
)
ModulesBindEmployeeEventMethod = ModulesBindEmployeeEventMethods(
    action=ActionsSchema(endpoint="/bind/modules/employee")
)
