# 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.not_found_error import NotFoundError
from ..types.retention_rule_page_public import RetentionRulePagePublic
from ..types.retention_rule_public import RetentionRulePublic
from .types.retention_rule_write_retention import RetentionRuleWriteRetention

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


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

    def find_retention_rules(
        self,
        *,
        page: typing.Optional[int] = None,
        size: typing.Optional[int] = None,
        include_inactive: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[RetentionRulePagePublic]:
        """
        List retention rules for the caller's workspace. Defaults to active only.

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

        size : typing.Optional[int]

        include_inactive : typing.Optional[bool]

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

        Returns
        -------
        HttpResponse[RetentionRulePagePublic]
            Retention rules page
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/private/retention/rules",
            method="GET",
            params={
                "page": page,
                "size": size,
                "include_inactive": include_inactive,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetentionRulePagePublic,
                    parse_obj_as(
                        type_=RetentionRulePagePublic,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 create_retention_rule(
        self,
        *,
        retention: RetentionRuleWriteRetention,
        project_id: typing.Optional[str] = OMIT,
        organization_level: typing.Optional[bool] = OMIT,
        apply_to_past: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[RetentionRulePublic]:
        """
        Create a new retention rule. Auto-deactivates any existing active rule for the same scope.

        Parameters
        ----------
        retention : RetentionRuleWriteRetention

        project_id : typing.Optional[str]

        organization_level : typing.Optional[bool]
            Set to true to create an organization-level rule

        apply_to_past : typing.Optional[bool]

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

        Returns
        -------
        HttpResponse[RetentionRulePublic]
            Created
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/private/retention/rules",
            method="POST",
            json={
                "project_id": project_id,
                "organization_level": organization_level,
                "retention": retention,
                "apply_to_past": apply_to_past,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetentionRulePublic,
                    parse_obj_as(
                        type_=RetentionRulePublic,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 get_retention_rule_by_id(
        self, rule_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[RetentionRulePublic]:
        """
        Get a specific retention rule by id

        Parameters
        ----------
        rule_id : str

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

        Returns
        -------
        HttpResponse[RetentionRulePublic]
            Retention rule
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/private/retention/rules/{jsonable_encoder(rule_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetentionRulePublic,
                    parse_obj_as(
                        type_=RetentionRulePublic,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 deactivate_retention_rule(
        self, rule_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[None]:
        """
        Soft-deactivate a retention rule (sets enabled=false). Rules are never hard-deleted for audit trail.

        Parameters
        ----------
        rule_id : str

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/private/retention/rules/{jsonable_encoder(rule_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 AsyncRawRetentionRulesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def find_retention_rules(
        self,
        *,
        page: typing.Optional[int] = None,
        size: typing.Optional[int] = None,
        include_inactive: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[RetentionRulePagePublic]:
        """
        List retention rules for the caller's workspace. Defaults to active only.

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

        size : typing.Optional[int]

        include_inactive : typing.Optional[bool]

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

        Returns
        -------
        AsyncHttpResponse[RetentionRulePagePublic]
            Retention rules page
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/private/retention/rules",
            method="GET",
            params={
                "page": page,
                "size": size,
                "include_inactive": include_inactive,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetentionRulePagePublic,
                    parse_obj_as(
                        type_=RetentionRulePagePublic,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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 create_retention_rule(
        self,
        *,
        retention: RetentionRuleWriteRetention,
        project_id: typing.Optional[str] = OMIT,
        organization_level: typing.Optional[bool] = OMIT,
        apply_to_past: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[RetentionRulePublic]:
        """
        Create a new retention rule. Auto-deactivates any existing active rule for the same scope.

        Parameters
        ----------
        retention : RetentionRuleWriteRetention

        project_id : typing.Optional[str]

        organization_level : typing.Optional[bool]
            Set to true to create an organization-level rule

        apply_to_past : typing.Optional[bool]

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

        Returns
        -------
        AsyncHttpResponse[RetentionRulePublic]
            Created
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/private/retention/rules",
            method="POST",
            json={
                "project_id": project_id,
                "organization_level": organization_level,
                "retention": retention,
                "apply_to_past": apply_to_past,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetentionRulePublic,
                    parse_obj_as(
                        type_=RetentionRulePublic,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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 get_retention_rule_by_id(
        self, rule_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[RetentionRulePublic]:
        """
        Get a specific retention rule by id

        Parameters
        ----------
        rule_id : str

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

        Returns
        -------
        AsyncHttpResponse[RetentionRulePublic]
            Retention rule
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/private/retention/rules/{jsonable_encoder(rule_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetentionRulePublic,
                    parse_obj_as(
                        type_=RetentionRulePublic,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 deactivate_retention_rule(
        self, rule_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[None]:
        """
        Soft-deactivate a retention rule (sets enabled=false). Rules are never hard-deleted for audit trail.

        Parameters
        ----------
        rule_id : str

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/private/retention/rules/{jsonable_encoder(rule_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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)
