# This file was auto-generated by Fern from our API Definition.

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.utterance import Utterance
from ..core.pydantic_utilities import parse_obj_as
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..types.utterance_field import UtteranceField
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.jsonable_encoder import jsonable_encoder
from ..errors.not_found_error import NotFoundError
from ..types.utterance_tag_update_params import UtteranceTagUpdateParams
from ..core.client_wrapper import AsyncClientWrapper

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class UtterancesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def list(
        self,
        *,
        tags: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[Utterance]:
        """
        Parameters
        ----------
        tags : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter utterances by tags

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.List[Utterance]
            List of all utterances in the system

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.utterances.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "utterances",
            method="GET",
            params={
                "tags": tags,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Utterance],
                    parse_obj_as(
                        type_=typing.List[Utterance],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def create(
        self,
        *,
        value: str,
        fields: typing.Sequence[UtteranceField],
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Utterance:
        """
        Parameters
        ----------
        value : str
            The textual content of the utterance.

        fields : typing.Sequence[UtteranceField]
            A sequence of utterance fields associated with the utterance.

        tags : typing.Optional[typing.Sequence[str]]
            Collection of tag IDs associated with the utterance.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        Utterance
            Utterance successfully created.

        Examples
        --------
        from parlant.client import ParlantClient, UtteranceField

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.utterances.create(
            value="Your account balance is {balance}",
            fields=[
                UtteranceField(
                    name="balance",
                    description="Account's balance",
                    examples=["9000"],
                )
            ],
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "utterances",
            method="POST",
            json={
                "value": value,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields,
                    annotation=typing.Sequence[UtteranceField],
                    direction="write",
                ),
                "tags": tags,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Utterance,
                    parse_obj_as(
                        type_=Utterance,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def retrieve(
        self,
        utterance_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Utterance:
        """
        Retrieves details of a specific utterance by ID.

        Parameters
        ----------
        utterance_id : str

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        Utterance
            Utterance details successfully retrieved. Returns the Utterance object.

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.utterances.retrieve(
            utterance_id="t9a8g703f4",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"utterances/{jsonable_encoder(utterance_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Utterance,
                    parse_obj_as(
                        type_=Utterance,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def delete(
        self,
        utterance_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Parameters
        ----------
        utterance_id : str

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        None

        Examples
        --------
        from parlant.client import ParlantClient

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.utterances.delete(
            utterance_id="t9a8g703f4",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"utterances/{jsonable_encoder(utterance_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def update(
        self,
        utterance_id: str,
        *,
        value: typing.Optional[str] = OMIT,
        fields: typing.Optional[typing.Sequence[UtteranceField]] = OMIT,
        tags: typing.Optional[UtteranceTagUpdateParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Utterance:
        """
        Updates an existing utterance's attributes.

        Only provided attributes will be updated; others remain unchanged.
        The utterance's ID and creation timestamp cannot be modified.
        Extra metadata and tags can be added or removed independently.

        Parameters
        ----------
        utterance_id : str

        value : typing.Optional[str]
            The textual content of the utterance.

        fields : typing.Optional[typing.Sequence[UtteranceField]]
            A sequence of utterance fields associated with the utterance.

        tags : typing.Optional[UtteranceTagUpdateParams]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        Utterance
            Utterance successfully updated. Returns the updated Utterance object.

        Examples
        --------
        from parlant.client import ParlantClient, UtteranceField

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.utterances.update(
            utterance_id="t9a8g703f4",
            value="Your updated balance is {balance}",
            fields=[
                UtteranceField(
                    name="balance",
                    description="Updated account balance",
                    examples=["10000"],
                )
            ],
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"utterances/{jsonable_encoder(utterance_id)}",
            method="PATCH",
            json={
                "value": value,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields,
                    annotation=typing.Sequence[UtteranceField],
                    direction="write",
                ),
                "tags": convert_and_respect_annotation_metadata(
                    object_=tags, annotation=UtteranceTagUpdateParams, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Utterance,
                    parse_obj_as(
                        type_=Utterance,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


class AsyncUtterancesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        *,
        tags: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[Utterance]:
        """
        Parameters
        ----------
        tags : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter utterances by tags

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.List[Utterance]
            List of all utterances in the system

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

        client = AsyncParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.utterances.list()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "utterances",
            method="GET",
            params={
                "tags": tags,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Utterance],
                    parse_obj_as(
                        type_=typing.List[Utterance],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def create(
        self,
        *,
        value: str,
        fields: typing.Sequence[UtteranceField],
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Utterance:
        """
        Parameters
        ----------
        value : str
            The textual content of the utterance.

        fields : typing.Sequence[UtteranceField]
            A sequence of utterance fields associated with the utterance.

        tags : typing.Optional[typing.Sequence[str]]
            Collection of tag IDs associated with the utterance.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        Utterance
            Utterance successfully created.

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient, UtteranceField

        client = AsyncParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.utterances.create(
                value="Your account balance is {balance}",
                fields=[
                    UtteranceField(
                        name="balance",
                        description="Account's balance",
                        examples=["9000"],
                    )
                ],
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "utterances",
            method="POST",
            json={
                "value": value,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields,
                    annotation=typing.Sequence[UtteranceField],
                    direction="write",
                ),
                "tags": tags,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Utterance,
                    parse_obj_as(
                        type_=Utterance,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def retrieve(
        self,
        utterance_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Utterance:
        """
        Retrieves details of a specific utterance by ID.

        Parameters
        ----------
        utterance_id : str

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        Utterance
            Utterance details successfully retrieved. Returns the Utterance object.

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

        client = AsyncParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.utterances.retrieve(
                utterance_id="t9a8g703f4",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"utterances/{jsonable_encoder(utterance_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Utterance,
                    parse_obj_as(
                        type_=Utterance,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def delete(
        self,
        utterance_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Parameters
        ----------
        utterance_id : str

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

        client = AsyncParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.utterances.delete(
                utterance_id="t9a8g703f4",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"utterances/{jsonable_encoder(utterance_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def update(
        self,
        utterance_id: str,
        *,
        value: typing.Optional[str] = OMIT,
        fields: typing.Optional[typing.Sequence[UtteranceField]] = OMIT,
        tags: typing.Optional[UtteranceTagUpdateParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Utterance:
        """
        Updates an existing utterance's attributes.

        Only provided attributes will be updated; others remain unchanged.
        The utterance's ID and creation timestamp cannot be modified.
        Extra metadata and tags can be added or removed independently.

        Parameters
        ----------
        utterance_id : str

        value : typing.Optional[str]
            The textual content of the utterance.

        fields : typing.Optional[typing.Sequence[UtteranceField]]
            A sequence of utterance fields associated with the utterance.

        tags : typing.Optional[UtteranceTagUpdateParams]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        Utterance
            Utterance successfully updated. Returns the updated Utterance object.

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient, UtteranceField

        client = AsyncParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.utterances.update(
                utterance_id="t9a8g703f4",
                value="Your updated balance is {balance}",
                fields=[
                    UtteranceField(
                        name="balance",
                        description="Updated account balance",
                        examples=["10000"],
                    )
                ],
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"utterances/{jsonable_encoder(utterance_id)}",
            method="PATCH",
            json={
                "value": value,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields,
                    annotation=typing.Sequence[UtteranceField],
                    direction="write",
                ),
                "tags": convert_and_respect_annotation_metadata(
                    object_=tags, annotation=UtteranceTagUpdateParams, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Utterance,
                    parse_obj_as(
                        type_=Utterance,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
