# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

import httpx

from ..types import notification_list_params, notification_list_deliveries_params
from .._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
from .._utils import maybe_transform
from .._compat import cached_property
from .._resource import SyncAPIResource, AsyncAPIResource
from .._response import (
    to_raw_response_wrapper,
    to_streamed_response_wrapper,
    async_to_raw_response_wrapper,
    async_to_streamed_response_wrapper,
)
from ..pagination import SyncCursorPage, AsyncCursorPage
from .._base_client import AsyncPaginator, make_request_options
from ..types.delivery import Delivery
from ..types.notification import Notification

__all__ = ["NotificationsResource", "AsyncNotificationsResource"]


class NotificationsResource(SyncAPIResource):
    @cached_property
    def with_raw_response(self) -> NotificationsResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/stainless-sdks/notiflows-sdk-python#accessing-raw-response-data-eg-headers
        """
        return NotificationsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> NotificationsResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/stainless-sdks/notiflows-sdk-python#with_streaming_response
        """
        return NotificationsResourceWithStreamingResponse(self)

    def retrieve(
        self,
        id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Notification:
        """
        Retrieve a single notification by ID, including the recipient, actor, and all
        delivery attempts.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return self._get(
            f"/notifications/{id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=Notification,
        )

    def list(
        self,
        *,
        after: str | Omit = omit,
        before: str | Omit = omit,
        created_after: str | Omit = omit,
        created_before: str | Omit = omit,
        limit: int | Omit = omit,
        status: str | Omit = omit,
        topic: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SyncCursorPage[Notification]:
        """
        Retrieve a paginated list of all notifications in your project.

        A notification represents a message sent to a single recipient through a
        notiflow run. Each notification can have multiple deliveries (one per channel).

        Args:
          after: Cursor for fetching the next page

          before: Cursor for fetching the previous page

          created_after: Filter by created after (ISO 8601 datetime)

          created_before: Filter by created before (ISO 8601 datetime)

          limit: Number of items per page (default: 25, max: 1000)

          status: Filter by status

          topic: Filter by topic

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return self._get_api_list(
            "/notifications",
            page=SyncCursorPage[Notification],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "after": after,
                        "before": before,
                        "created_after": created_after,
                        "created_before": created_before,
                        "limit": limit,
                        "status": status,
                        "topic": topic,
                    },
                    notification_list_params.NotificationListParams,
                ),
            ),
            model=Notification,
        )

    def list_deliveries(
        self,
        notification_id: str,
        *,
        after: str | Omit = omit,
        before: str | Omit = omit,
        channel_id: str | Omit = omit,
        created_after: str | Omit = omit,
        created_before: str | Omit = omit,
        limit: int | Omit = omit,
        status: str | Omit = omit,
        topic: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SyncCursorPage[Delivery]:
        """
        Retrieve all delivery attempts for a specific notification.

        Args:
          after: Cursor for fetching the next page

          before: Cursor for fetching the previous page

          channel_id: Filter by channel ID (UUID)

          created_after: Filter by created after (ISO 8601 datetime)

          created_before: Filter by created before (ISO 8601 datetime)

          limit: Number of items per page (default: 25, max: 1000)

          status: Filter by status (pending, sent, failed)

          topic: Filter by topic name

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not notification_id:
            raise ValueError(f"Expected a non-empty value for `notification_id` but received {notification_id!r}")
        return self._get_api_list(
            f"/notifications/{notification_id}/deliveries",
            page=SyncCursorPage[Delivery],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "after": after,
                        "before": before,
                        "channel_id": channel_id,
                        "created_after": created_after,
                        "created_before": created_before,
                        "limit": limit,
                        "status": status,
                        "topic": topic,
                    },
                    notification_list_deliveries_params.NotificationListDeliveriesParams,
                ),
            ),
            model=Delivery,
        )


class AsyncNotificationsResource(AsyncAPIResource):
    @cached_property
    def with_raw_response(self) -> AsyncNotificationsResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/stainless-sdks/notiflows-sdk-python#accessing-raw-response-data-eg-headers
        """
        return AsyncNotificationsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> AsyncNotificationsResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/stainless-sdks/notiflows-sdk-python#with_streaming_response
        """
        return AsyncNotificationsResourceWithStreamingResponse(self)

    async def retrieve(
        self,
        id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Notification:
        """
        Retrieve a single notification by ID, including the recipient, actor, and all
        delivery attempts.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        return await self._get(
            f"/notifications/{id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=Notification,
        )

    def list(
        self,
        *,
        after: str | Omit = omit,
        before: str | Omit = omit,
        created_after: str | Omit = omit,
        created_before: str | Omit = omit,
        limit: int | Omit = omit,
        status: str | Omit = omit,
        topic: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> AsyncPaginator[Notification, AsyncCursorPage[Notification]]:
        """
        Retrieve a paginated list of all notifications in your project.

        A notification represents a message sent to a single recipient through a
        notiflow run. Each notification can have multiple deliveries (one per channel).

        Args:
          after: Cursor for fetching the next page

          before: Cursor for fetching the previous page

          created_after: Filter by created after (ISO 8601 datetime)

          created_before: Filter by created before (ISO 8601 datetime)

          limit: Number of items per page (default: 25, max: 1000)

          status: Filter by status

          topic: Filter by topic

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return self._get_api_list(
            "/notifications",
            page=AsyncCursorPage[Notification],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "after": after,
                        "before": before,
                        "created_after": created_after,
                        "created_before": created_before,
                        "limit": limit,
                        "status": status,
                        "topic": topic,
                    },
                    notification_list_params.NotificationListParams,
                ),
            ),
            model=Notification,
        )

    def list_deliveries(
        self,
        notification_id: str,
        *,
        after: str | Omit = omit,
        before: str | Omit = omit,
        channel_id: str | Omit = omit,
        created_after: str | Omit = omit,
        created_before: str | Omit = omit,
        limit: int | Omit = omit,
        status: str | Omit = omit,
        topic: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> AsyncPaginator[Delivery, AsyncCursorPage[Delivery]]:
        """
        Retrieve all delivery attempts for a specific notification.

        Args:
          after: Cursor for fetching the next page

          before: Cursor for fetching the previous page

          channel_id: Filter by channel ID (UUID)

          created_after: Filter by created after (ISO 8601 datetime)

          created_before: Filter by created before (ISO 8601 datetime)

          limit: Number of items per page (default: 25, max: 1000)

          status: Filter by status (pending, sent, failed)

          topic: Filter by topic name

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not notification_id:
            raise ValueError(f"Expected a non-empty value for `notification_id` but received {notification_id!r}")
        return self._get_api_list(
            f"/notifications/{notification_id}/deliveries",
            page=AsyncCursorPage[Delivery],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "after": after,
                        "before": before,
                        "channel_id": channel_id,
                        "created_after": created_after,
                        "created_before": created_before,
                        "limit": limit,
                        "status": status,
                        "topic": topic,
                    },
                    notification_list_deliveries_params.NotificationListDeliveriesParams,
                ),
            ),
            model=Delivery,
        )


