# 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.request_options import RequestOptions
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.unchecked_base_model import construct_type
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.action_type import ActionType
from ..types.delete_trigger_response import DeleteTriggerResponse
from ..types.execute_trigger_response import ExecuteTriggerResponse
from ..types.http_validation_error import HttpValidationError
from ..types.list_triggers_response import ListTriggersResponse
from ..types.src_app_http_routes_trigger_create_trigger_response import SrcAppHttpRoutesTriggerCreateTriggerResponse
from ..types.update_cron_auth_response import UpdateCronAuthResponse
from ..types.update_trigger_response import UpdateTriggerResponse
from .types.triggers_create_request import TriggersCreateRequest
from .types.triggers_execute_slack_response import TriggersExecuteSlackResponse
from .types.triggers_update_request import TriggersUpdateRequest

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


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

    def list(
        self,
        *,
        action_type: typing.Optional[ActionType] = None,
        workflow_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ListTriggersResponse]:
        """
        All apis and effects are injected into every endpoint
        via request.context. The request_context() utility can be used
        to get Intellisense type-completion

        Parameters
        ----------
        action_type : typing.Optional[ActionType]
            Filter by action type

        workflow_id : typing.Optional[str]
            Filter by workflow ID

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

        Returns
        -------
        HttpResponse[ListTriggersResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/triggers",
            method="GET",
            params={
                "action_type": action_type,
                "workflow_id": workflow_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListTriggersResponse,
                    construct_type(
                        type_=ListTriggersResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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 create(
        self, *, request: TriggersCreateRequest, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[SrcAppHttpRoutesTriggerCreateTriggerResponse]:
        """
        Parameters
        ----------
        request : TriggersCreateRequest

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

        Returns
        -------
        HttpResponse[SrcAppHttpRoutesTriggerCreateTriggerResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/triggers",
            method="POST",
            json=convert_and_respect_annotation_metadata(
                object_=request, annotation=TriggersCreateRequest, direction="write"
            ),
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SrcAppHttpRoutesTriggerCreateTriggerResponse,
                    construct_type(
                        type_=SrcAppHttpRoutesTriggerCreateTriggerResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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(
        self,
        trigger_id: str,
        *,
        request: TriggersUpdateRequest,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[UpdateTriggerResponse]:
        """
        Parameters
        ----------
        trigger_id : str

        request : TriggersUpdateRequest

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

        Returns
        -------
        HttpResponse[UpdateTriggerResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/triggers/{jsonable_encoder(trigger_id)}",
            method="PUT",
            json=convert_and_respect_annotation_metadata(
                object_=request, annotation=TriggersUpdateRequest, direction="write"
            ),
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateTriggerResponse,
                    construct_type(
                        type_=UpdateTriggerResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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(
        self, trigger_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DeleteTriggerResponse]:
        """
        Parameters
        ----------
        trigger_id : str

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

        Returns
        -------
        HttpResponse[DeleteTriggerResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/triggers/{jsonable_encoder(trigger_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteTriggerResponse,
                    construct_type(
                        type_=DeleteTriggerResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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 execute_slack(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[TriggersExecuteSlackResponse]:
        """
        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[TriggersExecuteSlackResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/triggers/slack/execute",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    TriggersExecuteSlackResponse,
                    construct_type(
                        type_=TriggersExecuteSlackResponse,  # 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 execute_cron(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ExecuteTriggerResponse]:
        """
        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[ExecuteTriggerResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/triggers/cron/execute",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExecuteTriggerResponse,
                    construct_type(
                        type_=ExecuteTriggerResponse,  # 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 update_cron_auth_headers(
        self,
        *,
        dry_run: typing.Optional[bool] = None,
        test_org_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[UpdateCronAuthResponse]:
        """
        Update Cloud Scheduler job Authorization headers for organizations.
        Lists all Cloud Scheduler jobs and updates those matching the trigger pattern.

        Args:
            dry_run: If True, only shows what would be updated without making changes
            test_org_id: If provided, only processes jobs for this specific organization ID

        Only accessible to Scout internal organizations for security.

        Parameters
        ----------
        dry_run : typing.Optional[bool]

        test_org_id : typing.Optional[str]

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

        Returns
        -------
        HttpResponse[UpdateCronAuthResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/triggers/cron/update-auth-headers",
            method="POST",
            params={
                "dry_run": dry_run,
                "test_org_id": test_org_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateCronAuthResponse,
                    construct_type(
                        type_=UpdateCronAuthResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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 AsyncRawTriggersClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        *,
        action_type: typing.Optional[ActionType] = None,
        workflow_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ListTriggersResponse]:
        """
        All apis and effects are injected into every endpoint
        via request.context. The request_context() utility can be used
        to get Intellisense type-completion

        Parameters
        ----------
        action_type : typing.Optional[ActionType]
            Filter by action type

        workflow_id : typing.Optional[str]
            Filter by workflow ID

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

        Returns
        -------
        AsyncHttpResponse[ListTriggersResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/triggers",
            method="GET",
            params={
                "action_type": action_type,
                "workflow_id": workflow_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListTriggersResponse,
                    construct_type(
                        type_=ListTriggersResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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 create(
        self, *, request: TriggersCreateRequest, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[SrcAppHttpRoutesTriggerCreateTriggerResponse]:
        """
        Parameters
        ----------
        request : TriggersCreateRequest

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

        Returns
        -------
        AsyncHttpResponse[SrcAppHttpRoutesTriggerCreateTriggerResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/triggers",
            method="POST",
            json=convert_and_respect_annotation_metadata(
                object_=request, annotation=TriggersCreateRequest, direction="write"
            ),
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SrcAppHttpRoutesTriggerCreateTriggerResponse,
                    construct_type(
                        type_=SrcAppHttpRoutesTriggerCreateTriggerResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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(
        self,
        trigger_id: str,
        *,
        request: TriggersUpdateRequest,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[UpdateTriggerResponse]:
        """
        Parameters
        ----------
        trigger_id : str

        request : TriggersUpdateRequest

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

        Returns
        -------
        AsyncHttpResponse[UpdateTriggerResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/triggers/{jsonable_encoder(trigger_id)}",
            method="PUT",
            json=convert_and_respect_annotation_metadata(
                object_=request, annotation=TriggersUpdateRequest, direction="write"
            ),
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateTriggerResponse,
                    construct_type(
                        type_=UpdateTriggerResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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(
        self, trigger_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DeleteTriggerResponse]:
        """
        Parameters
        ----------
        trigger_id : str

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

        Returns
        -------
        AsyncHttpResponse[DeleteTriggerResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/triggers/{jsonable_encoder(trigger_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteTriggerResponse,
                    construct_type(
                        type_=DeleteTriggerResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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 execute_slack(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[TriggersExecuteSlackResponse]:
        """
        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[TriggersExecuteSlackResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/triggers/slack/execute",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    TriggersExecuteSlackResponse,
                    construct_type(
                        type_=TriggersExecuteSlackResponse,  # 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 execute_cron(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ExecuteTriggerResponse]:
        """
        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[ExecuteTriggerResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/triggers/cron/execute",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExecuteTriggerResponse,
                    construct_type(
                        type_=ExecuteTriggerResponse,  # 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 update_cron_auth_headers(
        self,
        *,
        dry_run: typing.Optional[bool] = None,
        test_org_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[UpdateCronAuthResponse]:
        """
        Update Cloud Scheduler job Authorization headers for organizations.
        Lists all Cloud Scheduler jobs and updates those matching the trigger pattern.

        Args:
            dry_run: If True, only shows what would be updated without making changes
            test_org_id: If provided, only processes jobs for this specific organization ID

        Only accessible to Scout internal organizations for security.

        Parameters
        ----------
        dry_run : typing.Optional[bool]

        test_org_id : typing.Optional[str]

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

        Returns
        -------
        AsyncHttpResponse[UpdateCronAuthResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/triggers/cron/update-auth-headers",
            method="POST",
            params={
                "dry_run": dry_run,
                "test_org_id": test_org_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateCronAuthResponse,
                    construct_type(
                        type_=UpdateCronAuthResponse,  # 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(
                        HttpValidationError,
                        construct_type(
                            type_=HttpValidationError,  # 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)
