# 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.app_api_v1schemas_audit_log_v2audit_log_list_response import AppApiV1SchemasAuditLogV2AuditLogListResponse


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

    def query_audit_log(
        self,
        organization_id: str,
        *,
        entity_type: typing.Optional[str] = None,
        entity_id: typing.Optional[str] = None,
        action_type: typing.Optional[str] = None,
        actor_id: typing.Optional[str] = None,
        target_id: typing.Optional[str] = None,
        department_id: typing.Optional[str] = None,
        from_date: typing.Optional[str] = None,
        to_date: typing.Optional[str] = None,
        search_term: typing.Optional[str] = None,
        skip: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[AppApiV1SchemasAuditLogV2AuditLogListResponse]:
        """
        Query audit log entries with filters and pagination.

        Parameters
        ----------
        organization_id : str

        entity_type : typing.Optional[str]

        entity_id : typing.Optional[str]

        action_type : typing.Optional[str]

        actor_id : typing.Optional[str]

        target_id : typing.Optional[str]

        department_id : typing.Optional[str]

        from_date : typing.Optional[str]

        to_date : typing.Optional[str]

        search_term : typing.Optional[str]

        skip : typing.Optional[int]

        limit : typing.Optional[int]

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

        Returns
        -------
        HttpResponse[AppApiV1SchemasAuditLogV2AuditLogListResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(organization_id)}/audit-log",
            method="GET",
            params={
                "entity_type": entity_type,
                "entity_id": entity_id,
                "action_type": action_type,
                "actor_id": actor_id,
                "target_id": target_id,
                "department_id": department_id,
                "from_date": from_date,
                "to_date": to_date,
                "search_term": search_term,
                "skip": skip,
                "limit": limit,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AppApiV1SchemasAuditLogV2AuditLogListResponse,
                    parse_obj_as(
                        type_=AppApiV1SchemasAuditLogV2AuditLogListResponse,  # 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 export_audit_log(
        self,
        organization_id: str,
        *,
        entity_type: typing.Optional[str] = None,
        entity_id: typing.Optional[str] = None,
        action_type: typing.Optional[str] = None,
        actor_id: typing.Optional[str] = None,
        target_id: typing.Optional[str] = None,
        department_id: typing.Optional[str] = None,
        from_date: typing.Optional[str] = None,
        to_date: typing.Optional[str] = None,
        search_term: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Any]:
        """
        Export audit log entries as CSV.

        Parameters
        ----------
        organization_id : str

        entity_type : typing.Optional[str]

        entity_id : typing.Optional[str]

        action_type : typing.Optional[str]

        actor_id : typing.Optional[str]

        target_id : typing.Optional[str]

        department_id : typing.Optional[str]

        from_date : typing.Optional[str]

        to_date : typing.Optional[str]

        search_term : typing.Optional[str]

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

        Returns
        -------
        HttpResponse[typing.Any]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(organization_id)}/audit-log/export",
            method="GET",
            params={
                "entity_type": entity_type,
                "entity_id": entity_id,
                "action_type": action_type,
                "actor_id": actor_id,
                "target_id": target_id,
                "department_id": department_id,
                "from_date": from_date,
                "to_date": to_date,
                "search_term": search_term,
            },
            request_options=request_options,
        )
        try:
            if _response is None or not _response.text.strip():
                return HttpResponse(response=_response, data=None)
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Any,
                    parse_obj_as(
                        type_=typing.Any,  # 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 AsyncRawAuditLogV2Client:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def query_audit_log(
        self,
        organization_id: str,
        *,
        entity_type: typing.Optional[str] = None,
        entity_id: typing.Optional[str] = None,
        action_type: typing.Optional[str] = None,
        actor_id: typing.Optional[str] = None,
        target_id: typing.Optional[str] = None,
        department_id: typing.Optional[str] = None,
        from_date: typing.Optional[str] = None,
        to_date: typing.Optional[str] = None,
        search_term: typing.Optional[str] = None,
        skip: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[AppApiV1SchemasAuditLogV2AuditLogListResponse]:
        """
        Query audit log entries with filters and pagination.

        Parameters
        ----------
        organization_id : str

        entity_type : typing.Optional[str]

        entity_id : typing.Optional[str]

        action_type : typing.Optional[str]

        actor_id : typing.Optional[str]

        target_id : typing.Optional[str]

        department_id : typing.Optional[str]

        from_date : typing.Optional[str]

        to_date : typing.Optional[str]

        search_term : typing.Optional[str]

        skip : typing.Optional[int]

        limit : typing.Optional[int]

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

        Returns
        -------
        AsyncHttpResponse[AppApiV1SchemasAuditLogV2AuditLogListResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(organization_id)}/audit-log",
            method="GET",
            params={
                "entity_type": entity_type,
                "entity_id": entity_id,
                "action_type": action_type,
                "actor_id": actor_id,
                "target_id": target_id,
                "department_id": department_id,
                "from_date": from_date,
                "to_date": to_date,
                "search_term": search_term,
                "skip": skip,
                "limit": limit,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AppApiV1SchemasAuditLogV2AuditLogListResponse,
                    parse_obj_as(
                        type_=AppApiV1SchemasAuditLogV2AuditLogListResponse,  # 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 export_audit_log(
        self,
        organization_id: str,
        *,
        entity_type: typing.Optional[str] = None,
        entity_id: typing.Optional[str] = None,
        action_type: typing.Optional[str] = None,
        actor_id: typing.Optional[str] = None,
        target_id: typing.Optional[str] = None,
        department_id: typing.Optional[str] = None,
        from_date: typing.Optional[str] = None,
        to_date: typing.Optional[str] = None,
        search_term: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Any]:
        """
        Export audit log entries as CSV.

        Parameters
        ----------
        organization_id : str

        entity_type : typing.Optional[str]

        entity_id : typing.Optional[str]

        action_type : typing.Optional[str]

        actor_id : typing.Optional[str]

        target_id : typing.Optional[str]

        department_id : typing.Optional[str]

        from_date : typing.Optional[str]

        to_date : typing.Optional[str]

        search_term : typing.Optional[str]

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

        Returns
        -------
        AsyncHttpResponse[typing.Any]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/organizations/{jsonable_encoder(organization_id)}/audit-log/export",
            method="GET",
            params={
                "entity_type": entity_type,
                "entity_id": entity_id,
                "action_type": action_type,
                "actor_id": actor_id,
                "target_id": target_id,
                "department_id": department_id,
                "from_date": from_date,
                "to_date": to_date,
                "search_term": search_term,
            },
            request_options=request_options,
        )
        try:
            if _response is None or not _response.text.strip():
                return AsyncHttpResponse(response=_response, data=None)
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Any,
                    parse_obj_as(
                        type_=typing.Any,  # 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)
