"""
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, Dict, Union
from uuid import UUID

import httpx

from ...client import _client_context
from ...errors import get_error_for_response
from ...models.add_memory_recommendations import AddMemoryRecommendations
from ...types import UNSET, Response, Unset


def _get_kwargs(
    prediction_id: UUID,
    *,
    num_memories: Union[Unset, int] = 3,
    refresh: Union[Unset, bool] = False,
) -> dict[str, Any]:
    params: Dict[str, Any] = {}

    params["num_memories"] = num_memories

    params["refresh"] = refresh

    params = {k: v for k, v in params.items() if v is not UNSET and v is not None}

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": f"/telemetry/prediction/{prediction_id}/memory-suggestions",
        "params": params,
    }

    return _kwargs


def _parse_response(*, response: httpx.Response) -> AddMemoryRecommendations:
    if response.status_code == 200:
        response_200 = AddMemoryRecommendations.from_dict(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)
    if response.status_code == 409:
        raise get_error_for_response(response)
    else:
        raise get_error_for_response(response)


def _build_response(*, response: httpx.Response) -> Response[AddMemoryRecommendations]:
    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,
    *,
    num_memories: Union[Unset, int] = 3,
    refresh: Union[Unset, bool] = False,
) -> Response[AddMemoryRecommendations]:
    """Generate Memory Suggestions

     Generate synthetic memory suggestions to improve a specific prediction.

    The returned suggestions have labels as string representations of integer indices
    corresponding to the memoryset's label_names.

    Params:
        prediction_id: ID of the prediction to generate suggestions for
        num_memories: Number of memory suggestions to generate (default: 3)
        refresh: Force the explanation agent to re-run even if a cached explanation exists

    Args:
        prediction_id (UUID):
        num_memories (Union[Unset, int]):  Default: 3.
        refresh (Union[Unset, bool]):  Default: False.

    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[AddMemoryRecommendations]
    """

    kwargs = _get_kwargs(
        prediction_id=prediction_id,
        num_memories=num_memories,
        refresh=refresh,
    )

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

        return _build_response(response=response)


def sync(
    prediction_id: UUID,
    *,
    num_memories: Union[Unset, int] = 3,
    refresh: Union[Unset, bool] = False,
) -> AddMemoryRecommendations:
    """Generate Memory Suggestions

     Generate synthetic memory suggestions to improve a specific prediction.

    The returned suggestions have labels as string representations of integer indices
    corresponding to the memoryset's label_names.

    Params:
        prediction_id: ID of the prediction to generate suggestions for
        num_memories: Number of memory suggestions to generate (default: 3)
        refresh: Force the explanation agent to re-run even if a cached explanation exists

    Args:
        prediction_id (UUID):
        num_memories (Union[Unset, int]):  Default: 3.
        refresh (Union[Unset, bool]):  Default: False.

    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:
        AddMemoryRecommendations
    """

    return sync_detailed(
        prediction_id=prediction_id,
        num_memories=num_memories,
        refresh=refresh,
    ).parsed


async def asyncio_detailed(
    prediction_id: UUID,
    *,
    num_memories: Union[Unset, int] = 3,
    refresh: Union[Unset, bool] = False,
) -> Response[AddMemoryRecommendations]:
    """Generate Memory Suggestions

     Generate synthetic memory suggestions to improve a specific prediction.

    The returned suggestions have labels as string representations of integer indices
    corresponding to the memoryset's label_names.

    Params:
        prediction_id: ID of the prediction to generate suggestions for
        num_memories: Number of memory suggestions to generate (default: 3)
        refresh: Force the explanation agent to re-run even if a cached explanation exists

    Args:
        prediction_id (UUID):
        num_memories (Union[Unset, int]):  Default: 3.
        refresh (Union[Unset, bool]):  Default: False.

    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[AddMemoryRecommendations]
    """

    kwargs = _get_kwargs(
        prediction_id=prediction_id,
        num_memories=num_memories,
        refresh=refresh,
    )

    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,
    *,
    num_memories: Union[Unset, int] = 3,
    refresh: Union[Unset, bool] = False,
) -> AddMemoryRecommendations:
    """Generate Memory Suggestions

     Generate synthetic memory suggestions to improve a specific prediction.

    The returned suggestions have labels as string representations of integer indices
    corresponding to the memoryset's label_names.

    Params:
        prediction_id: ID of the prediction to generate suggestions for
        num_memories: Number of memory suggestions to generate (default: 3)
        refresh: Force the explanation agent to re-run even if a cached explanation exists

    Args:
        prediction_id (UUID):
        num_memories (Union[Unset, int]):  Default: 3.
        refresh (Union[Unset, bool]):  Default: False.

    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:
        AddMemoryRecommendations
    """

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