# 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 ...types.paginated_folder_entity_list import PaginatedFolderEntityList
from .types.folder_entities_list_request_entity_status import FolderEntitiesListRequestEntityStatus

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


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

    def list(
        self,
        *,
        parent_folder_id: str,
        entity_status: typing.Optional[FolderEntitiesListRequestEntityStatus] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        ordering: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[PaginatedFolderEntityList]:
        """
        List all folder entities within a specified folder.

        Parameters
        ----------
        parent_folder_id : str
            Filter down to only those entities whose parent folder has the specified ID.

            To filter by an entity's parent folder, provide the ID of the parent folder. To filter by the root directory, provide
            a string representing the entity type of the root directory. Supported root directories include:
            - PROMPT_SANDBOX
            - WORKFLOW_SANDBOX
            - DOCUMENT_INDEX
            - TEST_SUITE

        entity_status : typing.Optional[FolderEntitiesListRequestEntityStatus]
            Filter down to only those objects whose entities have a status matching the status specified.

            * `ACTIVE` - Active
            * `ARCHIVED` - Archived

        limit : typing.Optional[int]
            Number of results to return per page.

        offset : typing.Optional[int]
            The initial index from which to return the results.

        ordering : typing.Optional[str]
            Which field to use when ordering the results.

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

        Returns
        -------
        HttpResponse[PaginatedFolderEntityList]

        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/folder-entities",
            base_url=self._client_wrapper.get_environment().default,
            method="GET",
            params={
                "entity_status": entity_status,
                "limit": limit,
                "offset": offset,
                "ordering": ordering,
                "parent_folder_id": parent_folder_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PaginatedFolderEntityList,
                    parse_obj_as(
                        type_=PaginatedFolderEntityList,  # 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 add_entity_to_folder(
        self, folder_id: str, *, entity_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[None]:
        """
        Add an entity to a specific folder or root directory.

        Adding an entity to a folder will remove it from any other folders it might have been a member of.

        Parameters
        ----------
        folder_id : str
            The ID of the folder to which the entity should be added. This can be a UUID of a folder, or the name of a root
            directory. Supported root directories include:
            - PROMPT_SANDBOX
            - WORKFLOW_SANDBOX
            - DOCUMENT_INDEX
            - TEST_SUITE

        entity_id : str
            The ID of the entity you would like to move.

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/folders/{jsonable_encoder(folder_id)}/add-entity",
            base_url=self._client_wrapper.get_environment().default,
            method="POST",
            json={
                "entity_id": entity_id,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            _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 AsyncRawFolderEntitiesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        *,
        parent_folder_id: str,
        entity_status: typing.Optional[FolderEntitiesListRequestEntityStatus] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        ordering: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[PaginatedFolderEntityList]:
        """
        List all folder entities within a specified folder.

        Parameters
        ----------
        parent_folder_id : str
            Filter down to only those entities whose parent folder has the specified ID.

            To filter by an entity's parent folder, provide the ID of the parent folder. To filter by the root directory, provide
            a string representing the entity type of the root directory. Supported root directories include:
            - PROMPT_SANDBOX
            - WORKFLOW_SANDBOX
            - DOCUMENT_INDEX
            - TEST_SUITE

        entity_status : typing.Optional[FolderEntitiesListRequestEntityStatus]
            Filter down to only those objects whose entities have a status matching the status specified.

            * `ACTIVE` - Active
            * `ARCHIVED` - Archived

        limit : typing.Optional[int]
            Number of results to return per page.

        offset : typing.Optional[int]
            The initial index from which to return the results.

        ordering : typing.Optional[str]
            Which field to use when ordering the results.

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

        Returns
        -------
        AsyncHttpResponse[PaginatedFolderEntityList]

        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/folder-entities",
            base_url=self._client_wrapper.get_environment().default,
            method="GET",
            params={
                "entity_status": entity_status,
                "limit": limit,
                "offset": offset,
                "ordering": ordering,
                "parent_folder_id": parent_folder_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PaginatedFolderEntityList,
                    parse_obj_as(
                        type_=PaginatedFolderEntityList,  # 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 add_entity_to_folder(
        self, folder_id: str, *, entity_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[None]:
        """
        Add an entity to a specific folder or root directory.

        Adding an entity to a folder will remove it from any other folders it might have been a member of.

        Parameters
        ----------
        folder_id : str
            The ID of the folder to which the entity should be added. This can be a UUID of a folder, or the name of a root
            directory. Supported root directories include:
            - PROMPT_SANDBOX
            - WORKFLOW_SANDBOX
            - DOCUMENT_INDEX
            - TEST_SUITE

        entity_id : str
            The ID of the entity you would like to move.

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/folders/{jsonable_encoder(folder_id)}/add-entity",
            base_url=self._client_wrapper.get_environment().default,
            method="POST",
            json={
                "entity_id": entity_id,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            _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)
