# 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.canned_response import CannedResponse
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.canned_response_field import CannedResponseField
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.canned_response_tag_update_params import CannedResponseTagUpdateParams
from ..types.canned_response_metadata_update_params import (
    CannedResponseMetadataUpdateParams,
)
from ..core.client_wrapper import AsyncClientWrapper

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


class CannedResponsesClient:
    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[CannedResponse]:
        """
        Lists all canned responses, optionally filtered by tags.

        Parameters
        ----------
        tags : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter canned responses by tags

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

        Returns
        -------
        typing.List[CannedResponse]
            List of all canned responses in the system

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.canned_responses.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "canned_responses",
            method="GET",
            params={
                "tags": tags,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[CannedResponse],
                    parse_obj_as(
                        type_=typing.List[CannedResponse],  # 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[CannedResponseField],
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        signals: typing.Optional[typing.Sequence[str]] = OMIT,
        metadata: typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CannedResponse:
        """
        Parameters
        ----------
        value : str
            The textual content of the canned response.

        fields : typing.Sequence[CannedResponseField]
            A sequence of canned response fields associated with the canned response.

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

        signals : typing.Optional[typing.Sequence[str]]
            A sequence of signals associated with the canned response, to help with filtering and matching.

        metadata : typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]]
            Additional metadata associated with the canned response.

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

        Returns
        -------
        CannedResponse
            CannedResponse successfully created.

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.canned_responses.create(
            value="Your account balance is {balance}",
            fields=[
                CannedResponseField(
                    name="balance",
                    description="Account's balance",
                    examples=["9000"],
                )
            ],
            metadata={"category": "account", "priority": 1},
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "canned_responses",
            method="POST",
            json={
                "value": value,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields,
                    annotation=typing.Sequence[CannedResponseField],
                    direction="write",
                ),
                "tags": tags,
                "signals": signals,
                "metadata": metadata,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    CannedResponse,
                    parse_obj_as(
                        type_=CannedResponse,  # 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,
        canned_response_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CannedResponse:
        """
        Retrieves details of a specific canned response by ID.

        Parameters
        ----------
        canned_response_id : str

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

        Returns
        -------
        CannedResponse
            Canned response details successfully retrieved. Returns the CannedResponse object.

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.canned_responses.retrieve(
            canned_response_id="t9a8g703f4",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"canned_responses/{jsonable_encoder(canned_response_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    CannedResponse,
                    parse_obj_as(
                        type_=CannedResponse,  # 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,
        canned_response_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Parameters
        ----------
        canned_response_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.canned_responses.delete(
            canned_response_id="t9a8g703f4",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"canned_responses/{jsonable_encoder(canned_response_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,
        canned_response_id: str,
        *,
        value: typing.Optional[str] = OMIT,
        fields: typing.Optional[typing.Sequence[CannedResponseField]] = OMIT,
        tags: typing.Optional[CannedResponseTagUpdateParams] = OMIT,
        metadata: typing.Optional[CannedResponseMetadataUpdateParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CannedResponse:
        """
        Updates an existing canned response's attributes.

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

        Parameters
        ----------
        canned_response_id : str

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

        fields : typing.Optional[typing.Sequence[CannedResponseField]]
            A sequence of canned response fields associated with the canned response.

        tags : typing.Optional[CannedResponseTagUpdateParams]

        metadata : typing.Optional[CannedResponseMetadataUpdateParams]

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

        Returns
        -------
        CannedResponse
            Canned response successfully updated. Returns the updated CannedResponse object.

        Examples
        --------
        from parlant.client import (
            CannedResponseField,
            CannedResponseMetadataUpdateParams,
            ParlantClient,
        )

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.canned_responses.update(
            canned_response_id="t9a8g703f4",
            value="Your updated balance is {balance}",
            fields=[
                CannedResponseField(
                    name="balance",
                    description="Updated account balance",
                    examples=["10000"],
                )
            ],
            metadata=CannedResponseMetadataUpdateParams(
                set_={"category": "account", "priority": 2},
                unset=["old_field"],
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"canned_responses/{jsonable_encoder(canned_response_id)}",
            method="PATCH",
            json={
                "value": value,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields,
                    annotation=typing.Sequence[CannedResponseField],
                    direction="write",
                ),
                "tags": convert_and_respect_annotation_metadata(
                    object_=tags,
                    annotation=CannedResponseTagUpdateParams,
                    direction="write",
                ),
                "metadata": convert_and_respect_annotation_metadata(
                    object_=metadata,
                    annotation=CannedResponseMetadataUpdateParams,
                    direction="write",
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    CannedResponse,
                    parse_obj_as(
                        type_=CannedResponse,  # 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 AsyncCannedResponsesClient:
    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[CannedResponse]:
        """
        Lists all canned responses, optionally filtered by tags.

        Parameters
        ----------
        tags : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter canned responses by tags

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

        Returns
        -------
        typing.List[CannedResponse]
            List of all canned responses 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.canned_responses.list()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "canned_responses",
            method="GET",
            params={
                "tags": tags,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[CannedResponse],
                    parse_obj_as(
                        type_=typing.List[CannedResponse],  # 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[CannedResponseField],
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        signals: typing.Optional[typing.Sequence[str]] = OMIT,
        metadata: typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CannedResponse:
        """
        Parameters
        ----------
        value : str
            The textual content of the canned response.

        fields : typing.Sequence[CannedResponseField]
            A sequence of canned response fields associated with the canned response.

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

        signals : typing.Optional[typing.Sequence[str]]
            A sequence of signals associated with the canned response, to help with filtering and matching.

        metadata : typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]]
            Additional metadata associated with the canned response.

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

        Returns
        -------
        CannedResponse
            CannedResponse successfully created.

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient, CannedResponseField

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


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


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "canned_responses",
            method="POST",
            json={
                "value": value,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields,
                    annotation=typing.Sequence[CannedResponseField],
                    direction="write",
                ),
                "tags": tags,
                "signals": signals,
                "metadata": metadata,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    CannedResponse,
                    parse_obj_as(
                        type_=CannedResponse,  # 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,
        canned_response_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CannedResponse:
        """
        Retrieves details of a specific canned response by ID.

        Parameters
        ----------
        canned_response_id : str

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

        Returns
        -------
        CannedResponse
            Canned response details successfully retrieved. Returns the CannedResponse 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.canned_responses.retrieve(
                canned_response_id="t9a8g703f4",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"canned_responses/{jsonable_encoder(canned_response_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    CannedResponse,
                    parse_obj_as(
                        type_=CannedResponse,  # 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,
        canned_response_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Parameters
        ----------
        canned_response_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.canned_responses.delete(
                canned_response_id="t9a8g703f4",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"canned_responses/{jsonable_encoder(canned_response_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,
        canned_response_id: str,
        *,
        value: typing.Optional[str] = OMIT,
        fields: typing.Optional[typing.Sequence[CannedResponseField]] = OMIT,
        tags: typing.Optional[CannedResponseTagUpdateParams] = OMIT,
        metadata: typing.Optional[CannedResponseMetadataUpdateParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CannedResponse:
        """
        Updates an existing canned response's attributes.

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

        Parameters
        ----------
        canned_response_id : str

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

        fields : typing.Optional[typing.Sequence[CannedResponseField]]
            A sequence of canned response fields associated with the canned response.

        tags : typing.Optional[CannedResponseTagUpdateParams]

        metadata : typing.Optional[CannedResponseMetadataUpdateParams]

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

        Returns
        -------
        CannedResponse
            Canned response successfully updated. Returns the updated CannedResponse object.

        Examples
        --------
        import asyncio

        from parlant.client import (
            AsyncParlantClient,
            CannedResponseField,
            CannedResponseMetadataUpdateParams,
        )

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


        async def main() -> None:
            await client.canned_responses.update(
                canned_response_id="t9a8g703f4",
                value="Your updated balance is {balance}",
                fields=[
                    CannedResponseField(
                        name="balance",
                        description="Updated account balance",
                        examples=["10000"],
                    )
                ],
                metadata=CannedResponseMetadataUpdateParams(
                    set_={"category": "account", "priority": 2},
                    unset=["old_field"],
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"canned_responses/{jsonable_encoder(canned_response_id)}",
            method="PATCH",
            json={
                "value": value,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields,
                    annotation=typing.Sequence[CannedResponseField],
                    direction="write",
                ),
                "tags": convert_and_respect_annotation_metadata(
                    object_=tags,
                    annotation=CannedResponseTagUpdateParams,
                    direction="write",
                ),
                "metadata": convert_and_respect_annotation_metadata(
                    object_=metadata,
                    annotation=CannedResponseMetadataUpdateParams,
                    direction="write",
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    CannedResponse,
                    parse_obj_as(
                        type_=CannedResponse,  # 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)
