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

from __future__ import annotations

import typing_extensions
from typing import Any, 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, 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 ....._wrappers import ResultWrapper
from .....pagination import SyncV4PagePaginationArray, AsyncV4PagePaginationArray
from ....._base_client import AsyncPaginator, make_request_options
from .....types.firewall.waf.packages import group_edit_params, group_list_params
from .....types.firewall.waf.packages.group import Group
from .....types.firewall.waf.packages.group_get_response import GroupGetResponse
from .....types.firewall.waf.packages.group_edit_response import GroupEditResponse

__all__ = ["GroupsResource", "AsyncGroupsResource"]


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

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

    @typing_extensions.deprecated("deprecated")
    def list(
        self,
        package_id: str,
        *,
        zone_id: str,
        direction: Literal["asc", "desc"] | Omit = omit,
        match: Literal["any", "all"] | Omit = omit,
        mode: Literal["on", "off"] | Omit = omit,
        name: str | Omit = omit,
        order: Literal["mode", "rules_count"] | Omit = omit,
        page: float | Omit = omit,
        per_page: float | Omit = omit,
        rules_count: 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[Group]:
        """
        Fetches the WAF rule groups in a WAF package.

        **Note:** Applies only to the
        [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).

        Args:
          zone_id: Defines an identifier of a schema.

          package_id: Defines the unique identifier of a WAF package.

          direction: Defines the direction used to sort returned rule groups.

          match: Defines the condition for search requirements. When set to `all`, all the search
              requirements must match. When set to `any`, only one of the search requirements
              has to match.

          mode: Defines the state of the rules contained in the rule group. When `on`, the rules
              in the group are configurable/usable.

          name: Defines the name of the rule group.

          order: Defines the field used to sort returned rule groups.

          page: Defines the page number of paginated results.

          per_page: Defines the number of rule groups per page.

          rules_count: Defines the number of rules in the current rule 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 zone_id:
            raise ValueError(f"Expected a non-empty value for `zone_id` but received {zone_id!r}")
        if not package_id:
            raise ValueError(f"Expected a non-empty value for `package_id` but received {package_id!r}")
        return self._get_api_list(
            path_template(
                "/zones/{zone_id}/firewall/waf/packages/{package_id}/groups", zone_id=zone_id, package_id=package_id
            ),
            page=SyncV4PagePaginationArray[Group],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "direction": direction,
                        "match": match,
                        "mode": mode,
                        "name": name,
                        "order": order,
                        "page": page,
                        "per_page": per_page,
                        "rules_count": rules_count,
                    },
                    group_list_params.GroupListParams,
                ),
            ),
            model=Group,
        )

    @typing_extensions.deprecated("deprecated")
    def edit(
        self,
        group_id: str,
        *,
        zone_id: str,
        package_id: str,
        mode: Literal["on", "off"] | 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,
    ) -> GroupEditResponse:
        """Updates a WAF rule group.

        You can update the state (`mode` parameter) of a rule
        group.

        **Note:** Applies only to the
        [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).

        Args:
          zone_id: Defines an identifier of a schema.

          package_id: Defines the unique identifier of a WAF package.

          group_id: Defines the unique identifier of a WAF package.

          mode: Defines the state of the rules contained in the rule group. When `on`, the rules
              in the group are configurable/usable.

          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 zone_id:
            raise ValueError(f"Expected a non-empty value for `zone_id` but received {zone_id!r}")
        if not package_id:
            raise ValueError(f"Expected a non-empty value for `package_id` but received {package_id!r}")
        if not group_id:
            raise ValueError(f"Expected a non-empty value for `group_id` but received {group_id!r}")
        return cast(
            GroupEditResponse,
            self._patch(
                path_template(
                    "/zones/{zone_id}/firewall/waf/packages/{package_id}/groups/{group_id}",
                    zone_id=zone_id,
                    package_id=package_id,
                    group_id=group_id,
                ),
                body=maybe_transform({"mode": mode}, group_edit_params.GroupEditParams),
                options=make_request_options(
                    extra_headers=extra_headers,
                    extra_query=extra_query,
                    extra_body=extra_body,
                    timeout=timeout,
                    post_parser=ResultWrapper[GroupEditResponse]._unwrapper,
                ),
                cast_to=cast(
                    Any, ResultWrapper[GroupEditResponse]
                ),  # Union types cannot be passed in as arguments in the type system
            ),
        )

    @typing_extensions.deprecated("deprecated")
    def get(
        self,
        group_id: str,
        *,
        zone_id: str,
        package_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,
    ) -> GroupGetResponse:
        """
        Fetches the details of a WAF rule group.

        **Note:** Applies only to the
        [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).

        Args:
          zone_id: Defines an identifier of a schema.

          package_id: Defines the unique identifier of a WAF package.

          group_id: Defines the unique identifier of a WAF package.

          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 zone_id:
            raise ValueError(f"Expected a non-empty value for `zone_id` but received {zone_id!r}")
        if not package_id:
            raise ValueError(f"Expected a non-empty value for `package_id` but received {package_id!r}")
        if not group_id:
            raise ValueError(f"Expected a non-empty value for `group_id` but received {group_id!r}")
        return cast(
            GroupGetResponse,
            self._get(
                path_template(
                    "/zones/{zone_id}/firewall/waf/packages/{package_id}/groups/{group_id}",
                    zone_id=zone_id,
                    package_id=package_id,
                    group_id=group_id,
                ),
                options=make_request_options(
                    extra_headers=extra_headers,
                    extra_query=extra_query,
                    extra_body=extra_body,
                    timeout=timeout,
                    post_parser=ResultWrapper[GroupGetResponse]._unwrapper,
                ),
                cast_to=cast(
                    Any, ResultWrapper[GroupGetResponse]
                ),  # Union types cannot be passed in as arguments in the type system
            ),
        )


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

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

    @typing_extensions.deprecated("deprecated")
    def list(
        self,
        package_id: str,
        *,
        zone_id: str,
        direction: Literal["asc", "desc"] | Omit = omit,
        match: Literal["any", "all"] | Omit = omit,
        mode: Literal["on", "off"] | Omit = omit,
        name: str | Omit = omit,
        order: Literal["mode", "rules_count"] | Omit = omit,
        page: float | Omit = omit,
        per_page: float | Omit = omit,
        rules_count: 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[Group, AsyncV4PagePaginationArray[Group]]:
        """
        Fetches the WAF rule groups in a WAF package.

        **Note:** Applies only to the
        [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).

        Args:
          zone_id: Defines an identifier of a schema.

          package_id: Defines the unique identifier of a WAF package.

          direction: Defines the direction used to sort returned rule groups.

          match: Defines the condition for search requirements. When set to `all`, all the search
              requirements must match. When set to `any`, only one of the search requirements
              has to match.

          mode: Defines the state of the rules contained in the rule group. When `on`, the rules
              in the group are configurable/usable.

          name: Defines the name of the rule group.

          order: Defines the field used to sort returned rule groups.

          page: Defines the page number of paginated results.

          per_page: Defines the number of rule groups per page.

          rules_count: Defines the number of rules in the current rule 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 zone_id:
            raise ValueError(f"Expected a non-empty value for `zone_id` but received {zone_id!r}")
        if not package_id:
            raise ValueError(f"Expected a non-empty value for `package_id` but received {package_id!r}")
        return self._get_api_list(
            path_template(
                "/zones/{zone_id}/firewall/waf/packages/{package_id}/groups", zone_id=zone_id, package_id=package_id
            ),
            page=AsyncV4PagePaginationArray[Group],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "direction": direction,
                        "match": match,
                        "mode": mode,
                        "name": name,
                        "order": order,
                        "page": page,
                        "per_page": per_page,
                        "rules_count": rules_count,
                    },
                    group_list_params.GroupListParams,
                ),
            ),
            model=Group,
        )

    @typing_extensions.deprecated("deprecated")
    async def edit(
        self,
        group_id: str,
        *,
        zone_id: str,
        package_id: str,
        mode: Literal["on", "off"] | 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,
    ) -> GroupEditResponse:
        """Updates a WAF rule group.

        You can update the state (`mode` parameter) of a rule
        group.

        **Note:** Applies only to the
        [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).

        Args:
          zone_id: Defines an identifier of a schema.

          package_id: Defines the unique identifier of a WAF package.

          group_id: Defines the unique identifier of a WAF package.

          mode: Defines the state of the rules contained in the rule group. When `on`, the rules
              in the group are configurable/usable.

          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 zone_id:
            raise ValueError(f"Expected a non-empty value for `zone_id` but received {zone_id!r}")
        if not package_id:
            raise ValueError(f"Expected a non-empty value for `package_id` but received {package_id!r}")
        if not group_id:
            raise ValueError(f"Expected a non-empty value for `group_id` but received {group_id!r}")
        return cast(
            GroupEditResponse,
            await self._patch(
                path_template(
                    "/zones/{zone_id}/firewall/waf/packages/{package_id}/groups/{group_id}",
                    zone_id=zone_id,
                    package_id=package_id,
                    group_id=group_id,
                ),
                body=await async_maybe_transform({"mode": mode}, group_edit_params.GroupEditParams),
                options=make_request_options(
                    extra_headers=extra_headers,
                    extra_query=extra_query,
                    extra_body=extra_body,
                    timeout=timeout,
                    post_parser=ResultWrapper[GroupEditResponse]._unwrapper,
                ),
                cast_to=cast(
                    Any, ResultWrapper[GroupEditResponse]
                ),  # Union types cannot be passed in as arguments in the type system
            ),
        )

    @typing_extensions.deprecated("deprecated")
    async def get(
        self,
        group_id: str,
        *,
        zone_id: str,
        package_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,
    ) -> GroupGetResponse:
        """
        Fetches the details of a WAF rule group.

        **Note:** Applies only to the
        [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).

        Args:
          zone_id: Defines an identifier of a schema.

          package_id: Defines the unique identifier of a WAF package.

          group_id: Defines the unique identifier of a WAF package.

          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 zone_id:
            raise ValueError(f"Expected a non-empty value for `zone_id` but received {zone_id!r}")
        if not package_id:
            raise ValueError(f"Expected a non-empty value for `package_id` but received {package_id!r}")
        if not group_id:
            raise ValueError(f"Expected a non-empty value for `group_id` but received {group_id!r}")
        return cast(
            GroupGetResponse,
            await self._get(
                path_template(
                    "/zones/{zone_id}/firewall/waf/packages/{package_id}/groups/{group_id}",
                    zone_id=zone_id,
                    package_id=package_id,
                    group_id=group_id,
                ),
                options=make_request_options(
                    extra_headers=extra_headers,
                    extra_query=extra_query,
                    extra_body=extra_body,
                    timeout=timeout,
                    post_parser=ResultWrapper[GroupGetResponse]._unwrapper,
                ),
                cast_to=cast(
                    Any, ResultWrapper[GroupGetResponse]
                ),  # Union types cannot be passed in as arguments in the type system
            ),
        )


