# Generated by oapi-gen. DO NOT EDIT.
from __future__ import annotations

from typing import Annotated
from uuid import UUID

from msgspec import Meta, convert, json, to_builtins
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route

from .._runtime import RequestError as _runtime_RequestError
from .._runtime import boolean as _runtime_boolean
from .._runtime import check_json_content_type as _runtime_check_json_content_type
from .._runtime import check_property_counts as _runtime_check_property_counts
from .._runtime import comma_values as _runtime_comma_values
from .._runtime import encode_header as _runtime_encode_header
from .._runtime import error_response as _runtime_error_response
from .._runtime import json_body as _runtime_json_body
from .._runtime import multipart_body as _runtime_multipart_body
from .._runtime import parameter as _runtime_parameter
from .._runtime import upload as _runtime_upload
from ..contracts.cats import CatsHandler
from ..contracts.cats import CreateCat as _contracts_CreateCat
from ..contracts.cats import ListCats as _contracts_ListCats
from ..contracts.cats import ShowCatById as _contracts_ShowCatById
from ..models import Cat as _models_Cat
from ..models import CatCreate as _models_CatCreate
from ..models import HttpError as _models_HttpError

_encoder = json.Encoder()
_list_cats__parameter_limit = Annotated[int, Meta(ge=1, le=100)] | None
json.Decoder(_list_cats__parameter_limit)
_list_cats__200_decoder = json.Decoder(list[_models_Cat])
_list_cats__404_decoder = json.Decoder(_models_HttpError)
_create_cat__body_decoder = json.Decoder(_models_CatCreate)
_show_cat_by_id__parameter_cat_id = UUID
json.Decoder(_show_cat_by_id__parameter_cat_id)
_show_cat_by_id__200_decoder = json.Decoder(_models_Cat)
_show_cat_by_id__404_decoder = json.Decoder(_models_HttpError)


def create_routes(handler: CatsHandler, *, prefix: str = "") -> dict[tuple[str, str], Route]:
    __oapi_handler = handler
    __oapi_routes: dict[tuple[str, str], Route] = {}

    async def __oapi_endpoint_list_cats(__oapi_http: Request) -> Response:
        try:
            __oapi_parameter_limit = _runtime_parameter(
                __oapi_http.query_params.get("limit"),
                _list_cats__parameter_limit,
                ("query", "limit"),
                required=False,
                default=None,
            )
        except _runtime_RequestError as __oapi_error:
            return _runtime_error_response(__oapi_error)
        __oapi_request = _contracts_ListCats.Request(limit=__oapi_parameter_limit)
        try:
            __oapi_result = await __oapi_handler.list_cats(__oapi_request)
        except _contracts_ListCats.NotFound as __oapi_error:
            __oapi_result = __oapi_error
        else:
            if isinstance(__oapi_result, Exception):
                raise TypeError("listCats error responses must be raised")
        if isinstance(__oapi_result, _contracts_ListCats.Ok):
            __oapi_body = convert(
                to_builtins(__oapi_result.body), type=_list_cats__200_decoder.type
            )
            __oapi_content = _encoder.encode(__oapi_body)
            return Response(__oapi_content, status_code=200, media_type="application/json")
        if isinstance(__oapi_result, _contracts_ListCats.NotFound):
            __oapi_body = convert(
                to_builtins(__oapi_result.body), type=_list_cats__404_decoder.type
            )
            __oapi_content = _encoder.encode(__oapi_body)
            return Response(__oapi_content, status_code=404, media_type="application/json")
        raise TypeError("listCats returned an unsupported response variant")

    __oapi_route_list_cats = Route(
        prefix + "/cats", __oapi_endpoint_list_cats, methods=["GET"], name="listCats"
    )
    __oapi_route_list_cats.methods = {"GET"}
    __oapi_routes[("/cats", "get")] = __oapi_route_list_cats

    async def __oapi_endpoint_create_cat(__oapi_http: Request) -> Response:
        try:
            __oapi_bytes = await __oapi_http.body()
            if __oapi_bytes:
                _runtime_check_json_content_type(__oapi_http.headers.get("content-type"))
            __oapi_body = _runtime_json_body(__oapi_bytes, _create_cat__body_decoder, required=True)
        except _runtime_RequestError as __oapi_error:
            return _runtime_error_response(__oapi_error)
        __oapi_request = _contracts_CreateCat.Request(body=__oapi_body)
        __oapi_result = await __oapi_handler.create_cat(__oapi_request)
        if isinstance(__oapi_result, _contracts_CreateCat.Created):
            return Response(status_code=201)
        raise TypeError("createCat returned an unsupported response variant")

    __oapi_route_create_cat = Route(
        prefix + "/cats", __oapi_endpoint_create_cat, methods=["POST"], name="createCat"
    )
    __oapi_routes[("/cats", "post")] = __oapi_route_create_cat

    async def __oapi_endpoint_show_cat_by_id(__oapi_http: Request) -> Response:
        try:
            __oapi_parameter_cat_id = _runtime_parameter(
                __oapi_http.path_params.get("cat_id"),
                _show_cat_by_id__parameter_cat_id,
                ("path", "catId"),
                required=True,
                default=None,
            )
        except _runtime_RequestError as __oapi_error:
            return _runtime_error_response(__oapi_error)
        __oapi_request = _contracts_ShowCatById.Request(cat_id=__oapi_parameter_cat_id)
        try:
            __oapi_result = await __oapi_handler.show_cat_by_id(__oapi_request)
        except _contracts_ShowCatById.NotFound as __oapi_error:
            __oapi_result = __oapi_error
        else:
            if isinstance(__oapi_result, Exception):
                raise TypeError("showCatById error responses must be raised")
        if isinstance(__oapi_result, _contracts_ShowCatById.Ok):
            __oapi_body = convert(
                to_builtins(__oapi_result.body), type=_show_cat_by_id__200_decoder.type
            )
            __oapi_content = _encoder.encode(__oapi_body)
            return Response(__oapi_content, status_code=200, media_type="application/json")
        if isinstance(__oapi_result, _contracts_ShowCatById.NotFound):
            __oapi_body = convert(
                to_builtins(__oapi_result.body), type=_show_cat_by_id__404_decoder.type
            )
            __oapi_content = _encoder.encode(__oapi_body)
            return Response(__oapi_content, status_code=404, media_type="application/json")
        raise TypeError("showCatById returned an unsupported response variant")

    __oapi_route_show_cat_by_id = Route(
        prefix + "/cats/{cat_id}",
        __oapi_endpoint_show_cat_by_id,
        methods=["GET"],
        name="showCatById",
    )
    __oapi_route_show_cat_by_id.methods = {"GET"}
    __oapi_routes[("/cats/{catId}", "get")] = __oapi_route_show_cat_by_id
    return __oapi_routes
