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

from __future__ import annotations

from typing import Iterable

import httpx

from ..types import notiflow_run_params
from .._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
from .._utils import maybe_transform, async_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 .._base_client import make_request_options
from ..types.notiflow_run_response import NotiflowRunResponse

__all__ = ["NotiflowsResource", "AsyncNotiflowsResource"]


class NotiflowsResource(SyncAPIResource):
    @cached_property
    def with_raw_response(self) -> NotiflowsResourceWithRawResponse:
        """
        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 NotiflowsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> NotiflowsResourceWithStreamingResponse:
        """
        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 NotiflowsResourceWithStreamingResponse(self)

    def run(
        self,
        notiflow_handle: str,
        *,
        actor: notiflow_run_params.Actor | Omit = omit,
        data: object | Omit = omit,
        recipients: Iterable[notiflow_run_params.Recipient] | 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,
    ) -> NotiflowRunResponse:
        """
        Execute a notiflow to send notifications to one or more recipients.

        You must provide either `recipients` (direct targeting) or `topic` (pub/sub
        targeting), but not both.

        **Direct targeting (`recipients`):** Specify exactly which users should receive
        the notification. Users are identified by their `external_id`. If a user doesn't
        exist, they will be created automatically with the provided attributes.

        **Topic targeting (`topic`):** Send to all users subscribed to the specified
        topic. Useful for broadcast scenarios.

        **Template variables:** The `data` object is passed to notification templates.
        Access variables using Liquid syntax: `{{ data.variable_name }}`

        Args:
          actor: Actor (triggering user) specification. Accessible in templates as `actor.*`.

          data: Template variables. Accessible in notification templates as `data.*`.

          recipients: List of recipients. Each recipient must have an `external_id`. Users are
              auto-created if they don't exist.

          topic: Topic name for pub/sub targeting. Cannot be used with `recipients`.

          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 notiflow_handle:
            raise ValueError(f"Expected a non-empty value for `notiflow_handle` but received {notiflow_handle!r}")
        return self._post(
            f"/notiflows/{notiflow_handle}/run",
            body=maybe_transform(
                {
                    "actor": actor,
                    "data": data,
                    "recipients": recipients,
                    "topic": topic,
                },
                notiflow_run_params.NotiflowRunParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=NotiflowRunResponse,
        )


class AsyncNotiflowsResource(AsyncAPIResource):
    @cached_property
    def with_raw_response(self) -> AsyncNotiflowsResourceWithRawResponse:
        """
        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 AsyncNotiflowsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> AsyncNotiflowsResourceWithStreamingResponse:
        """
        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 AsyncNotiflowsResourceWithStreamingResponse(self)

    async def run(
        self,
        notiflow_handle: str,
        *,
        actor: notiflow_run_params.Actor | Omit = omit,
        data: object | Omit = omit,
        recipients: Iterable[notiflow_run_params.Recipient] | 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,
    ) -> NotiflowRunResponse:
        """
        Execute a notiflow to send notifications to one or more recipients.

        You must provide either `recipients` (direct targeting) or `topic` (pub/sub
        targeting), but not both.

        **Direct targeting (`recipients`):** Specify exactly which users should receive
        the notification. Users are identified by their `external_id`. If a user doesn't
        exist, they will be created automatically with the provided attributes.

        **Topic targeting (`topic`):** Send to all users subscribed to the specified
        topic. Useful for broadcast scenarios.

        **Template variables:** The `data` object is passed to notification templates.
        Access variables using Liquid syntax: `{{ data.variable_name }}`

        Args:
          actor: Actor (triggering user) specification. Accessible in templates as `actor.*`.

          data: Template variables. Accessible in notification templates as `data.*`.

          recipients: List of recipients. Each recipient must have an `external_id`. Users are
              auto-created if they don't exist.

          topic: Topic name for pub/sub targeting. Cannot be used with `recipients`.

          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 notiflow_handle:
            raise ValueError(f"Expected a non-empty value for `notiflow_handle` but received {notiflow_handle!r}")
        return await self._post(
            f"/notiflows/{notiflow_handle}/run",
            body=await async_maybe_transform(
                {
                    "actor": actor,
                    "data": data,
                    "recipients": recipients,
                    "topic": topic,
                },
                notiflow_run_params.NotiflowRunParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=NotiflowRunResponse,
        )


class NotiflowsResourceWithRawResponse:
    def __init__(self, notiflows: NotiflowsResource) -> None:
        self._notiflows = notiflows

        self.run = to_raw_response_wrapper(
            notiflows.run,
        )


class AsyncNotiflowsResourceWithRawResponse:
    def __init__(self, notiflows: AsyncNotiflowsResource) -> None:
        self._notiflows = notiflows

        self.run = async_to_raw_response_wrapper(
            notiflows.run,
        )


class NotiflowsResourceWithStreamingResponse:
    def __init__(self, notiflows: NotiflowsResource) -> None:
        self._notiflows = notiflows

        self.run = to_streamed_response_wrapper(
            notiflows.run,
        )


class AsyncNotiflowsResourceWithStreamingResponse:
    def __init__(self, notiflows: AsyncNotiflowsResource) -> None:
        self._notiflows = notiflows

        self.run = async_to_streamed_response_wrapper(
            notiflows.run,
        )
