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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..types.relationship_kind_dto import RelationshipKindDto
from ..core.request_options import RequestOptions
from ..types.relationship import Relationship
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.tool_id import ToolId
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.jsonable_encoder import jsonable_encoder
from ..errors.not_found_error import NotFoundError
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def list(
        self,
        *,
        kind: typing.Optional[RelationshipKindDto] = None,
        indirect: typing.Optional[bool] = None,
        guideline_id: typing.Optional[str] = None,
        tag_id: typing.Optional[str] = None,
        tool_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[Relationship]:
        """
        List relationships.

        Either `guideline_id` or `tag_id` or `tool_id` must be provided.

        Parameters
        ----------
        kind : typing.Optional[RelationshipKindDto]

        indirect : typing.Optional[bool]
            Whether to include indirect relationships

        guideline_id : typing.Optional[str]

        tag_id : typing.Optional[str]

        tool_id : typing.Optional[str]

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

        Returns
        -------
        typing.List[Relationship]
            Relationships successfully retrieved. Returns a list of all relationships.

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.relationships.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "relationships",
            method="GET",
            params={
                "kind": kind,
                "indirect": indirect,
                "guideline_id": guideline_id,
                "tag_id": tag_id,
                "tool_id": tool_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Relationship],
                    parse_obj_as(
                        type_=typing.List[Relationship],  # 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,
        *,
        kind: RelationshipKindDto,
        source_guideline: typing.Optional[str] = OMIT,
        source_tag: typing.Optional[str] = OMIT,
        source_tool: typing.Optional[ToolId] = OMIT,
        target_guideline: typing.Optional[str] = OMIT,
        target_tag: typing.Optional[str] = OMIT,
        target_tool: typing.Optional[ToolId] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Relationship:
        """
        Create a relationship.

        A relationship is a relationship between a guideline and a tag.
        It can be created between a guideline and a tag, or between two guidelines, or between two tags.

        Parameters
        ----------
        kind : RelationshipKindDto

        source_guideline : typing.Optional[str]
            Unique identifier for the guideline

        source_tag : typing.Optional[str]
            Unique identifier for the tag

        source_tool : typing.Optional[ToolId]

        target_guideline : typing.Optional[str]
            Unique identifier for the guideline

        target_tag : typing.Optional[str]
            Unique identifier for the tag

        target_tool : typing.Optional[ToolId]

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

        Returns
        -------
        Relationship
            Relationship successfully created. Returns the created relationship.

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.relationships.create(
            source_guideline="gid_123",
            target_tag="tid_456",
            kind="entailment",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "relationships",
            method="POST",
            json={
                "source_guideline": source_guideline,
                "source_tag": source_tag,
                "source_tool": convert_and_respect_annotation_metadata(
                    object_=source_tool, annotation=ToolId, direction="write"
                ),
                "target_guideline": target_guideline,
                "target_tag": target_tag,
                "target_tool": convert_and_respect_annotation_metadata(
                    object_=target_tool, annotation=ToolId, direction="write"
                ),
                "kind": kind,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Relationship,
                    parse_obj_as(
                        type_=Relationship,  # 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,
        relationship_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Relationship:
        """
        Read a relationship by ID.

        Parameters
        ----------
        relationship_id : str
            identifier of relationship

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

        Returns
        -------
        Relationship
            Relationship successfully retrieved. Returns the requested relationship.

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

        client = ParlantClient(
            base_url="https://yourhost.com/path/to/api",
        )
        client.relationships.retrieve(
            relationship_id="gr_123",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"relationships/{jsonable_encoder(relationship_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Relationship,
                    parse_obj_as(
                        type_=Relationship,  # 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,
        relationship_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Delete a relationship by ID.

        Parameters
        ----------
        relationship_id : str
            identifier of relationship

        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.relationships.delete(
            relationship_id="gr_123",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"relationships/{jsonable_encoder(relationship_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)


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

    async def list(
        self,
        *,
        kind: typing.Optional[RelationshipKindDto] = None,
        indirect: typing.Optional[bool] = None,
        guideline_id: typing.Optional[str] = None,
        tag_id: typing.Optional[str] = None,
        tool_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[Relationship]:
        """
        List relationships.

        Either `guideline_id` or `tag_id` or `tool_id` must be provided.

        Parameters
        ----------
        kind : typing.Optional[RelationshipKindDto]

        indirect : typing.Optional[bool]
            Whether to include indirect relationships

        guideline_id : typing.Optional[str]

        tag_id : typing.Optional[str]

        tool_id : typing.Optional[str]

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

        Returns
        -------
        typing.List[Relationship]
            Relationships successfully retrieved. Returns a list of all relationships.

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


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


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "relationships",
            method="GET",
            params={
                "kind": kind,
                "indirect": indirect,
                "guideline_id": guideline_id,
                "tag_id": tag_id,
                "tool_id": tool_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Relationship],
                    parse_obj_as(
                        type_=typing.List[Relationship],  # 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,
        *,
        kind: RelationshipKindDto,
        source_guideline: typing.Optional[str] = OMIT,
        source_tag: typing.Optional[str] = OMIT,
        source_tool: typing.Optional[ToolId] = OMIT,
        target_guideline: typing.Optional[str] = OMIT,
        target_tag: typing.Optional[str] = OMIT,
        target_tool: typing.Optional[ToolId] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Relationship:
        """
        Create a relationship.

        A relationship is a relationship between a guideline and a tag.
        It can be created between a guideline and a tag, or between two guidelines, or between two tags.

        Parameters
        ----------
        kind : RelationshipKindDto

        source_guideline : typing.Optional[str]
            Unique identifier for the guideline

        source_tag : typing.Optional[str]
            Unique identifier for the tag

        source_tool : typing.Optional[ToolId]

        target_guideline : typing.Optional[str]
            Unique identifier for the guideline

        target_tag : typing.Optional[str]
            Unique identifier for the tag

        target_tool : typing.Optional[ToolId]

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

        Returns
        -------
        Relationship
            Relationship successfully created. Returns the created relationship.

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.relationships.create(
                source_guideline="gid_123",
                target_tag="tid_456",
                kind="entailment",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "relationships",
            method="POST",
            json={
                "source_guideline": source_guideline,
                "source_tag": source_tag,
                "source_tool": convert_and_respect_annotation_metadata(
                    object_=source_tool, annotation=ToolId, direction="write"
                ),
                "target_guideline": target_guideline,
                "target_tag": target_tag,
                "target_tool": convert_and_respect_annotation_metadata(
                    object_=target_tool, annotation=ToolId, direction="write"
                ),
                "kind": kind,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Relationship,
                    parse_obj_as(
                        type_=Relationship,  # 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,
        relationship_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Relationship:
        """
        Read a relationship by ID.

        Parameters
        ----------
        relationship_id : str
            identifier of relationship

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

        Returns
        -------
        Relationship
            Relationship successfully retrieved. Returns the requested relationship.

        Examples
        --------
        import asyncio

        from parlant.client import AsyncParlantClient

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


        async def main() -> None:
            await client.relationships.retrieve(
                relationship_id="gr_123",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"relationships/{jsonable_encoder(relationship_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Relationship,
                    parse_obj_as(
                        type_=Relationship,  # 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,
        relationship_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Delete a relationship by ID.

        Parameters
        ----------
        relationship_id : str
            identifier of relationship

        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.relationships.delete(
                relationship_id="gr_123",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"relationships/{jsonable_encoder(relationship_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)
