from typing import Any
from fastapi import APIRouter, Depends

from index import endpoints_index
from events.services.cluster import ServicesRouterCluster

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


# Create API router
services_route = APIRouter(prefix="/services", tags=["Service Cluster"])


services_list = "ServicesList"
@services_route.post(
    path="/list",
    description="List all services endpoint",
    operation_id=endpoints_index[services_list],
)
def services_list(data: PaginateOnly, headers: CommonHeaders = Depends(CommonHeaders)):
    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 = ServicesRouterCluster.get_event_cluster(services_list)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(list_options=data)


services_create = "ServicesCreate"
@services_route.post(
    path="/create",
    description="Create service endpoint",
    operation_id=endpoints_index[services_create],
)
def services_create(data, headers: CommonHeaders = Depends(CommonHeaders)):
    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 = ServicesRouterCluster.get_event_cluster(services_create)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(list_options=data)


services_update = "ServicesUpdate"
@services_route.post(
    path="/update/{uu_id}",
    description="Update service endpoint",
    operation_id=endpoints_index[services_update],
)
def services_update(uu_id: str, data, headers: CommonHeaders = Depends(CommonHeaders)):
    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 = ServicesRouterCluster.get_event_cluster(services_update)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(list_options=data)


services_delete = "ServicesDelete"
@services_route.post(
    path="/delete/{uu_id}",
    description="Delete service endpoint",
    operation_id=endpoints_index[services_delete],
)
def services_delete(uu_id: str, headers: CommonHeaders = Depends(CommonHeaders)):
    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 = ServicesRouterCluster.get_event_cluster(services_delete)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(list_options=data)