class GroupsResourceWithRawResponse:
    def __init__(self, groups: GroupsResource) -> None:
        self._groups = groups

        self.list = (  # pyright: ignore[reportDeprecated]
            to_raw_response_wrapper(
                groups.list,  # pyright: ignore[reportDeprecated],
            )
        )
        self.edit = (  # pyright: ignore[reportDeprecated]
            to_raw_response_wrapper(
                groups.edit,  # pyright: ignore[reportDeprecated],
            )
        )
        self.get = (  # pyright: ignore[reportDeprecated]
            to_raw_response_wrapper(
                groups.get,  # pyright: ignore[reportDeprecated],
            )
        )


class AsyncGroupsResourceWithRawResponse:
    def __init__(self, groups: AsyncGroupsResource) -> None:
        self._groups = groups

        self.list = (  # pyright: ignore[reportDeprecated]
            async_to_raw_response_wrapper(
                groups.list,  # pyright: ignore[reportDeprecated],
            )
        )
        self.edit = (  # pyright: ignore[reportDeprecated]
            async_to_raw_response_wrapper(
                groups.edit,  # pyright: ignore[reportDeprecated],
            )
        )
        self.get = (  # pyright: ignore[reportDeprecated]
            async_to_raw_response_wrapper(
                groups.get,  # pyright: ignore[reportDeprecated],
            )
        )


