# 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.max_page_size import MaxPageSize
from ..types.next_page_token import NextPageToken
from ..types.sort_dir import SortDir
from ..types.webhook_subscription import WebhookSubscription
from ..types.webhook_subscription_event_type import WebhookSubscriptionEventType
from ..types.webhook_subscription_resource_type import WebhookSubscriptionResourceType
from .raw_client import AsyncRawWebhookSubscriptionsClient, RawWebhookSubscriptionsClient
from .types.webhook_subscriptions_delete_response import WebhookSubscriptionsDeleteResponse
from .types.webhook_subscriptions_list_response import WebhookSubscriptionsListResponse

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


class WebhookSubscriptionsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawWebhookSubscriptionsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawWebhookSubscriptionsClient
        """
        return self._raw_client

    def list(
        self,
        *,
        webhook_endpoint_id: typing.Optional[str] = None,
        resource_id: typing.Optional[str] = None,
        sort_dir: typing.Optional[SortDir] = None,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookSubscriptionsListResponse:
        """
        List webhook subscriptions. You can filter by `webhookEndpointId` to see all subscriptions for a given endpoint, or by `resourceId` to see all subscriptions for a given resource.

        Parameters
        ----------
        webhook_endpoint_id : typing.Optional[str]
            Filter subscriptions by the webhook endpoint they belong to.

        resource_id : typing.Optional[str]
            Filter subscriptions by the resource they are scoped to.

        sort_dir : typing.Optional[SortDir]

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

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

        Returns
        -------
        WebhookSubscriptionsListResponse
            List of webhook subscriptions

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_subscriptions.list(
            next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
        )
        """
        _response = self._raw_client.list(
            webhook_endpoint_id=webhook_endpoint_id,
            resource_id=resource_id,
            sort_dir=sort_dir,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            request_options=request_options,
        )
        return _response.data

    def create(
        self,
        *,
        webhook_endpoint_id: str,
        resource_type: WebhookSubscriptionResourceType,
        resource_id: str,
        enabled_events: typing.Sequence[WebhookSubscriptionEventType],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookSubscription:
        """
        Create a resource-scoped webhook subscription on an existing webhook endpoint.

        Subscriptions let you receive events for a specific resource (e.g., a single extractor or workflow) rather than all resources of that type. The `enabledEvents` must be valid for the given `resourceType` and the endpoint's `apiVersion`.

        If a subscription already exists for the same endpoint and resource, it will be updated with the new `enabledEvents` instead of creating a duplicate.

        Parameters
        ----------
        webhook_endpoint_id : str
            The ID of the webhook endpoint to attach this subscription to.

        resource_type : WebhookSubscriptionResourceType

        resource_id : str
            The ID of the resource to scope this subscription to.

        enabled_events : typing.Sequence[WebhookSubscriptionEventType]
            The event types to subscribe to. Must be valid for the given `resourceType`.

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

        Returns
        -------
        WebhookSubscription
            Successfully created webhook subscription

        Examples
        --------
        from extend_ai import (
            Extend,
            WebhookSubscriptionEventType,
            WebhookSubscriptionResourceType,
        )

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_subscriptions.create(
            webhook_endpoint_id="wh_Xj8mK2pL9nR4vT7qY5wZ",
            resource_type=WebhookSubscriptionResourceType.EXTRACTOR,
            resource_id="ex_Xj8mK2pL9nR4vT7qY5wZ",
            enabled_events=[
                WebhookSubscriptionEventType.EXTRACT_RUN_PROCESSED,
                WebhookSubscriptionEventType.EXTRACT_RUN_FAILED,
            ],
        )
        """
        _response = self._raw_client.create(
            webhook_endpoint_id=webhook_endpoint_id,
            resource_type=resource_type,
            resource_id=resource_id,
            enabled_events=enabled_events,
            request_options=request_options,
        )
        return _response.data

    def retrieve(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> WebhookSubscription:
        """
        Retrieve a webhook subscription by ID.

        Parameters
        ----------
        id : str
            The ID of the webhook subscription.

            Example: `"whes_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        WebhookSubscription
            Retrieved webhook subscription

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_subscriptions.retrieve(
            id="webhook_subscription_id_here",
        )
        """
        _response = self._raw_client.retrieve(id, request_options=request_options)
        return _response.data

    def update(
        self,
        id: str,
        *,
        enabled_events: typing.Sequence[WebhookSubscriptionEventType],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookSubscription:
        """
        Update the enabled events on a webhook subscription.

        Parameters
        ----------
        id : str
            The ID of the webhook subscription to update.

            Example: `"whes_Xj8mK2pL9nR4vT7qY5wZ"`

        enabled_events : typing.Sequence[WebhookSubscriptionEventType]
            The event types to subscribe to. Must be valid for the subscription's resource type.

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

        Returns
        -------
        WebhookSubscription
            Successfully updated webhook subscription

        Examples
        --------
        from extend_ai import Extend, WebhookSubscriptionEventType

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_subscriptions.update(
            id="webhook_subscription_id_here",
            enabled_events=[WebhookSubscriptionEventType.EXTRACT_RUN_PROCESSED],
        )
        """
        _response = self._raw_client.update(id, enabled_events=enabled_events, request_options=request_options)
        return _response.data

    def delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WebhookSubscriptionsDeleteResponse:
        """
        Delete a webhook subscription. This operation is permanent and cannot be undone.

        Parameters
        ----------
        id : str
            The ID of the webhook subscription to delete.

            Example: `"whes_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        WebhookSubscriptionsDeleteResponse
            Successfully deleted webhook subscription

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_subscriptions.delete(
            id="webhook_subscription_id_here",
        )
        """
        _response = self._raw_client.delete(id, request_options=request_options)
        return _response.data


class AsyncWebhookSubscriptionsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawWebhookSubscriptionsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawWebhookSubscriptionsClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        webhook_endpoint_id: typing.Optional[str] = None,
        resource_id: typing.Optional[str] = None,
        sort_dir: typing.Optional[SortDir] = None,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookSubscriptionsListResponse:
        """
        List webhook subscriptions. You can filter by `webhookEndpointId` to see all subscriptions for a given endpoint, or by `resourceId` to see all subscriptions for a given resource.

        Parameters
        ----------
        webhook_endpoint_id : typing.Optional[str]
            Filter subscriptions by the webhook endpoint they belong to.

        resource_id : typing.Optional[str]
            Filter subscriptions by the resource they are scoped to.

        sort_dir : typing.Optional[SortDir]

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

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

        Returns
        -------
        WebhookSubscriptionsListResponse
            List of webhook subscriptions

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_subscriptions.list(
                next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list(
            webhook_endpoint_id=webhook_endpoint_id,
            resource_id=resource_id,
            sort_dir=sort_dir,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            request_options=request_options,
        )
        return _response.data

    async def create(
        self,
        *,
        webhook_endpoint_id: str,
        resource_type: WebhookSubscriptionResourceType,
        resource_id: str,
        enabled_events: typing.Sequence[WebhookSubscriptionEventType],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookSubscription:
        """
        Create a resource-scoped webhook subscription on an existing webhook endpoint.

        Subscriptions let you receive events for a specific resource (e.g., a single extractor or workflow) rather than all resources of that type. The `enabledEvents` must be valid for the given `resourceType` and the endpoint's `apiVersion`.

        If a subscription already exists for the same endpoint and resource, it will be updated with the new `enabledEvents` instead of creating a duplicate.

        Parameters
        ----------
        webhook_endpoint_id : str
            The ID of the webhook endpoint to attach this subscription to.

        resource_type : WebhookSubscriptionResourceType

        resource_id : str
            The ID of the resource to scope this subscription to.

        enabled_events : typing.Sequence[WebhookSubscriptionEventType]
            The event types to subscribe to. Must be valid for the given `resourceType`.

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

        Returns
        -------
        WebhookSubscription
            Successfully created webhook subscription

        Examples
        --------
        import asyncio

        from extend_ai import (
            AsyncExtend,
            WebhookSubscriptionEventType,
            WebhookSubscriptionResourceType,
        )

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_subscriptions.create(
                webhook_endpoint_id="wh_Xj8mK2pL9nR4vT7qY5wZ",
                resource_type=WebhookSubscriptionResourceType.EXTRACTOR,
                resource_id="ex_Xj8mK2pL9nR4vT7qY5wZ",
                enabled_events=[
                    WebhookSubscriptionEventType.EXTRACT_RUN_PROCESSED,
                    WebhookSubscriptionEventType.EXTRACT_RUN_FAILED,
                ],
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create(
            webhook_endpoint_id=webhook_endpoint_id,
            resource_type=resource_type,
            resource_id=resource_id,
            enabled_events=enabled_events,
            request_options=request_options,
        )
        return _response.data

    async def retrieve(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WebhookSubscription:
        """
        Retrieve a webhook subscription by ID.

        Parameters
        ----------
        id : str
            The ID of the webhook subscription.

            Example: `"whes_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        WebhookSubscription
            Retrieved webhook subscription

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_subscriptions.retrieve(
                id="webhook_subscription_id_here",
            )


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

    async def update(
        self,
        id: str,
        *,
        enabled_events: typing.Sequence[WebhookSubscriptionEventType],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookSubscription:
        """
        Update the enabled events on a webhook subscription.

        Parameters
        ----------
        id : str
            The ID of the webhook subscription to update.

            Example: `"whes_Xj8mK2pL9nR4vT7qY5wZ"`

        enabled_events : typing.Sequence[WebhookSubscriptionEventType]
            The event types to subscribe to. Must be valid for the subscription's resource type.

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

        Returns
        -------
        WebhookSubscription
            Successfully updated webhook subscription

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend, WebhookSubscriptionEventType

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_subscriptions.update(
                id="webhook_subscription_id_here",
                enabled_events=[WebhookSubscriptionEventType.EXTRACT_RUN_PROCESSED],
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update(id, enabled_events=enabled_events, request_options=request_options)
        return _response.data

    async def delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WebhookSubscriptionsDeleteResponse:
        """
        Delete a webhook subscription. This operation is permanent and cannot be undone.

        Parameters
        ----------
        id : str
            The ID of the webhook subscription to delete.

            Example: `"whes_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        WebhookSubscriptionsDeleteResponse
            Successfully deleted webhook subscription

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_subscriptions.delete(
                id="webhook_subscription_id_here",
            )


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