# 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.attach_webhook_result import AttachWebhookResult
from ..types.detach_webhook_result import DetachWebhookResult
from ..types.list_webhooks_response import ListWebhooksResponse
from ..types.webhook_event_enum import WebhookEventEnum
from ..types.webhook_result import WebhookResult
from .raw_client import AsyncRawWebhooksClient, RawWebhooksClient
from .types.list_webhooks_request_status import ListWebhooksRequestStatus

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


class WebhooksClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawWebhooksClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawWebhooksClient
        """
        return self._raw_client

    def get(
        self, *, identifiers: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
    ) -> WebhookResult:
        """
        Retrieve all webhook that are set up to receive events for specific URLs or phone numbers.

        Parameters
        ----------
        identifiers : typing.Sequence[str]
            List of URLs or phone numbers in E.164 format that the webhook is attached to. <br><br>
            **Limit:** 1 min

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

        Returns
        -------
        WebhookResult
            Returns all webhooks matching the search criteria. <br>

            Results are sorted by ID and duplicates are removed. This identifier is a string that always begins with the prefix `wh_`, for example: `wh_1234567890`.

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.get(
            identifiers=[
                "https://www.pinnacle.sh/payment",
                "+14155678901",
                "https://www.pinnacle.sh/sms-callback",
                "+14153456659",
            ],
        )
        """
        _response = self._raw_client.get(identifiers=identifiers, request_options=request_options)
        return _response.data

    def list(
        self,
        *,
        page_index: typing.Optional[int] = OMIT,
        page_size: typing.Optional[int] = OMIT,
        status: typing.Optional[ListWebhooksRequestStatus] = OMIT,
        endpoint: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ListWebhooksResponse:
        """
        List all webhooks with optional filtering and pagination. Results are sorted by creation date, newest first.

        Parameters
        ----------
        page_index : typing.Optional[int]

        page_size : typing.Optional[int]

        status : typing.Optional[ListWebhooksRequestStatus]

        endpoint : typing.Optional[str]
            Filter webhooks by endpoint URL (partial match, case-insensitive).

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

        Returns
        -------
        ListWebhooksResponse
            Returns paginated list of webhooks.

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.list()
        """
        _response = self._raw_client.list(
            page_index=page_index,
            page_size=page_size,
            status=status,
            endpoint=endpoint,
            request_options=request_options,
        )
        return _response.data

    def attach(
        self,
        *,
        senders: typing.Sequence[str],
        webhook_id: typing.Optional[str] = OMIT,
        name: typing.Optional[str] = OMIT,
        url: typing.Optional[str] = OMIT,
        event: typing.Optional[WebhookEventEnum] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AttachWebhookResult:
        """
        Attach a webhook to one or more senders (phone numbers or RCS agent IDs) to receive real-time event notifications. <br>

        You can attach an existing webhook by providing its ID, or create a new webhook by specifying a name and URL. Supports bulk operations with up to 50 senders per request. <br>

        Subscriptions are additive — attaching new senders does not remove existing ones. Re-attaching the same sender updates the event type filter without creating duplicates.

        Parameters
        ----------
        senders : typing.Sequence[str]
            Array of senders to attach the webhook to. Can be phone numbers in E.164 format or RCS agent IDs.

        webhook_id : typing.Optional[str]
            Existing webhook ID (starts with `wh_`). Provide this OR `name` + `url` to create a new webhook. The webhook must be in ENABLED status. Disabled webhooks can be re-enabled from the [dashboard](https://app.pinnacle.sh/dashboard/development/webhooks).

        name : typing.Optional[str]
            Name for a new webhook (required if no `webhookId`).

        url : typing.Optional[str]
            HTTPS endpoint URL for a new webhook (required if no `webhookId`).

        event : typing.Optional[WebhookEventEnum]
            Event type filter for the subscription. Set to `null` to receive all events. <br>

            `USER.TYPING` is only supported for RCS agent senders, not phone numbers.

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

        Returns
        -------
        AttachWebhookResult
            Successfully attached webhook to the specified senders. <br>

            Senders that could not be found are returned in the `failed` array with error details.

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.attach(
            senders=["+14155551234", "agent_abc123"],
        )
        """
        _response = self._raw_client.attach(
            senders=senders, webhook_id=webhook_id, name=name, url=url, event=event, request_options=request_options
        )
        return _response.data

    def detach(
        self, *, webhook_id: str, senders: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
    ) -> DetachWebhookResult:
        """
        Detach a webhook from one or more senders (phone numbers or RCS agent IDs) to stop receiving event notifications. <br>

        The webhook itself is not deleted and remains available for use with other senders. Works regardless of webhook status. Supports bulk operations with up to 50 senders per request.

        Parameters
        ----------
        webhook_id : str
            Webhook ID to detach (starts with `wh_`). Must be a webhook owned by your team.

        senders : typing.Sequence[str]
            Array of senders to detach the webhook from. Can be phone numbers in E.164 format or RCS agent IDs.

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

        Returns
        -------
        DetachWebhookResult
            Successfully detached webhook from the specified senders. <br>

            Senders that could not be found are returned in the `failed` array with error details.

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.detach(
            webhook_id="webhookId",
            senders=["+14155551234", "agent_abc123"],
        )
        """
        _response = self._raw_client.detach(webhook_id=webhook_id, senders=senders, request_options=request_options)
        return _response.data


class AsyncWebhooksClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawWebhooksClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawWebhooksClient
        """
        return self._raw_client

    async def get(
        self, *, identifiers: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
    ) -> WebhookResult:
        """
        Retrieve all webhook that are set up to receive events for specific URLs or phone numbers.

        Parameters
        ----------
        identifiers : typing.Sequence[str]
            List of URLs or phone numbers in E.164 format that the webhook is attached to. <br><br>
            **Limit:** 1 min

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

        Returns
        -------
        WebhookResult
            Returns all webhooks matching the search criteria. <br>

            Results are sorted by ID and duplicates are removed. This identifier is a string that always begins with the prefix `wh_`, for example: `wh_1234567890`.

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.get(
                identifiers=[
                    "https://www.pinnacle.sh/payment",
                    "+14155678901",
                    "https://www.pinnacle.sh/sms-callback",
                    "+14153456659",
                ],
            )


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

    async def list(
        self,
        *,
        page_index: typing.Optional[int] = OMIT,
        page_size: typing.Optional[int] = OMIT,
        status: typing.Optional[ListWebhooksRequestStatus] = OMIT,
        endpoint: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ListWebhooksResponse:
        """
        List all webhooks with optional filtering and pagination. Results are sorted by creation date, newest first.

        Parameters
        ----------
        page_index : typing.Optional[int]

        page_size : typing.Optional[int]

        status : typing.Optional[ListWebhooksRequestStatus]

        endpoint : typing.Optional[str]
            Filter webhooks by endpoint URL (partial match, case-insensitive).

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

        Returns
        -------
        ListWebhooksResponse
            Returns paginated list of webhooks.

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.list()


        asyncio.run(main())
        """
        _response = await self._raw_client.list(
            page_index=page_index,
            page_size=page_size,
            status=status,
            endpoint=endpoint,
            request_options=request_options,
        )
        return _response.data

    async def attach(
        self,
        *,
        senders: typing.Sequence[str],
        webhook_id: typing.Optional[str] = OMIT,
        name: typing.Optional[str] = OMIT,
        url: typing.Optional[str] = OMIT,
        event: typing.Optional[WebhookEventEnum] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AttachWebhookResult:
        """
        Attach a webhook to one or more senders (phone numbers or RCS agent IDs) to receive real-time event notifications. <br>

        You can attach an existing webhook by providing its ID, or create a new webhook by specifying a name and URL. Supports bulk operations with up to 50 senders per request. <br>

        Subscriptions are additive — attaching new senders does not remove existing ones. Re-attaching the same sender updates the event type filter without creating duplicates.

        Parameters
        ----------
        senders : typing.Sequence[str]
            Array of senders to attach the webhook to. Can be phone numbers in E.164 format or RCS agent IDs.

        webhook_id : typing.Optional[str]
            Existing webhook ID (starts with `wh_`). Provide this OR `name` + `url` to create a new webhook. The webhook must be in ENABLED status. Disabled webhooks can be re-enabled from the [dashboard](https://app.pinnacle.sh/dashboard/development/webhooks).

        name : typing.Optional[str]
            Name for a new webhook (required if no `webhookId`).

        url : typing.Optional[str]
            HTTPS endpoint URL for a new webhook (required if no `webhookId`).

        event : typing.Optional[WebhookEventEnum]
            Event type filter for the subscription. Set to `null` to receive all events. <br>

            `USER.TYPING` is only supported for RCS agent senders, not phone numbers.

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

        Returns
        -------
        AttachWebhookResult
            Successfully attached webhook to the specified senders. <br>

            Senders that could not be found are returned in the `failed` array with error details.

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.attach(
                senders=["+14155551234", "agent_abc123"],
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.attach(
            senders=senders, webhook_id=webhook_id, name=name, url=url, event=event, request_options=request_options
        )
        return _response.data

    async def detach(
        self, *, webhook_id: str, senders: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
    ) -> DetachWebhookResult:
        """
        Detach a webhook from one or more senders (phone numbers or RCS agent IDs) to stop receiving event notifications. <br>

        The webhook itself is not deleted and remains available for use with other senders. Works regardless of webhook status. Supports bulk operations with up to 50 senders per request.

        Parameters
        ----------
        webhook_id : str
            Webhook ID to detach (starts with `wh_`). Must be a webhook owned by your team.

        senders : typing.Sequence[str]
            Array of senders to detach the webhook from. Can be phone numbers in E.164 format or RCS agent IDs.

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

        Returns
        -------
        DetachWebhookResult
            Successfully detached webhook from the specified senders. <br>

            Senders that could not be found are returned in the `failed` array with error details.

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.detach(
                webhook_id="webhookId",
                senders=["+14155551234", "agent_abc123"],
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.detach(
            webhook_id=webhook_id, senders=senders, request_options=request_options
        )
        return _response.data
