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

from __future__ import annotations

import httpx

from ..._types import Body, Query, Headers, NoneType, NotGiven, 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 ...types.users import channel_setting_update_params
from ..._base_client import make_request_options
from ...types.users.channel_settings import ChannelSettings

__all__ = ["ChannelSettingsResource", "AsyncChannelSettingsResource"]


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

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

    def retrieve(
        self,
        channel_id: str,
        *,
        user_external_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,
    ) -> ChannelSettings:
        """
        Retrieve a user's settings for a specific channel.

        Channel settings store provider-specific data like device tokens (for push) or
        Slack user IDs (for chat).

        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 user_external_id:
            raise ValueError(f"Expected a non-empty value for `user_external_id` but received {user_external_id!r}")
        if not channel_id:
            raise ValueError(f"Expected a non-empty value for `channel_id` but received {channel_id!r}")
        return self._get(
            f"/users/{user_external_id}/channel-settings/{channel_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=ChannelSettings,
        )

    def update(
        self,
        channel_id: str,
        *,
        user_external_id: str,
        settings: channel_setting_update_params.Settings,
        # 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,
    ) -> ChannelSettings:
        """Update a user's settings for a specific channel.

        Creates the settings if they
        don't exist.

        **Provider-specific settings:**

        **Mobile Push (APNS/FCM):**

        ```json
        {
          "settings": {
            "device_tokens": ["token1", "token2"]
          }
        }
        ```

        **Slack:**

        ```json
        {
          "settings": {
            "slack_channel_id": "C0123456789",
            "slack_user_id": "U0123456789"
          }
        }
        ```

        At least one of `slack_channel_id` or `slack_user_id` must be provided.

        Args:
          settings: Provider-specific settings. Structure depends on channel provider.

          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 user_external_id:
            raise ValueError(f"Expected a non-empty value for `user_external_id` but received {user_external_id!r}")
        if not channel_id:
            raise ValueError(f"Expected a non-empty value for `channel_id` but received {channel_id!r}")
        return self._put(
            f"/users/{user_external_id}/channel-settings/{channel_id}",
            body=maybe_transform({"settings": settings}, channel_setting_update_params.ChannelSettingUpdateParams),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=ChannelSettings,
        )

    def delete(
        self,
        channel_id: str,
        *,
        user_external_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,
    ) -> None:
        """
        Remove a user's settings for a specific channel.

        After deletion, the user will no longer receive notifications through this
        channel until new settings are configured.

        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 user_external_id:
            raise ValueError(f"Expected a non-empty value for `user_external_id` but received {user_external_id!r}")
        if not channel_id:
            raise ValueError(f"Expected a non-empty value for `channel_id` but received {channel_id!r}")
        extra_headers = {"Accept": "*/*", **(extra_headers or {})}
        return self._delete(
            f"/users/{user_external_id}/channel-settings/{channel_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=NoneType,
        )


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

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

    async def retrieve(
        self,
        channel_id: str,
        *,
        user_external_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,
    ) -> ChannelSettings:
        """
        Retrieve a user's settings for a specific channel.

        Channel settings store provider-specific data like device tokens (for push) or
        Slack user IDs (for chat).

        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 user_external_id:
            raise ValueError(f"Expected a non-empty value for `user_external_id` but received {user_external_id!r}")
        if not channel_id:
            raise ValueError(f"Expected a non-empty value for `channel_id` but received {channel_id!r}")
        return await self._get(
            f"/users/{user_external_id}/channel-settings/{channel_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=ChannelSettings,
        )

    async def update(
        self,
        channel_id: str,
        *,
        user_external_id: str,
        settings: channel_setting_update_params.Settings,
        # 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,
    ) -> ChannelSettings:
        """Update a user's settings for a specific channel.

        Creates the settings if they
        don't exist.

        **Provider-specific settings:**

        **Mobile Push (APNS/FCM):**

        ```json
        {
          "settings": {
            "device_tokens": ["token1", "token2"]
          }
        }
        ```

        **Slack:**

        ```json
        {
          "settings": {
            "slack_channel_id": "C0123456789",
            "slack_user_id": "U0123456789"
          }
        }
        ```

        At least one of `slack_channel_id` or `slack_user_id` must be provided.

        Args:
          settings: Provider-specific settings. Structure depends on channel provider.

          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 user_external_id:
            raise ValueError(f"Expected a non-empty value for `user_external_id` but received {user_external_id!r}")
        if not channel_id:
            raise ValueError(f"Expected a non-empty value for `channel_id` but received {channel_id!r}")
        return await self._put(
            f"/users/{user_external_id}/channel-settings/{channel_id}",
            body=await async_maybe_transform(
                {"settings": settings}, channel_setting_update_params.ChannelSettingUpdateParams
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=ChannelSettings,
        )

    async def delete(
        self,
        channel_id: str,
        *,
        user_external_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,
    ) -> None:
        """
        Remove a user's settings for a specific channel.

        After deletion, the user will no longer receive notifications through this
        channel until new settings are configured.

        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 user_external_id:
            raise ValueError(f"Expected a non-empty value for `user_external_id` but received {user_external_id!r}")
        if not channel_id:
            raise ValueError(f"Expected a non-empty value for `channel_id` but received {channel_id!r}")
        extra_headers = {"Accept": "*/*", **(extra_headers or {})}
        return await self._delete(
            f"/users/{user_external_id}/channel-settings/{channel_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=NoneType,
        )


class ChannelSettingsResourceWithRawResponse:
    def __init__(self, channel_settings: ChannelSettingsResource) -> None:
        self._channel_settings = channel_settings

        self.retrieve = to_raw_response_wrapper(
            channel_settings.retrieve,
        )
        self.update = to_raw_response_wrapper(
            channel_settings.update,
        )
        self.delete = to_raw_response_wrapper(
            channel_settings.delete,
        )


class AsyncChannelSettingsResourceWithRawResponse:
    def __init__(self, channel_settings: AsyncChannelSettingsResource) -> None:
        self._channel_settings = channel_settings

        self.retrieve = async_to_raw_response_wrapper(
            channel_settings.retrieve,
        )
        self.update = async_to_raw_response_wrapper(
            channel_settings.update,
        )
        self.delete = async_to_raw_response_wrapper(
            channel_settings.delete,
        )


class ChannelSettingsResourceWithStreamingResponse:
    def __init__(self, channel_settings: ChannelSettingsResource) -> None:
        self._channel_settings = channel_settings

        self.retrieve = to_streamed_response_wrapper(
            channel_settings.retrieve,
        )
        self.update = to_streamed_response_wrapper(
            channel_settings.update,
        )
        self.delete = to_streamed_response_wrapper(
            channel_settings.delete,
        )


class AsyncChannelSettingsResourceWithStreamingResponse:
    def __init__(self, channel_settings: AsyncChannelSettingsResource) -> None:
        self._channel_settings = channel_settings

        self.retrieve = async_to_streamed_response_wrapper(
            channel_settings.retrieve,
        )
        self.update = async_to_streamed_response_wrapper(
            channel_settings.update,
        )
        self.delete = async_to_streamed_response_wrapper(
            channel_settings.delete,
        )
