"""
This file is generated by the openapi-python-client tool via the generate_api_client.py script

It is a customized template from the openapi-python-client tool's default template:
https://github.com/openapi-generators/openapi-python-client/blob/861ef5622f10fc96d240dc9becb0edf94e61446c/openapi_python_client/templates/endpoint_module.py.jinja

The main changes are:
- Update the API call responses to either return the successful response type or raise an error by:
    - Updating the _parse_response function to raise an error if the response status code is not in the 2xx range
- Inject a client into every method via a context manager
"""

from http import HTTPStatus
from typing import Any, Union
from uuid import UUID

import httpx

from ...client import _client_context
from ...errors import get_error_for_response
from ...models.label_prediction_with_memories_and_feedback import LabelPredictionWithMemoriesAndFeedback
from ...models.score_prediction_with_memories_and_feedback import ScorePredictionWithMemoriesAndFeedback
from ...types import Response


def _get_kwargs(
    prediction_id: UUID,
) -> dict[str, Any]:
    _kwargs: dict[str, Any] = {
        "method": "get",
        "url": f"/telemetry/prediction/{prediction_id}",
    }

    return _kwargs


def _parse_response(
    *, response: httpx.Response
) -> Union["LabelPredictionWithMemoriesAndFeedback", "ScorePredictionWithMemoriesAndFeedback"]:
    if response.status_code == 200:

        def _parse_response_200(
            data: object,
        ) -> Union["LabelPredictionWithMemoriesAndFeedback", "ScorePredictionWithMemoriesAndFeedback"]:
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                response_200_type_0 = LabelPredictionWithMemoriesAndFeedback.from_dict(data)

                return response_200_type_0
            except:  # noqa: E722
                pass
            if not isinstance(data, dict):
                raise TypeError()
            response_200_type_1 = ScorePredictionWithMemoriesAndFeedback.from_dict(data)

            return response_200_type_1

        response_200 = _parse_response_200(response.json())

        return response_200
    if response.status_code == 401:
        raise get_error_for_response(response)
    if response.status_code == 500:
        raise get_error_for_response(response)
    if response.status_code == 503:
        raise get_error_for_response(response)
    if response.status_code == 404:
        raise get_error_for_response(response)
    else:
        raise get_error_for_response(response)


def _build_response(
    *, response: httpx.Response
) -> Response[Union["LabelPredictionWithMemoriesAndFeedback", "ScorePredictionWithMemoriesAndFeedback"]]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(response=response),
    )


def sync_detailed(
    prediction_id: UUID,
) -> Response[Union["LabelPredictionWithMemoriesAndFeedback", "ScorePredictionWithMemoriesAndFeedback"]]:
    """Get Prediction

    Args:
        prediction_id (UUID):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[Union['LabelPredictionWithMemoriesAndFeedback', 'ScorePredictionWithMemoriesAndFeedback']]
    """

    kwargs = _get_kwargs(
        prediction_id=prediction_id,
    )

    with _client_context() as client:
        response = client.get_httpx_client().request(
            **kwargs,
        )

        return _build_response(response=response)


def sync(
    prediction_id: UUID,
) -> Union["LabelPredictionWithMemoriesAndFeedback", "ScorePredictionWithMemoriesAndFeedback"]:
    """Get Prediction

    Args:
        prediction_id (UUID):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Union['LabelPredictionWithMemoriesAndFeedback', 'ScorePredictionWithMemoriesAndFeedback']
    """

    return sync_detailed(
        prediction_id=prediction_id,
    ).parsed


async def asyncio_detailed(
    prediction_id: UUID,
) -> Response[Union["LabelPredictionWithMemoriesAndFeedback", "ScorePredictionWithMemoriesAndFeedback"]]:
    """Get Prediction

    Args:
        prediction_id (UUID):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[Union['LabelPredictionWithMemoriesAndFeedback', 'ScorePredictionWithMemoriesAndFeedback']]
    """

    kwargs = _get_kwargs(
        prediction_id=prediction_id,
    )

    with _client_context() as client:
        response = await client.get_async_httpx_client().request(**kwargs)

    return _build_response(response=response)


async def asyncio(
    prediction_id: UUID,
) -> Union["LabelPredictionWithMemoriesAndFeedback", "ScorePredictionWithMemoriesAndFeedback"]:
    """Get Prediction

    Args:
        prediction_id (UUID):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Union['LabelPredictionWithMemoriesAndFeedback', 'ScorePredictionWithMemoriesAndFeedback']
    """

    return (
        await asyncio_detailed(
            prediction_id=prediction_id,
        )
    ).parsed
