# 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.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.bad_request_error import BadRequestError
from .types.list_all_entities_request_sort_by import ListAllEntitiesRequestSortBy
from .types.list_all_entities_request_status import ListAllEntitiesRequestStatus
from .types.list_all_entities_response import ListAllEntitiesResponse


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

    def list_all_entities(
        self,
        *,
        page: typing.Optional[int] = None,
        page_limit: typing.Optional[int] = None,
        name: typing.Optional[str] = None,
        status: typing.Optional[ListAllEntitiesRequestStatus] = None,
        sort_by: typing.Optional[ListAllEntitiesRequestSortBy] = None,
        sort_option: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ListAllEntitiesResponse]:
        """
        This method returns a list of entities from all entity collections.
        This is an internal API primarily used by the search interface.

        Parameters
        ----------
        page : typing.Optional[int]
            A number that identifies the page to retrieve.

            **Default**: `1`.

        page_limit : typing.Optional[int]
            The number of items to return on each page.

            **Default**: `10`.
            **Max**: `50`.

        name : typing.Optional[str]
            Filter entities by name.

        status : typing.Optional[ListAllEntitiesRequestStatus]
            Filter entities by status.

        sort_by : typing.Optional[ListAllEntitiesRequestSortBy]
            Field to sort by.

        sort_option : typing.Optional[str]
            The sorting direction. The following options are available:
            - `asc`
            - `desc`

            **Default**: `desc`.

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

        Returns
        -------
        HttpResponse[ListAllEntitiesResponse]
            The entities have been successfully retrieved.
        """
        _response = self._client_wrapper.httpx_client.request(
            "entities",
            method="GET",
            params={
                "page": page,
                "page_limit": page_limit,
                "name": name,
                "status": status,
                "sort_by": sort_by,
                "sort_option": sort_option,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListAllEntitiesResponse,
                    parse_obj_as(
                        type_=ListAllEntitiesResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    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 AsyncRawManageEntitiesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list_all_entities(
        self,
        *,
        page: typing.Optional[int] = None,
        page_limit: typing.Optional[int] = None,
        name: typing.Optional[str] = None,
        status: typing.Optional[ListAllEntitiesRequestStatus] = None,
        sort_by: typing.Optional[ListAllEntitiesRequestSortBy] = None,
        sort_option: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ListAllEntitiesResponse]:
        """
        This method returns a list of entities from all entity collections.
        This is an internal API primarily used by the search interface.

        Parameters
        ----------
        page : typing.Optional[int]
            A number that identifies the page to retrieve.

            **Default**: `1`.

        page_limit : typing.Optional[int]
            The number of items to return on each page.

            **Default**: `10`.
            **Max**: `50`.

        name : typing.Optional[str]
            Filter entities by name.

        status : typing.Optional[ListAllEntitiesRequestStatus]
            Filter entities by status.

        sort_by : typing.Optional[ListAllEntitiesRequestSortBy]
            Field to sort by.

        sort_option : typing.Optional[str]
            The sorting direction. The following options are available:
            - `asc`
            - `desc`

            **Default**: `desc`.

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

        Returns
        -------
        AsyncHttpResponse[ListAllEntitiesResponse]
            The entities have been successfully retrieved.
        """
        _response = await self._client_wrapper.httpx_client.request(
            "entities",
            method="GET",
            params={
                "page": page,
                "page_limit": page_limit,
                "name": name,
                "status": status,
                "sort_by": sort_by,
                "sort_option": sort_option,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListAllEntitiesResponse,
                    parse_obj_as(
                        type_=ListAllEntitiesResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    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)
