import uuid

from fastapi import APIRouter, Request, Response, Header

from ApiServices.TemplateService.config import api_config
from ApiServices.TemplateService.events.template.event import template_event_cluster


test_template_route = APIRouter(prefix="/test", tags=["Test"])


@test_template_route.get(
    path="/template",
    description="Test Template Route",
    operation_id="bb20c8c6-a289-4cab-9da7-34ca8a36c8e5",
)
def test_template(
    request: Request,
    response: Response,
    language: str = Header(None, alias="language"),
    domain: str = Header(None, alias="domain"),
    tz: str = Header(None, alias="timezone"),
):
    """
    Test Template Route
    """
    event_code = "bb20c8c6-a289-4cab-9da7-34ca8a36c8e5"
    token = request.headers.get(api_config.ACCESS_TOKEN_TAG, None)
    headers = {
        "language": language or "",
        "domain": domain or "",
        "eys-ext": f"{str(uuid.uuid4())}",
        "tz": tz or "GMT+3",
        "token": token,
    }
    event_cluster_matched = template_event_cluster.match_event(
        event_keys=[
            "3f510dcf-9f84-4eb9-b919-f582f30adab1",
            "9f403034-deba-4e1f-b43e-b25d3c808d39",
            "b8ec6e64-286a-4f60-8554-7a3865454944",
        ]
    )
    response.headers["X-Header"] = "Test Header GET"
    if runner_callable := event_cluster_matched.example_callable():
        return runner_callable
    raise ValueError("Event key not found or multiple matches found")


@test_template_route.post(
    path="/template",
    description="Test Template Route with Post Method",
)
def test_template_post(request: Request, response: Response):
    """
    Test Template Route with Post Method
    """
    event_cluster_matched = template_event_cluster.match_event(
        event_keys=[
            "3f510dcf-9f84-4eb9-b919-f582f30adab1",
            "9f403034-deba-4e1f-b43e-b25d3c808d39",
            "b8ec6e64-286a-4f60-8554-7a3865454944",
        ]
    )
    response.headers["X-Header"] = "Test Header POST"
    if runner_callable := event_cluster_matched.example_callable():
        return runner_callable
    raise ValueError("Event key not found or multiple matches found")
