# 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.capability import Capability
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 ..core.jsonable_encoder import jsonable_encoder
from ..errors.not_found_error import NotFoundError
from ..types.capability_tag_update_params import CapabilityTagUpdateParams
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def list(
        self,
        *,
        tag_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[Capability]:
        """
        Retrieves a list of all capabilities in the system.

        Returns an empty list if no capabilities exist.
        Capabilities are returned in no guaranteed order.

        Parameters
        ----------
        tag_id : typing.Optional[str]
            The tag ID to filter capabilities by

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

        Returns
        -------
        typing.List[Capability]
            List of all capabilities in the system

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.capabilities.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "capabilities",
            method="GET",
            params={
                "tag_id": tag_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Capability],
                    parse_obj_as(
                        type_=typing.List[Capability],  # 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,
        *,
        title: str,
        description: str,
        signals: typing.Sequence[str],
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Capability:
        """
        Creates a new capability in the system.

        The capability will be initialized with the provided title, description, signals, and optional tags.
        A unique identifier will be automatically generated.

        Default behaviors:
        - `signals` defaults to an empty list if not provided

        Parameters
        ----------
        title : str
            The title of the capability

        description : str
            Detailed description of the capability's purpose

        signals : typing.Sequence[str]
            Example signals that this capability can handle

        tags : typing.Optional[typing.Sequence[str]]
            List of tag IDs associated with the capability

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

        Returns
        -------
        Capability
            Capability successfully created. Returns the complete capability object including generated ID.

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.capabilities.create(
            title="Provide Replacement Phone",
            description="Provide a replacement phone when a customer needs repair for their phone.",
            signals=[
                "My phone is broken",
                "I need a replacement while my phone is being repaired",
            ],
            tags=["tag1", "tag2"],
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "capabilities",
            method="POST",
            json={
                "title": title,
                "description": description,
                "signals": signals,
                "tags": tags,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Capability,
                    parse_obj_as(
                        type_=Capability,  # 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,
        capability_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Capability:
        """
        Retrieves details of a specific capability by ID.

        Returns the complete capability object.

        Parameters
        ----------
        capability_id : str
            Unique identifier for the capability

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

        Returns
        -------
        Capability
            Capability details successfully retrieved. Returns the complete capability object.

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.capabilities.retrieve(
            capability_id="cap_123abc",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"capabilities/{jsonable_encoder(capability_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Capability,
                    parse_obj_as(
                        type_=Capability,  # 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,
        capability_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes a capability from the system.

        Deleting a non-existent capability will return 404.
        No content will be returned from a successful deletion.

        Parameters
        ----------
        capability_id : str
            Unique identifier for the capability

        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.capabilities.delete(
            capability_id="cap_123abc",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"capabilities/{jsonable_encoder(capability_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,
        capability_id: str,
        *,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        signals: typing.Optional[typing.Sequence[str]] = OMIT,
        tags: typing.Optional[CapabilityTagUpdateParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Capability:
        """
        Updates an existing capability's attributes.

        Only the provided attributes will be updated; others will remain unchanged.
        The capability's ID and creation timestamp cannot be modified.

        Parameters
        ----------
        capability_id : str
            Unique identifier for the capability

        title : typing.Optional[str]
            The title of the capability

        description : typing.Optional[str]
            Detailed description of the capability's purpose

        signals : typing.Optional[typing.Sequence[str]]
            Example signals that this capability can handle

        tags : typing.Optional[CapabilityTagUpdateParams]

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

        Returns
        -------
        Capability
            Capability successfully updated. Returns the updated capability.

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.capabilities.update(
            capability_id="cap_123abc",
            title="Provide Replacement Phone",
            description="Provide a replacement phone when a customer needs repair for their phone.",
            signals=[
                "My phone is broken",
                "I need a replacement while my phone is being repaired",
            ],
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"capabilities/{jsonable_encoder(capability_id)}",
            method="PATCH",
            json={
                "title": title,
                "description": description,
                "signals": signals,
                "tags": convert_and_respect_annotation_metadata(
                    object_=tags,
                    annotation=CapabilityTagUpdateParams,
                    direction="write",
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Capability,
                    parse_obj_as(
                        type_=Capability,  # 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 AsyncCapabilitiesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        *,
        tag_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[Capability]:
        """
        Retrieves a list of all capabilities in the system.

        Returns an empty list if no capabilities exist.
        Capabilities are returned in no guaranteed order.

        Parameters
        ----------
        tag_id : typing.Optional[str]
            The tag ID to filter capabilities by

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

        Returns
        -------
        typing.List[Capability]
            List of all capabilities 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.capabilities.list()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "capabilities",
            method="GET",
            params={
                "tag_id": tag_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Capability],
                    parse_obj_as(
                        type_=typing.List[Capability],  # 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,
        *,
        title: str,
        description: str,
        signals: typing.Sequence[str],
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Capability:
        """
        Creates a new capability in the system.

        The capability will be initialized with the provided title, description, signals, and optional tags.
        A unique identifier will be automatically generated.

        Default behaviors:
        - `signals` defaults to an empty list if not provided

        Parameters
        ----------
        title : str
            The title of the capability

        description : str
            Detailed description of the capability's purpose

        signals : typing.Sequence[str]
            Example signals that this capability can handle

        tags : typing.Optional[typing.Sequence[str]]
            List of tag IDs associated with the capability

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

        Returns
        -------
        Capability
            Capability successfully created. Returns the complete capability object including generated ID.

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.capabilities.create(
                title="Provide Replacement Phone",
                description="Provide a replacement phone when a customer needs repair for their phone.",
                signals=[
                    "My phone is broken",
                    "I need a replacement while my phone is being repaired",
                ],
                tags=["tag1", "tag2"],
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "capabilities",
            method="POST",
            json={
                "title": title,
                "description": description,
                "signals": signals,
                "tags": tags,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Capability,
                    parse_obj_as(
                        type_=Capability,  # 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,
        capability_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Capability:
        """
        Retrieves details of a specific capability by ID.

        Returns the complete capability object.

        Parameters
        ----------
        capability_id : str
            Unique identifier for the capability

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

        Returns
        -------
        Capability
            Capability details successfully retrieved. Returns the complete capability 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.capabilities.retrieve(
                capability_id="cap_123abc",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"capabilities/{jsonable_encoder(capability_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Capability,
                    parse_obj_as(
                        type_=Capability,  # 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,
        capability_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes a capability from the system.

        Deleting a non-existent capability will return 404.
        No content will be returned from a successful deletion.

        Parameters
        ----------
        capability_id : str
            Unique identifier for the capability

        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.capabilities.delete(
                capability_id="cap_123abc",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"capabilities/{jsonable_encoder(capability_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,
        capability_id: str,
        *,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        signals: typing.Optional[typing.Sequence[str]] = OMIT,
        tags: typing.Optional[CapabilityTagUpdateParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Capability:
        """
        Updates an existing capability's attributes.

        Only the provided attributes will be updated; others will remain unchanged.
        The capability's ID and creation timestamp cannot be modified.

        Parameters
        ----------
        capability_id : str
            Unique identifier for the capability

        title : typing.Optional[str]
            The title of the capability

        description : typing.Optional[str]
            Detailed description of the capability's purpose

        signals : typing.Optional[typing.Sequence[str]]
            Example signals that this capability can handle

        tags : typing.Optional[CapabilityTagUpdateParams]

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

        Returns
        -------
        Capability
            Capability successfully updated. Returns the updated capability.

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.capabilities.update(
                capability_id="cap_123abc",
                title="Provide Replacement Phone",
                description="Provide a replacement phone when a customer needs repair for their phone.",
                signals=[
                    "My phone is broken",
                    "I need a replacement while my phone is being repaired",
                ],
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"capabilities/{jsonable_encoder(capability_id)}",
            method="PATCH",
            json={
                "title": title,
                "description": description,
                "signals": signals,
                "tags": convert_and_respect_annotation_metadata(
                    object_=tags,
                    annotation=CapabilityTagUpdateParams,
                    direction="write",
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Capability,
                    parse_obj_as(
                        type_=Capability,  # 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)
