from typing import Any
from fastapi import APIRouter, Depends

from index import endpoints_index
from events.user.cluster import UserRouterCluster

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


user_endpoint_route = APIRouter(prefix="/users", tags=["User Cluster"])


user_list = "UserList"
@user_endpoint_route.post(
    path="/list",
    description="List all users endpoint",
    operation_id=endpoints_index[user_list],
)
def user_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 = UserRouterCluster.get_event_cluster(user_list)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(list_options=data, headers=headers)


user_create = "UserCreate"
@user_endpoint_route.post(
    path="/create",
    description="Create user endpoint",
    operation_id=endpoints_index[user_create],
)
def user_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 = UserRouterCluster.get_event_cluster(user_create)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(data=data, headers=headers)


user_update = "UserUpdate"
@user_endpoint_route.post(
    path="/update/{uu_id}",
    description="Update user endpoint",
    operation_id=endpoints_index[user_update],
)
def user_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 = UserRouterCluster.get_event_cluster(user_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)


user_delete = "UserDelete"
@user_endpoint_route.delete(
    path="/delete/{uu_id}",
    description="Delete user endpoint",
    operation_id=endpoints_index[user_delete],
)
def user_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 = UserRouterCluster.get_event_cluster(user_delete)
    event_cluster_matched = FoundCluster.match_event(event_key=event_key)
    return event_cluster_matched.event_callable(uu_id=uu_id, headers=headers)
    