class NotificationsResourceWithRawResponse:
    def __init__(self, notifications: NotificationsResource) -> None:
        self._notifications = notifications

        self.retrieve = to_raw_response_wrapper(
            notifications.retrieve,
        )
        self.list = to_raw_response_wrapper(
            notifications.list,
        )
        self.list_deliveries = to_raw_response_wrapper(
            notifications.list_deliveries,
        )


class AsyncNotificationsResourceWithRawResponse:
    def __init__(self, notifications: AsyncNotificationsResource) -> None:
        self._notifications = notifications

        self.retrieve = async_to_raw_response_wrapper(
            notifications.retrieve,
        )
        self.list = async_to_raw_response_wrapper(
            notifications.list,
        )
        self.list_deliveries = async_to_raw_response_wrapper(
            notifications.list_deliveries,
        )


class NotificationsResourceWithStreamingResponse:
    def __init__(self, notifications: NotificationsResource) -> None:
        self._notifications = notifications

        self.retrieve = to_streamed_response_wrapper(
            notifications.retrieve,
        )
        self.list = to_streamed_response_wrapper(
            notifications.list,
        )
        self.list_deliveries = to_streamed_response_wrapper(
            notifications.list_deliveries,
        )


class AsyncNotificationsResourceWithStreamingResponse:
    def __init__(self, notifications: AsyncNotificationsResource) -> None:
        self._notifications = notifications

        self.retrieve = async_to_streamed_response_wrapper(
            notifications.retrieve,
        )
        self.list = async_to_streamed_response_wrapper(
            notifications.list,
        )
        self.list_deliveries = async_to_streamed_response_wrapper(
            notifications.list_deliveries,
        )
