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

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.convert_to_workflow_response import ConvertToWorkflowResponse
from ..types.detach_workflow_response import DetachWorkflowResponse
from ..types.workflow_response import WorkflowResponse

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


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

    def convert_assistant_to_workflow(
        self,
        assistant_id: str,
        *,
        workflow_name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ConvertToWorkflowResponse]:
        """
        Create a workflow from an assistant's settings and link it to the assistant.

        Parameters
        ----------
        assistant_id : str

        workflow_name : typing.Optional[str]
            Workflow name (defaults to '{assistant.name} Workflow')

        description : typing.Optional[str]
            Workflow description

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

        Returns
        -------
        HttpResponse[ConvertToWorkflowResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/assistants/{jsonable_encoder(assistant_id)}/convert-to-workflow",
            method="POST",
            json={
                "workflow_name": workflow_name,
                "description": description,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ConvertToWorkflowResponse,
                    parse_obj_as(
                        type_=ConvertToWorkflowResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get_assistant_workflow(
        self, assistant_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[WorkflowResponse]:
        """
        Retrieve the workflow linked to an assistant.

        Parameters
        ----------
        assistant_id : str

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

        Returns
        -------
        HttpResponse[WorkflowResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/assistants/{jsonable_encoder(assistant_id)}/workflow",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowResponse,
                    parse_obj_as(
                        type_=WorkflowResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def detach_assistant_workflow(
        self, assistant_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DetachWorkflowResponse]:
        """
        Unlink the workflow from an assistant without deleting the workflow itself.

        Parameters
        ----------
        assistant_id : str

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

        Returns
        -------
        HttpResponse[DetachWorkflowResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/assistants/{jsonable_encoder(assistant_id)}/workflow",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DetachWorkflowResponse,
                    parse_obj_as(
                        type_=DetachWorkflowResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def update_assistant_workflow(
        self,
        assistant_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        definition: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[WorkflowResponse]:
        """
        Update the workflow definition or metadata linked to an assistant.

        Parameters
        ----------
        assistant_id : str

        name : typing.Optional[str]

        description : typing.Optional[str]

        definition : typing.Optional[typing.Dict[str, typing.Any]]
            Updated workflow definition

        is_active : typing.Optional[bool]
            Active status

        external_id : typing.Optional[str]
            Flow-plate internal flow ID for correlation

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

        Returns
        -------
        HttpResponse[WorkflowResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/assistants/{jsonable_encoder(assistant_id)}/workflow",
            method="PATCH",
            json={
                "name": name,
                "description": description,
                "definition": definition,
                "is_active": is_active,
                "external_id": external_id,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowResponse,
                    parse_obj_as(
                        type_=WorkflowResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


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

    async def convert_assistant_to_workflow(
        self,
        assistant_id: str,
        *,
        workflow_name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ConvertToWorkflowResponse]:
        """
        Create a workflow from an assistant's settings and link it to the assistant.

        Parameters
        ----------
        assistant_id : str

        workflow_name : typing.Optional[str]
            Workflow name (defaults to '{assistant.name} Workflow')

        description : typing.Optional[str]
            Workflow description

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

        Returns
        -------
        AsyncHttpResponse[ConvertToWorkflowResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/assistants/{jsonable_encoder(assistant_id)}/convert-to-workflow",
            method="POST",
            json={
                "workflow_name": workflow_name,
                "description": description,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ConvertToWorkflowResponse,
                    parse_obj_as(
                        type_=ConvertToWorkflowResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get_assistant_workflow(
        self, assistant_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[WorkflowResponse]:
        """
        Retrieve the workflow linked to an assistant.

        Parameters
        ----------
        assistant_id : str

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

        Returns
        -------
        AsyncHttpResponse[WorkflowResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/assistants/{jsonable_encoder(assistant_id)}/workflow",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowResponse,
                    parse_obj_as(
                        type_=WorkflowResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def detach_assistant_workflow(
        self, assistant_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DetachWorkflowResponse]:
        """
        Unlink the workflow from an assistant without deleting the workflow itself.

        Parameters
        ----------
        assistant_id : str

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

        Returns
        -------
        AsyncHttpResponse[DetachWorkflowResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/assistants/{jsonable_encoder(assistant_id)}/workflow",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DetachWorkflowResponse,
                    parse_obj_as(
                        type_=DetachWorkflowResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def update_assistant_workflow(
        self,
        assistant_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        definition: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[WorkflowResponse]:
        """
        Update the workflow definition or metadata linked to an assistant.

        Parameters
        ----------
        assistant_id : str

        name : typing.Optional[str]

        description : typing.Optional[str]

        definition : typing.Optional[typing.Dict[str, typing.Any]]
            Updated workflow definition

        is_active : typing.Optional[bool]
            Active status

        external_id : typing.Optional[str]
            Flow-plate internal flow ID for correlation

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

        Returns
        -------
        AsyncHttpResponse[WorkflowResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/assistants/{jsonable_encoder(assistant_id)}/workflow",
            method="PATCH",
            json={
                "name": name,
                "description": description,
                "definition": definition,
                "is_active": is_active,
                "external_id": external_id,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowResponse,
                    parse_obj_as(
                        type_=WorkflowResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
