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

from __future__ import annotations

from typing import Type, Iterable, Optional, cast
from typing_extensions import Literal

import httpx

from ...._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
from ...._utils import path_template, 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 ...._wrappers import ResultWrapper
from ....pagination import SyncSinglePage, AsyncSinglePage, SyncV4PagePaginationArray, AsyncV4PagePaginationArray
from ...._base_client import AsyncPaginator, make_request_options
from ....types.iam.user_groups import member_list_params, member_create_params, member_update_params
from ....types.iam.user_groups.member_get_response import MemberGetResponse
from ....types.iam.user_groups.member_list_response import MemberListResponse
from ....types.iam.user_groups.member_create_response import MemberCreateResponse
from ....types.iam.user_groups.member_delete_response import MemberDeleteResponse
from ....types.iam.user_groups.member_update_response import MemberUpdateResponse

__all__ = ["MembersResource", "AsyncMembersResource"]


class MembersResource(SyncAPIResource):
    @cached_property
    def with_raw_response(self) -> MembersResourceWithRawResponse:
        """
        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/cloudflare/cloudflare-python#accessing-raw-response-data-eg-headers
        """
        return MembersResourceWithRawResponse(self)

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

        For more information, see https://www.github.com/cloudflare/cloudflare-python#with_streaming_response
        """
        return MembersResourceWithStreamingResponse(self)

    def create(
        self,
        user_group_id: str,
        *,
        account_id: str,
        members: Iterable[member_create_params.Member],
        # 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,
    ) -> SyncSinglePage[MemberCreateResponse]:
        """
        Add members to a User Group.

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members",
                account_id=account_id,
                user_group_id=user_group_id,
            ),
            page=SyncSinglePage[MemberCreateResponse],
            body=maybe_transform(members, Iterable[member_create_params.Member]),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            model=MemberCreateResponse,
            method="post",
        )

    def update(
        self,
        user_group_id: str,
        *,
        account_id: str,
        members: Iterable[member_update_params.Member],
        # 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,
    ) -> SyncSinglePage[MemberUpdateResponse]:
        """
        Replace the set of members attached to a User Group.

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          members: Set/Replace members to a user group.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members",
                account_id=account_id,
                user_group_id=user_group_id,
            ),
            page=SyncSinglePage[MemberUpdateResponse],
            body=maybe_transform(members, Iterable[member_update_params.Member]),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            model=MemberUpdateResponse,
            method="put",
        )

    def list(
        self,
        user_group_id: str,
        *,
        account_id: str,
        direction: Literal["asc", "desc"] | Omit = omit,
        fuzzy_email: str | Omit = omit,
        page: float | Omit = omit,
        per_page: float | 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,
    ) -> SyncV4PagePaginationArray[MemberListResponse]:
        """
        List all the members attached to a user group.

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          direction: The sort order of returned user group members by email.

          fuzzy_email: A string used for filtering members by partial email match.

          page: Page number of paginated results.

          per_page: Maximum number of results per page.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members",
                account_id=account_id,
                user_group_id=user_group_id,
            ),
            page=SyncV4PagePaginationArray[MemberListResponse],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "direction": direction,
                        "fuzzy_email": fuzzy_email,
                        "page": page,
                        "per_page": per_page,
                    },
                    member_list_params.MemberListParams,
                ),
            ),
            model=MemberListResponse,
        )

    def delete(
        self,
        member_id: str,
        *,
        account_id: str,
        user_group_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,
    ) -> Optional[MemberDeleteResponse]:
        """
        Remove a member from User Group

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          member_id: The identifier of an existing account Member.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        if not member_id:
            raise ValueError(f"Expected a non-empty value for `member_id` but received {member_id!r}")
        return self._delete(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members/{member_id}",
                account_id=account_id,
                user_group_id=user_group_id,
                member_id=member_id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[MemberDeleteResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[MemberDeleteResponse]], ResultWrapper[MemberDeleteResponse]),
        )

    def get(
        self,
        member_id: str,
        *,
        account_id: str,
        user_group_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,
    ) -> Optional[MemberGetResponse]:
        """
        Get details of a specific member in a user group.

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          member_id: The identifier of an existing account Member.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        if not member_id:
            raise ValueError(f"Expected a non-empty value for `member_id` but received {member_id!r}")
        return self._get(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members/{member_id}",
                account_id=account_id,
                user_group_id=user_group_id,
                member_id=member_id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[MemberGetResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[MemberGetResponse]], ResultWrapper[MemberGetResponse]),
        )


