# 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.workflow_execution_response import WorkflowExecutionResponse
from ..types.workflow_trigger_response import WorkflowTriggerResponse

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


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

    def list_workflow_triggers(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[typing.List[WorkflowTriggerResponse]]:
        """
        List all workflow triggers configured for the organization.

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

        Returns
        -------
        HttpResponse[typing.List[WorkflowTriggerResponse]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/meeting-buddy/workflow-triggers",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[WorkflowTriggerResponse],
                    parse_obj_as(
                        type_=typing.List[WorkflowTriggerResponse],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 create_workflow_trigger(
        self,
        *,
        name: str,
        event_type: str,
        workflow_id: str,
        conditions: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        is_enabled: typing.Optional[bool] = OMIT,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[WorkflowTriggerResponse]:
        """
        Create a new workflow trigger that fires a FlowPlate workflow when an intelligence event occurs.

        Parameters
        ----------
        name : str

        event_type : str
            Event type that fires this trigger (e.g. intelligence_completed, deal_risk_detected)

        workflow_id : str

        conditions : typing.Optional[typing.Dict[str, typing.Any]]
            Optional conditions for trigger evaluation

        is_enabled : typing.Optional[bool]

        description : typing.Optional[str]

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

        Returns
        -------
        HttpResponse[WorkflowTriggerResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/meeting-buddy/workflow-triggers",
            method="POST",
            json={
                "name": name,
                "event_type": event_type,
                "workflow_id": workflow_id,
                "conditions": conditions,
                "is_enabled": is_enabled,
                "description": description,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowTriggerResponse,
                    parse_obj_as(
                        type_=WorkflowTriggerResponse,  # 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 delete_workflow_trigger(
        self, trigger_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[None]:
        """
        Soft-delete a workflow trigger.

        Parameters
        ----------
        trigger_id : str

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/meeting-buddy/workflow-triggers/{jsonable_encoder(trigger_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            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_workflow_trigger(
        self,
        trigger_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        event_type: typing.Optional[str] = OMIT,
        workflow_id: typing.Optional[str] = OMIT,
        conditions: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        is_enabled: typing.Optional[bool] = OMIT,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[WorkflowTriggerResponse]:
        """
        Update an existing workflow trigger's configuration.

        Parameters
        ----------
        trigger_id : str

        name : typing.Optional[str]

        event_type : typing.Optional[str]

        workflow_id : typing.Optional[str]

        conditions : typing.Optional[typing.Dict[str, typing.Any]]

        is_enabled : typing.Optional[bool]

        description : typing.Optional[str]

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

        Returns
        -------
        HttpResponse[WorkflowTriggerResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/meeting-buddy/workflow-triggers/{jsonable_encoder(trigger_id)}",
            method="PATCH",
            json={
                "name": name,
                "event_type": event_type,
                "workflow_id": workflow_id,
                "conditions": conditions,
                "is_enabled": is_enabled,
                "description": description,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowTriggerResponse,
                    parse_obj_as(
                        type_=WorkflowTriggerResponse,  # 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 list_session_workflow_executions(
        self, session_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[typing.List[WorkflowExecutionResponse]]:
        """
        Get all workflow executions that ran for a specific meeting session.

        Parameters
        ----------
        session_id : str

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

        Returns
        -------
        HttpResponse[typing.List[WorkflowExecutionResponse]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/meeting-buddy/sessions/{jsonable_encoder(session_id)}/workflow-executions",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[WorkflowExecutionResponse],
                    parse_obj_as(
                        type_=typing.List[WorkflowExecutionResponse],  # 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 trigger_workflow_manually(
        self,
        session_id: str,
        *,
        workflow_id: str,
        message: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[WorkflowExecutionResponse]:
        """
        Manually trigger a FlowPlate workflow for a meeting session, regardless of event conditions.

        Parameters
        ----------
        session_id : str

        workflow_id : str

        message : typing.Optional[str]
            Optional custom prompt for the workflow. If omitted, a default is generated from session context.

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

        Returns
        -------
        HttpResponse[WorkflowExecutionResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/meeting-buddy/sessions/{jsonable_encoder(session_id)}/trigger-workflow",
            method="POST",
            json={
                "workflow_id": workflow_id,
                "message": message,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowExecutionResponse,
                    parse_obj_as(
                        type_=WorkflowExecutionResponse,  # 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 AsyncRawMeetingBuddyWorkflowTriggersClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list_workflow_triggers(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[typing.List[WorkflowTriggerResponse]]:
        """
        List all workflow triggers configured for the organization.

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

        Returns
        -------
        AsyncHttpResponse[typing.List[WorkflowTriggerResponse]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/meeting-buddy/workflow-triggers",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[WorkflowTriggerResponse],
                    parse_obj_as(
                        type_=typing.List[WorkflowTriggerResponse],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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 create_workflow_trigger(
        self,
        *,
        name: str,
        event_type: str,
        workflow_id: str,
        conditions: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        is_enabled: typing.Optional[bool] = OMIT,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[WorkflowTriggerResponse]:
        """
        Create a new workflow trigger that fires a FlowPlate workflow when an intelligence event occurs.

        Parameters
        ----------
        name : str

        event_type : str
            Event type that fires this trigger (e.g. intelligence_completed, deal_risk_detected)

        workflow_id : str

        conditions : typing.Optional[typing.Dict[str, typing.Any]]
            Optional conditions for trigger evaluation

        is_enabled : typing.Optional[bool]

        description : typing.Optional[str]

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

        Returns
        -------
        AsyncHttpResponse[WorkflowTriggerResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/meeting-buddy/workflow-triggers",
            method="POST",
            json={
                "name": name,
                "event_type": event_type,
                "workflow_id": workflow_id,
                "conditions": conditions,
                "is_enabled": is_enabled,
                "description": description,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowTriggerResponse,
                    parse_obj_as(
                        type_=WorkflowTriggerResponse,  # 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 delete_workflow_trigger(
        self, trigger_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[None]:
        """
        Soft-delete a workflow trigger.

        Parameters
        ----------
        trigger_id : str

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/meeting-buddy/workflow-triggers/{jsonable_encoder(trigger_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            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_workflow_trigger(
        self,
        trigger_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        event_type: typing.Optional[str] = OMIT,
        workflow_id: typing.Optional[str] = OMIT,
        conditions: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        is_enabled: typing.Optional[bool] = OMIT,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[WorkflowTriggerResponse]:
        """
        Update an existing workflow trigger's configuration.

        Parameters
        ----------
        trigger_id : str

        name : typing.Optional[str]

        event_type : typing.Optional[str]

        workflow_id : typing.Optional[str]

        conditions : typing.Optional[typing.Dict[str, typing.Any]]

        is_enabled : typing.Optional[bool]

        description : typing.Optional[str]

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

        Returns
        -------
        AsyncHttpResponse[WorkflowTriggerResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/meeting-buddy/workflow-triggers/{jsonable_encoder(trigger_id)}",
            method="PATCH",
            json={
                "name": name,
                "event_type": event_type,
                "workflow_id": workflow_id,
                "conditions": conditions,
                "is_enabled": is_enabled,
                "description": description,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowTriggerResponse,
                    parse_obj_as(
                        type_=WorkflowTriggerResponse,  # 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 list_session_workflow_executions(
        self, session_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[typing.List[WorkflowExecutionResponse]]:
        """
        Get all workflow executions that ran for a specific meeting session.

        Parameters
        ----------
        session_id : str

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

        Returns
        -------
        AsyncHttpResponse[typing.List[WorkflowExecutionResponse]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/meeting-buddy/sessions/{jsonable_encoder(session_id)}/workflow-executions",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[WorkflowExecutionResponse],
                    parse_obj_as(
                        type_=typing.List[WorkflowExecutionResponse],  # 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 trigger_workflow_manually(
        self,
        session_id: str,
        *,
        workflow_id: str,
        message: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[WorkflowExecutionResponse]:
        """
        Manually trigger a FlowPlate workflow for a meeting session, regardless of event conditions.

        Parameters
        ----------
        session_id : str

        workflow_id : str

        message : typing.Optional[str]
            Optional custom prompt for the workflow. If omitted, a default is generated from session context.

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

        Returns
        -------
        AsyncHttpResponse[WorkflowExecutionResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/meeting-buddy/sessions/{jsonable_encoder(session_id)}/trigger-workflow",
            method="POST",
            json={
                "workflow_id": workflow_id,
                "message": message,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    WorkflowExecutionResponse,
                    parse_obj_as(
                        type_=WorkflowExecutionResponse,  # 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)