class GroupsResourceWithStreamingResponse:
    def __init__(self, groups: GroupsResource) -> None:
        self._groups = groups

        self.list = (  # pyright: ignore[reportDeprecated]
            to_streamed_response_wrapper(
                groups.list,  # pyright: ignore[reportDeprecated],
            )
        )
        self.edit = (  # pyright: ignore[reportDeprecated]
            to_streamed_response_wrapper(
                groups.edit,  # pyright: ignore[reportDeprecated],
            )
        )
        self.get = (  # pyright: ignore[reportDeprecated]
            to_streamed_response_wrapper(
                groups.get,  # pyright: ignore[reportDeprecated],
            )
        )


class AsyncGroupsResourceWithStreamingResponse:
    def __init__(self, groups: AsyncGroupsResource) -> None:
        self._groups = groups

        self.list = (  # pyright: ignore[reportDeprecated]
            async_to_streamed_response_wrapper(
                groups.list,  # pyright: ignore[reportDeprecated],
            )
        )
        self.edit = (  # pyright: ignore[reportDeprecated]
            async_to_streamed_response_wrapper(
                groups.edit,  # pyright: ignore[reportDeprecated],
            )
        )
        self.get = (  # pyright: ignore[reportDeprecated]
            async_to_streamed_response_wrapper(
                groups.get,  # pyright: ignore[reportDeprecated],
            )
        )