class AsyncMembersResource(AsyncAPIResource):
    @cached_property
    def with_raw_response(self) -> AsyncMembersResourceWithRawResponse:
        """
        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/cloudflare/cloudflare-python#accessing-raw-response-data-eg-headers
        """
        return AsyncMembersResourceWithRawResponse(self)

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

        For more information, see https://www.github.com/cloudflare/cloudflare-python#with_streaming_response
        """
        return AsyncMembersResourceWithStreamingResponse(self)

    def create(
        self,
        user_group_id: str,
        *,
        account_id: str,
        members: Iterable[member_create_params.Member],
        # 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[MemberCreateResponse, AsyncSinglePage[MemberCreateResponse]]:
        """
        Add members to a User Group.

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members",
                account_id=account_id,
                user_group_id=user_group_id,
            ),
            page=AsyncSinglePage[MemberCreateResponse],
            body=maybe_transform(members, Iterable[member_create_params.Member]),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            model=MemberCreateResponse,
            method="post",
        )

    def update(
        self,
        user_group_id: str,
        *,
        account_id: str,
        members: Iterable[member_update_params.Member],
        # 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[MemberUpdateResponse, AsyncSinglePage[MemberUpdateResponse]]:
        """
        Replace the set of members attached to a User Group.

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          members: Set/Replace members to a user group.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members",
                account_id=account_id,
                user_group_id=user_group_id,
            ),
            page=AsyncSinglePage[MemberUpdateResponse],
            body=maybe_transform(members, Iterable[member_update_params.Member]),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            model=MemberUpdateResponse,
            method="put",
        )

    def list(
        self,
        user_group_id: str,
        *,
        account_id: str,
        direction: Literal["asc", "desc"] | Omit = omit,
        fuzzy_email: str | Omit = omit,
        page: float | Omit = omit,
        per_page: float | 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[MemberListResponse, AsyncV4PagePaginationArray[MemberListResponse]]:
        """
        List all the members attached to a user group.

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          direction: The sort order of returned user group members by email.

          fuzzy_email: A string used for filtering members by partial email match.

          page: Page number of paginated results.

          per_page: Maximum number of results per page.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members",
                account_id=account_id,
                user_group_id=user_group_id,
            ),
            page=AsyncV4PagePaginationArray[MemberListResponse],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "direction": direction,
                        "fuzzy_email": fuzzy_email,
                        "page": page,
                        "per_page": per_page,
                    },
                    member_list_params.MemberListParams,
                ),
            ),
            model=MemberListResponse,
        )

    async def delete(
        self,
        member_id: str,
        *,
        account_id: str,
        user_group_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,
    ) -> Optional[MemberDeleteResponse]:
        """
        Remove a member from User Group

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          member_id: The identifier of an existing account Member.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        if not member_id:
            raise ValueError(f"Expected a non-empty value for `member_id` but received {member_id!r}")
        return await self._delete(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members/{member_id}",
                account_id=account_id,
                user_group_id=user_group_id,
                member_id=member_id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[MemberDeleteResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[MemberDeleteResponse]], ResultWrapper[MemberDeleteResponse]),
        )

    async def get(
        self,
        member_id: str,
        *,
        account_id: str,
        user_group_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,
    ) -> Optional[MemberGetResponse]:
        """
        Get details of a specific member in a user group.

        Args:
          account_id: Account identifier tag.

          user_group_id: User Group identifier tag.

          member_id: The identifier of an existing account Member.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not user_group_id:
            raise ValueError(f"Expected a non-empty value for `user_group_id` but received {user_group_id!r}")
        if not member_id:
            raise ValueError(f"Expected a non-empty value for `member_id` but received {member_id!r}")
        return await self._get(
            path_template(
                "/accounts/{account_id}/iam/user_groups/{user_group_id}/members/{member_id}",
                account_id=account_id,
                user_group_id=user_group_id,
                member_id=member_id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[MemberGetResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[MemberGetResponse]], ResultWrapper[MemberGetResponse]),
        )


class MembersResourceWithRawResponse:
    def __init__(self, members: MembersResource) -> None:
        self._members = members

        self.create = to_raw_response_wrapper(
            members.create,
        )
        self.update = to_raw_response_wrapper(
            members.update,
        )
        self.list = to_raw_response_wrapper(
            members.list,
        )
        self.delete = to_raw_response_wrapper(
            members.delete,
        )
        self.get = to_raw_response_wrapper(
            members.get,
        )


class AsyncMembersResourceWithRawResponse:
    def __init__(self, members: AsyncMembersResource) -> None:
        self._members = members

        self.create = async_to_raw_response_wrapper(
            members.create,
        )
        self.update = async_to_raw_response_wrapper(
            members.update,
        )
        self.list = async_to_raw_response_wrapper(
            members.list,
        )
        self.delete = async_to_raw_response_wrapper(
            members.delete,
        )
        self.get = async_to_raw_response_wrapper(
            members.get,
        )


class MembersResourceWithStreamingResponse:
    def __init__(self, members: MembersResource) -> None:
        self._members = members

        self.create = to_streamed_response_wrapper(
            members.create,
        )
        self.update = to_streamed_response_wrapper(
            members.update,
        )
        self.list = to_streamed_response_wrapper(
            members.list,
        )
        self.delete = to_streamed_response_wrapper(
            members.delete,
        )
        self.get = to_streamed_response_wrapper(
            members.get,
        )


class AsyncMembersResourceWithStreamingResponse:
    def __init__(self, members: AsyncMembersResource) -> None:
        self._members = members

        self.create = async_to_streamed_response_wrapper(
            members.create,
        )
        self.update = async_to_streamed_response_wrapper(
            members.update,
        )
        self.list = async_to_streamed_response_wrapper(
            members.list,
        )
        self.delete = async_to_streamed_response_wrapper(
            members.delete,
        )
        self.get = async_to_streamed_response_wrapper(
            members.get,
        )
