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

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.trigger_subscription_list_response import TriggerSubscriptionListResponse
from ..types.trigger_subscription_response import TriggerSubscriptionResponse
from .raw_client import AsyncRawTriggerSubscriptionsClient, RawTriggerSubscriptionsClient

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


class TriggerSubscriptionsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawTriggerSubscriptionsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawTriggerSubscriptionsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawTriggerSubscriptionsClient
        """
        return self._raw_client

    def list_subscriptions(
        self,
        *,
        organization_id: str,
        skip: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TriggerSubscriptionListResponse:
        """
        List all trigger subscriptions for the current user in the given organization.

        Parameters
        ----------
        organization_id : str
            Organization ID

        skip : typing.Optional[int]

        limit : typing.Optional[int]

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

        Returns
        -------
        TriggerSubscriptionListResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.trigger_subscriptions.list_subscriptions(
            organization_id="organization_id",
        )
        """
        _response = self._raw_client.list_subscriptions(
            organization_id=organization_id, skip=skip, limit=limit, request_options=request_options
        )
        return _response.data

    def create_subscription(
        self,
        *,
        organization_id: str,
        provider: str,
        event_type: str,
        flow_id: str,
        trigger_name: str,
        api_key: str,
        connection_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TriggerSubscriptionResponse:
        """
        Subscribe a FlowPlate flow to a webhook event. Idempotent: if a subscription with the same (provider, event_type, connection_id, flow_id) already exists for this user, it is returned and re-enabled if disabled.

        Parameters
        ----------
        organization_id : str
            Organization ID

        provider : str
            Provider name (e.g., 'connector', 'freshdesk')

        event_type : str
            Event type (e.g., 'gmail_new_email', 'ticket_created')

        flow_id : str
            FlowPlate flow UUID to trigger

        trigger_name : str
            Trigger node display name in the flow (e.g., 'Answer Tickets')

        api_key : str
            FlowPlate API key for the flow owner (sk-...)

        connection_id : typing.Optional[str]
            Provider-specific account/connection ID. NULL matches all connections.

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

        Returns
        -------
        TriggerSubscriptionResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.trigger_subscriptions.create_subscription(
            organization_id="organization_id",
            provider="provider",
            event_type="event_type",
            flow_id="flow_id",
            trigger_name="trigger_name",
            api_key="api_key",
        )
        """
        _response = self._raw_client.create_subscription(
            organization_id=organization_id,
            provider=provider,
            event_type=event_type,
            flow_id=flow_id,
            trigger_name=trigger_name,
            api_key=api_key,
            connection_id=connection_id,
            request_options=request_options,
        )
        return _response.data

    def get_subscription(
        self, subscription_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> TriggerSubscriptionResponse:
        """
        Retrieve a single trigger subscription by ID. Only the owner can access it.

        Parameters
        ----------
        subscription_id : str

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

        Returns
        -------
        TriggerSubscriptionResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.trigger_subscriptions.get_subscription(
            subscription_id="subscription_id",
        )
        """
        _response = self._raw_client.get_subscription(subscription_id, request_options=request_options)
        return _response.data

    def delete_subscription(
        self, subscription_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Soft-delete a trigger subscription. It will no longer match incoming webhooks.

        Parameters
        ----------
        subscription_id : str

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

        Returns
        -------
        None

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.trigger_subscriptions.delete_subscription(
            subscription_id="subscription_id",
        )
        """
        _response = self._raw_client.delete_subscription(subscription_id, request_options=request_options)
        return _response.data

    def update_subscription(
        self,
        subscription_id: str,
        *,
        provider: typing.Optional[str] = OMIT,
        event_type: typing.Optional[str] = OMIT,
        connection_id: typing.Optional[str] = OMIT,
        flow_id: typing.Optional[str] = OMIT,
        trigger_name: typing.Optional[str] = OMIT,
        api_key: typing.Optional[str] = OMIT,
        enabled: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TriggerSubscriptionResponse:
        """
        Update fields on a trigger subscription. If api_key is provided, it is re-encrypted at rest.

        Parameters
        ----------
        subscription_id : str

        provider : typing.Optional[str]

        event_type : typing.Optional[str]

        connection_id : typing.Optional[str]

        flow_id : typing.Optional[str]

        trigger_name : typing.Optional[str]

        api_key : typing.Optional[str]
            New API key (re-encrypted if provided)

        enabled : typing.Optional[bool]

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

        Returns
        -------
        TriggerSubscriptionResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.trigger_subscriptions.update_subscription(
            subscription_id="subscription_id",
        )
        """
        _response = self._raw_client.update_subscription(
            subscription_id,
            provider=provider,
            event_type=event_type,
            connection_id=connection_id,
            flow_id=flow_id,
            trigger_name=trigger_name,
            api_key=api_key,
            enabled=enabled,
            request_options=request_options,
        )
        return _response.data

    def create_subscription_s2s(
        self,
        *,
        organization_id: str,
        user_id: str,
        flowplate_master_key: str,
        provider: str,
        event_type: str,
        flow_id: str,
        trigger_name: str,
        api_key: str,
        connection_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TriggerSubscriptionResponse:
        """
        Called by FlowPlate when a user publishes a flow with trigger components. Authenticated via X-Flowplate-Master-Key. Idempotent: re-enables if the same subscription already exists.

        Parameters
        ----------
        organization_id : str
            Organization ID

        user_id : str
            User ID (flow owner)

        flowplate_master_key : str
            Flowplate master API key

        provider : str
            Provider name (e.g., 'connector', 'freshdesk')

        event_type : str
            Event type (e.g., 'gmail_new_email', 'ticket_created')

        flow_id : str
            FlowPlate flow UUID to trigger

        trigger_name : str
            Trigger node display name in the flow (e.g., 'Answer Tickets')

        api_key : str
            FlowPlate API key for the flow owner (sk-...)

        connection_id : typing.Optional[str]
            Provider-specific account/connection ID. NULL matches all connections.

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

        Returns
        -------
        TriggerSubscriptionResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.trigger_subscriptions.create_subscription_s2s(
            flowplate_master_key="x-flowplate-master-key",
            organization_id="organization_id",
            user_id="user_id",
            provider="provider",
            event_type="event_type",
            flow_id="flow_id",
            trigger_name="trigger_name",
            api_key="api_key",
        )
        """
        _response = self._raw_client.create_subscription_s2s(
            organization_id=organization_id,
            user_id=user_id,
            flowplate_master_key=flowplate_master_key,
            provider=provider,
            event_type=event_type,
            flow_id=flow_id,
            trigger_name=trigger_name,
            api_key=api_key,
            connection_id=connection_id,
            request_options=request_options,
        )
        return _response.data

    def delete_subscriptions_by_flow(
        self,
        flow_id: str,
        *,
        user_id: str,
        flowplate_master_key: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Dict[str, typing.Any]:
        """
        Called by FlowPlate when a user unpublishes a flow. Soft-deletes all trigger subscriptions for the given flow_id. Authenticated via X-Flowplate-Master-Key.

        Parameters
        ----------
        flow_id : str

        user_id : str
            User ID (flow owner)

        flowplate_master_key : str
            Flowplate master API key

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

        Returns
        -------
        typing.Dict[str, typing.Any]
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.trigger_subscriptions.delete_subscriptions_by_flow(
            flow_id="flow_id",
            flowplate_master_key="x-flowplate-master-key",
            user_id="user_id",
        )
        """
        _response = self._raw_client.delete_subscriptions_by_flow(
            flow_id, user_id=user_id, flowplate_master_key=flowplate_master_key, request_options=request_options
        )
        return _response.data


class AsyncTriggerSubscriptionsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawTriggerSubscriptionsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawTriggerSubscriptionsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawTriggerSubscriptionsClient
        """
        return self._raw_client

    async def list_subscriptions(
        self,
        *,
        organization_id: str,
        skip: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TriggerSubscriptionListResponse:
        """
        List all trigger subscriptions for the current user in the given organization.

        Parameters
        ----------
        organization_id : str
            Organization ID

        skip : typing.Optional[int]

        limit : typing.Optional[int]

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

        Returns
        -------
        TriggerSubscriptionListResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.trigger_subscriptions.list_subscriptions(
                organization_id="organization_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list_subscriptions(
            organization_id=organization_id, skip=skip, limit=limit, request_options=request_options
        )
        return _response.data

    async def create_subscription(
        self,
        *,
        organization_id: str,
        provider: str,
        event_type: str,
        flow_id: str,
        trigger_name: str,
        api_key: str,
        connection_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TriggerSubscriptionResponse:
        """
        Subscribe a FlowPlate flow to a webhook event. Idempotent: if a subscription with the same (provider, event_type, connection_id, flow_id) already exists for this user, it is returned and re-enabled if disabled.

        Parameters
        ----------
        organization_id : str
            Organization ID

        provider : str
            Provider name (e.g., 'connector', 'freshdesk')

        event_type : str
            Event type (e.g., 'gmail_new_email', 'ticket_created')

        flow_id : str
            FlowPlate flow UUID to trigger

        trigger_name : str
            Trigger node display name in the flow (e.g., 'Answer Tickets')

        api_key : str
            FlowPlate API key for the flow owner (sk-...)

        connection_id : typing.Optional[str]
            Provider-specific account/connection ID. NULL matches all connections.

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

        Returns
        -------
        TriggerSubscriptionResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.trigger_subscriptions.create_subscription(
                organization_id="organization_id",
                provider="provider",
                event_type="event_type",
                flow_id="flow_id",
                trigger_name="trigger_name",
                api_key="api_key",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create_subscription(
            organization_id=organization_id,
            provider=provider,
            event_type=event_type,
            flow_id=flow_id,
            trigger_name=trigger_name,
            api_key=api_key,
            connection_id=connection_id,
            request_options=request_options,
        )
        return _response.data

    async def get_subscription(
        self, subscription_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> TriggerSubscriptionResponse:
        """
        Retrieve a single trigger subscription by ID. Only the owner can access it.

        Parameters
        ----------
        subscription_id : str

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

        Returns
        -------
        TriggerSubscriptionResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.trigger_subscriptions.get_subscription(
                subscription_id="subscription_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_subscription(subscription_id, request_options=request_options)
        return _response.data

    async def delete_subscription(
        self, subscription_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Soft-delete a trigger subscription. It will no longer match incoming webhooks.

        Parameters
        ----------
        subscription_id : str

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.trigger_subscriptions.delete_subscription(
                subscription_id="subscription_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.delete_subscription(subscription_id, request_options=request_options)
        return _response.data

    async def update_subscription(
        self,
        subscription_id: str,
        *,
        provider: typing.Optional[str] = OMIT,
        event_type: typing.Optional[str] = OMIT,
        connection_id: typing.Optional[str] = OMIT,
        flow_id: typing.Optional[str] = OMIT,
        trigger_name: typing.Optional[str] = OMIT,
        api_key: typing.Optional[str] = OMIT,
        enabled: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TriggerSubscriptionResponse:
        """
        Update fields on a trigger subscription. If api_key is provided, it is re-encrypted at rest.

        Parameters
        ----------
        subscription_id : str

        provider : typing.Optional[str]

        event_type : typing.Optional[str]

        connection_id : typing.Optional[str]

        flow_id : typing.Optional[str]

        trigger_name : typing.Optional[str]

        api_key : typing.Optional[str]
            New API key (re-encrypted if provided)

        enabled : typing.Optional[bool]

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

        Returns
        -------
        TriggerSubscriptionResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.trigger_subscriptions.update_subscription(
                subscription_id="subscription_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update_subscription(
            subscription_id,
            provider=provider,
            event_type=event_type,
            connection_id=connection_id,
            flow_id=flow_id,
            trigger_name=trigger_name,
            api_key=api_key,
            enabled=enabled,
            request_options=request_options,
        )
        return _response.data

    async def create_subscription_s2s(
        self,
        *,
        organization_id: str,
        user_id: str,
        flowplate_master_key: str,
        provider: str,
        event_type: str,
        flow_id: str,
        trigger_name: str,
        api_key: str,
        connection_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TriggerSubscriptionResponse:
        """
        Called by FlowPlate when a user publishes a flow with trigger components. Authenticated via X-Flowplate-Master-Key. Idempotent: re-enables if the same subscription already exists.

        Parameters
        ----------
        organization_id : str
            Organization ID

        user_id : str
            User ID (flow owner)

        flowplate_master_key : str
            Flowplate master API key

        provider : str
            Provider name (e.g., 'connector', 'freshdesk')

        event_type : str
            Event type (e.g., 'gmail_new_email', 'ticket_created')

        flow_id : str
            FlowPlate flow UUID to trigger

        trigger_name : str
            Trigger node display name in the flow (e.g., 'Answer Tickets')

        api_key : str
            FlowPlate API key for the flow owner (sk-...)

        connection_id : typing.Optional[str]
            Provider-specific account/connection ID. NULL matches all connections.

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

        Returns
        -------
        TriggerSubscriptionResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.trigger_subscriptions.create_subscription_s2s(
                flowplate_master_key="x-flowplate-master-key",
                organization_id="organization_id",
                user_id="user_id",
                provider="provider",
                event_type="event_type",
                flow_id="flow_id",
                trigger_name="trigger_name",
                api_key="api_key",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create_subscription_s2s(
            organization_id=organization_id,
            user_id=user_id,
            flowplate_master_key=flowplate_master_key,
            provider=provider,
            event_type=event_type,
            flow_id=flow_id,
            trigger_name=trigger_name,
            api_key=api_key,
            connection_id=connection_id,
            request_options=request_options,
        )
        return _response.data

    async def delete_subscriptions_by_flow(
        self,
        flow_id: str,
        *,
        user_id: str,
        flowplate_master_key: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Dict[str, typing.Any]:
        """
        Called by FlowPlate when a user unpublishes a flow. Soft-deletes all trigger subscriptions for the given flow_id. Authenticated via X-Flowplate-Master-Key.

        Parameters
        ----------
        flow_id : str

        user_id : str
            User ID (flow owner)

        flowplate_master_key : str
            Flowplate master API key

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

        Returns
        -------
        typing.Dict[str, typing.Any]
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.trigger_subscriptions.delete_subscriptions_by_flow(
                flow_id="flow_id",
                flowplate_master_key="x-flowplate-master-key",
                user_id="user_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.delete_subscriptions_by_flow(
            flow_id, user_id=user_id, flowplate_master_key=flowplate_master_key, request_options=request_options
        )
        return _response.data
