# This file was auto-generated by Fern from our API Definition.

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.apply_mode import ApplyMode
from ..types.rule_category import RuleCategory
from ..types.rule_detail_response import RuleDetailResponse
from ..types.rule_list_paginated_response import RuleListPaginatedResponse
from ..types.rule_scope import RuleScope
from ..types.rule_scope_api import RuleScopeApi
from ..types.rule_type import RuleType

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class RawRuleOperationsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def update_rule(
        self,
        org_id: str,
        rule_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        content: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        category: typing.Optional[RuleCategory] = OMIT,
        rule_type: typing.Optional[RuleType] = OMIT,
        scope: typing.Optional[RuleScopeApi] = OMIT,
        apply_mode: typing.Optional[ApplyMode] = OMIT,
        is_public: typing.Optional[bool] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[RuleDetailResponse]:
        """
        Update an existing rule. Requires edit or owner access.

        Parameters
        ----------
        org_id : str

        rule_id : str

        name : typing.Optional[str]

        content : typing.Optional[str]

        description : typing.Optional[str]

        category : typing.Optional[RuleCategory]

        rule_type : typing.Optional[RuleType]

        scope : typing.Optional[RuleScopeApi]
            Rule scope (global scope restricted - admin only)

        apply_mode : typing.Optional[ApplyMode]

        is_public : typing.Optional[bool]

        is_active : typing.Optional[bool]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[RuleDetailResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(org_id)}/rules/{jsonable_encoder(rule_id)}",
            method="PUT",
            json={
                "name": name,
                "content": content,
                "description": description,
                "category": category,
                "rule_type": rule_type,
                "scope": scope,
                "apply_mode": apply_mode,
                "is_public": is_public,
                "is_active": is_active,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RuleDetailResponse,
                    parse_obj_as(
                        type_=RuleDetailResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def delete_rule(
        self,
        org_id: str,
        rule_id: str,
        *,
        force: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[None]:
        """
        Soft delete a rule. Requires owner access.

        Parameters
        ----------
        org_id : str

        rule_id : str

        force : typing.Optional[bool]
            Force delete even if rule has active attachments

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(org_id)}/rules/{jsonable_encoder(rule_id)}",
            method="DELETE",
            params={
                "force": force,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def list_public_rules(
        self,
        *,
        skip: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        category: typing.Optional[RuleCategory] = None,
        rule_type: typing.Optional[RuleType] = None,
        scope: typing.Optional[RuleScope] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[RuleListPaginatedResponse]:
        """
        List all public rules from all organizations.

        Parameters
        ----------
        skip : typing.Optional[int]

        limit : typing.Optional[int]

        category : typing.Optional[RuleCategory]

        rule_type : typing.Optional[RuleType]

        scope : typing.Optional[RuleScope]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[RuleListPaginatedResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/rules/public",
            method="GET",
            params={
                "skip": skip,
                "limit": limit,
                "category": category,
                "rule_type": rule_type,
                "scope": scope,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RuleListPaginatedResponse,
                    parse_obj_as(
                        type_=RuleListPaginatedResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def duplicate_rule(
        self, org_id: str, rule_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[RuleDetailResponse]:
        """
        Create a copy of an existing rule with a new ID and name suffix.

        Parameters
        ----------
        org_id : str

        rule_id : str

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[RuleDetailResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(org_id)}/rules/{jsonable_encoder(rule_id)}/duplicate",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RuleDetailResponse,
                    parse_obj_as(
                        type_=RuleDetailResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawRuleOperationsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def update_rule(
        self,
        org_id: str,
        rule_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        content: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        category: typing.Optional[RuleCategory] = OMIT,
        rule_type: typing.Optional[RuleType] = OMIT,
        scope: typing.Optional[RuleScopeApi] = OMIT,
        apply_mode: typing.Optional[ApplyMode] = OMIT,
        is_public: typing.Optional[bool] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[RuleDetailResponse]:
        """
        Update an existing rule. Requires edit or owner access.

        Parameters
        ----------
        org_id : str

        rule_id : str

        name : typing.Optional[str]

        content : typing.Optional[str]

        description : typing.Optional[str]

        category : typing.Optional[RuleCategory]

        rule_type : typing.Optional[RuleType]

        scope : typing.Optional[RuleScopeApi]
            Rule scope (global scope restricted - admin only)

        apply_mode : typing.Optional[ApplyMode]

        is_public : typing.Optional[bool]

        is_active : typing.Optional[bool]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[RuleDetailResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(org_id)}/rules/{jsonable_encoder(rule_id)}",
            method="PUT",
            json={
                "name": name,
                "content": content,
                "description": description,
                "category": category,
                "rule_type": rule_type,
                "scope": scope,
                "apply_mode": apply_mode,
                "is_public": is_public,
                "is_active": is_active,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RuleDetailResponse,
                    parse_obj_as(
                        type_=RuleDetailResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def delete_rule(
        self,
        org_id: str,
        rule_id: str,
        *,
        force: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[None]:
        """
        Soft delete a rule. Requires owner access.

        Parameters
        ----------
        org_id : str

        rule_id : str

        force : typing.Optional[bool]
            Force delete even if rule has active attachments

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(org_id)}/rules/{jsonable_encoder(rule_id)}",
            method="DELETE",
            params={
                "force": force,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def list_public_rules(
        self,
        *,
        skip: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        category: typing.Optional[RuleCategory] = None,
        rule_type: typing.Optional[RuleType] = None,
        scope: typing.Optional[RuleScope] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[RuleListPaginatedResponse]:
        """
        List all public rules from all organizations.

        Parameters
        ----------
        skip : typing.Optional[int]

        limit : typing.Optional[int]

        category : typing.Optional[RuleCategory]

        rule_type : typing.Optional[RuleType]

        scope : typing.Optional[RuleScope]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[RuleListPaginatedResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/rules/public",
            method="GET",
            params={
                "skip": skip,
                "limit": limit,
                "category": category,
                "rule_type": rule_type,
                "scope": scope,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RuleListPaginatedResponse,
                    parse_obj_as(
                        type_=RuleListPaginatedResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def duplicate_rule(
        self, org_id: str, rule_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[RuleDetailResponse]:
        """
        Create a copy of an existing rule with a new ID and name suffix.

        Parameters
        ----------
        org_id : str

        rule_id : str

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[RuleDetailResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(org_id)}/rules/{jsonable_encoder(rule_id)}/duplicate",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RuleDetailResponse,
                    parse_obj_as(
                        type_=RuleDetailResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
