from typing import Any
from fastapi import APIRouter, Depends

from index import endpoints_index
from events.building_parts.cluster import PartsRouterCluster

from api_validations.defaults.validations import CommonHeaders
from api_validations.response.pagination import PaginateOnly
from api_middlewares.token_provider import TokenProvider


parts_endpoint_route = APIRouter(prefix="/parts", tags=["Parts Cluster"])


parts_list = "PartsList"
@parts_endpoint_route.post(
    path="/list",
    description="List all parts endpoint",
    operation_id=endpoints_index[parts_list],
)
def parts_list_route(data: PaginateOnly, headers: CommonHeaders = Depends(CommonHeaders.as_dependency)):
    token_object = TokenProvider.get_dict_from_redis(token=headers.token)
    event_founder_dict = dict(endpoint_code=headers.operation_id, token=token_object)
    event_key = TokenProvider.retrieve_event_codes(**event_founder_dict)
    FoundCluster = PartsRouterCluster.get_event_cluster(parts_list)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(list_options=data, headers=headers)


parts_create = "PartsCreate"
@parts_endpoint_route.post(
    path="/create",
    description="Create part endpoint",
    operation_id=endpoints_index[parts_create],
)
def parts_create_route(data, headers: CommonHeaders = Depends(CommonHeaders.as_dependency)):
    token_object = TokenProvider.get_dict_from_redis(token=headers.token)
    event_founder_dict = dict(endpoint_code=headers.operation_id, token=token_object)
    event_key = TokenProvider.retrieve_event_codes(**event_founder_dict)
    FoundCluster = PartsRouterCluster.get_event_cluster(parts_create)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(data=data, headers=headers)


parts_update = "PartsUpdate"
@parts_endpoint_route.post(
    path="/update/{uu_id}",
    description="Update part endpoint",
    operation_id=endpoints_index[parts_update],
)
def parts_update_route(uu_id: str, data, headers: CommonHeaders = Depends(CommonHeaders.as_dependency)):
    token_object = TokenProvider.get_dict_from_redis(token=headers.token)
    event_founder_dict = dict(endpoint_code=headers.operation_id, token=token_object)
    event_key = TokenProvider.retrieve_event_codes(**event_founder_dict)
    FoundCluster = PartsRouterCluster.get_event_cluster(parts_update)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(uu_id=uu_id, data=data, headers=headers)


parts_delete = "PartsDelete"
@parts_endpoint_route.post(
    path="/delete/{uu_id}",
    description="Delete part endpoint",
    operation_id=endpoints_index[parts_delete],
)
def parts_delete_route(uu_id: str, headers: CommonHeaders = Depends(CommonHeaders.as_dependency)):
    token_object = TokenProvider.get_dict_from_redis(token=headers.token)
    event_founder_dict = dict(endpoint_code=headers.operation_id, token=token_object)
    event_key = TokenProvider.retrieve_event_codes(**event_founder_dict)
    FoundCluster = PartsRouterCluster.get_event_cluster(parts_delete)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(uu_id=uu_id, headers=headers)