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

import typing
import uuid
from json.decoder import JSONDecodeError

from ..common.types.collaborator import Collaborator
from ..common.types.resource_identifier import ResourceIdentifier
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 ..core.serialization import convert_and_respect_annotation_metadata
from .types.create_collection_response import CreateCollectionResponse
from .types.delete_collection_response import DeleteCollectionResponse
from .types.list_documents_in_collection_response import ListDocumentsInCollectionResponse
from .types.mongo_collection_sync_config import MongoCollectionSyncConfig
from .types.mongo_collection_sync_response import MongoCollectionSyncResponse

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


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

    def add_documents_to_collection(
        self,
        *,
        collection_id: uuid.UUID,
        resource_identifiers: typing.Sequence[ResourceIdentifier],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[None]:
        """
        Add documents to a document collection. Note that the documents must already exist in the document catalog to use this endpoint. If you want to upload a new document to a collection, use the `uploadDocumentContents` endpoint.

        Parameters
        ----------
        collection_id : uuid.UUID
            The ID of the document collection you want to add to.

        resource_identifiers : typing.Sequence[ResourceIdentifier]
            The set of resource identifier for which you want to add to the collection.

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v0/documentCollections/addDocumentsToCollection",
            method="POST",
            json={
                "collectionId": collection_id,
                "resourceIdentifiers": convert_and_respect_annotation_metadata(
                    object_=resource_identifiers, annotation=typing.Sequence[ResourceIdentifier], direction="write"
                ),
            },
            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)

    def remove_documents_from_collection(
        self,
        *,
        collection_id: uuid.UUID,
        resource_identifiers: typing.Sequence[ResourceIdentifier],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[None]:
        """
        Remove documents from a collection

        Parameters
        ----------
        collection_id : uuid.UUID
            The ID of the document collection you want to add to.

        resource_identifiers : typing.Sequence[ResourceIdentifier]
            The set of resource identifier for which you want to remove from the collection

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v0/documentCollections/removeDocumentsFromCollection",
            method="DELETE",
            json={
                "collectionId": collection_id,
                "resourceIdentifiers": convert_and_respect_annotation_metadata(
                    object_=resource_identifiers, annotation=typing.Sequence[ResourceIdentifier], direction="write"
                ),
            },
            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)

    def list_documents_in_collection(
        self, *, collection_id: uuid.UUID, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ListDocumentsInCollectionResponse]:
        """
        List documents in a collection

        Parameters
        ----------
        collection_id : uuid.UUID
            The ID of the document collection to list documents from.

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

        Returns
        -------
        HttpResponse[ListDocumentsInCollectionResponse]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v0/documentCollections/listDocumentsInCollection",
            method="GET",
            params={
                "collectionId": collection_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListDocumentsInCollectionResponse,
                    parse_obj_as(
                        type_=ListDocumentsInCollectionResponse,  # 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_collection(
        self,
        *,
        name: str,
        description: str,
        collaborators: typing.Sequence[Collaborator],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateCollectionResponse]:
        """
        Create a new collection. The API key used will be added to the collection for future Requests

        Parameters
        ----------
        name : str
            A descriptive name for the collection.

        description : str
            An in depth name for the agent's function. Useful for routing requests to the right agent.

        collaborators : typing.Sequence[Collaborator]
            A list of collaborator emails and roles that will have access to the agent.

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

        Returns
        -------
        HttpResponse[CreateCollectionResponse]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v0/documentCollections/createCollection",
            method="POST",
            json={
                "name": name,
                "description": description,
                "collaborators": convert_and_respect_annotation_metadata(
                    object_=collaborators, annotation=typing.Sequence[Collaborator], direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateCollectionResponse,
                    parse_obj_as(
                        type_=CreateCollectionResponse,  # 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 delete_collection(
        self, *, collection_id: uuid.UUID, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DeleteCollectionResponse]:
        """
        Delete the collection.

        Parameters
        ----------
        collection_id : uuid.UUID

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

        Returns
        -------
        HttpResponse[DeleteCollectionResponse]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v0/documentCollections/deleteCollection",
            method="DELETE",
            json={
                "collectionId": collection_id,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteCollectionResponse,
                    parse_obj_as(
                        type_=DeleteCollectionResponse,  # 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_mongo_collection_sync(
        self,
        *,
        collection_id: uuid.UUID,
        mongo_uri: str,
        config: MongoCollectionSyncConfig,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[MongoCollectionSyncResponse]:
        """
        Credal lets you easily sync your MongoDB data for use in Collections and Agents. Create a new sync from a MongoDB collection to a Credal collection.

        Parameters
        ----------
        collection_id : uuid.UUID

        mongo_uri : str

        config : MongoCollectionSyncConfig

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

        Returns
        -------
        HttpResponse[MongoCollectionSyncResponse]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v0/documentCollections/mongodb/createMongoSync",
            method="POST",
            json={
                "collectionId": collection_id,
                "mongoURI": mongo_uri,
                "config": convert_and_respect_annotation_metadata(
                    object_=config, annotation=MongoCollectionSyncConfig, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MongoCollectionSyncResponse,
                    parse_obj_as(
                        type_=MongoCollectionSyncResponse,  # 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 update_mongo_collection_sync(
        self,
        *,
        mongo_credential_id: uuid.UUID,
        mongo_uri: str,
        config: MongoCollectionSyncConfig,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[MongoCollectionSyncResponse]:
        """
        Credal lets you easily sync your MongoDB data for use in Collections and Agents. Update an existing sync from a MongoDB collection to a Credal collection via the `mongoCredentialId`, to disambiguate between multiple potential syncs to a given collection.

        Parameters
        ----------
        mongo_credential_id : uuid.UUID

        mongo_uri : str

        config : MongoCollectionSyncConfig

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

        Returns
        -------
        HttpResponse[MongoCollectionSyncResponse]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v0/documentCollections/mongodb/updateMongoSync",
            method="POST",
            json={
                "mongoCredentialId": mongo_credential_id,
                "mongoURI": mongo_uri,
                "config": convert_and_respect_annotation_metadata(
                    object_=config, annotation=MongoCollectionSyncConfig, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MongoCollectionSyncResponse,
                    parse_obj_as(
                        type_=MongoCollectionSyncResponse,  # 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)


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

    async def add_documents_to_collection(
        self,
        *,
        collection_id: uuid.UUID,
        resource_identifiers: typing.Sequence[ResourceIdentifier],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[None]:
        """
        Add documents to a document collection. Note that the documents must already exist in the document catalog to use this endpoint. If you want to upload a new document to a collection, use the `uploadDocumentContents` endpoint.

        Parameters
        ----------
        collection_id : uuid.UUID
            The ID of the document collection you want to add to.

        resource_identifiers : typing.Sequence[ResourceIdentifier]
            The set of resource identifier for which you want to add to the collection.

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v0/documentCollections/addDocumentsToCollection",
            method="POST",
            json={
                "collectionId": collection_id,
                "resourceIdentifiers": convert_and_respect_annotation_metadata(
                    object_=resource_identifiers, annotation=typing.Sequence[ResourceIdentifier], direction="write"
                ),
            },
            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)

    async def remove_documents_from_collection(
        self,
        *,
        collection_id: uuid.UUID,
        resource_identifiers: typing.Sequence[ResourceIdentifier],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[None]:
        """
        Remove documents from a collection

        Parameters
        ----------
        collection_id : uuid.UUID
            The ID of the document collection you want to add to.

        resource_identifiers : typing.Sequence[ResourceIdentifier]
            The set of resource identifier for which you want to remove from the collection

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v0/documentCollections/removeDocumentsFromCollection",
            method="DELETE",
            json={
                "collectionId": collection_id,
                "resourceIdentifiers": convert_and_respect_annotation_metadata(
                    object_=resource_identifiers, annotation=typing.Sequence[ResourceIdentifier], direction="write"
                ),
            },
            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)

    async def list_documents_in_collection(
        self, *, collection_id: uuid.UUID, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ListDocumentsInCollectionResponse]:
        """
        List documents in a collection

        Parameters
        ----------
        collection_id : uuid.UUID
            The ID of the document collection to list documents from.

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

        Returns
        -------
        AsyncHttpResponse[ListDocumentsInCollectionResponse]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v0/documentCollections/listDocumentsInCollection",
            method="GET",
            params={
                "collectionId": collection_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListDocumentsInCollectionResponse,
                    parse_obj_as(
                        type_=ListDocumentsInCollectionResponse,  # 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_collection(
        self,
        *,
        name: str,
        description: str,
        collaborators: typing.Sequence[Collaborator],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateCollectionResponse]:
        """
        Create a new collection. The API key used will be added to the collection for future Requests

        Parameters
        ----------
        name : str
            A descriptive name for the collection.

        description : str
            An in depth name for the agent's function. Useful for routing requests to the right agent.

        collaborators : typing.Sequence[Collaborator]
            A list of collaborator emails and roles that will have access to the agent.

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

        Returns
        -------
        AsyncHttpResponse[CreateCollectionResponse]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v0/documentCollections/createCollection",
            method="POST",
            json={
                "name": name,
                "description": description,
                "collaborators": convert_and_respect_annotation_metadata(
                    object_=collaborators, annotation=typing.Sequence[Collaborator], direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateCollectionResponse,
                    parse_obj_as(
                        type_=CreateCollectionResponse,  # 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 delete_collection(
        self, *, collection_id: uuid.UUID, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DeleteCollectionResponse]:
        """
        Delete the collection.

        Parameters
        ----------
        collection_id : uuid.UUID

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

        Returns
        -------
        AsyncHttpResponse[DeleteCollectionResponse]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v0/documentCollections/deleteCollection",
            method="DELETE",
            json={
                "collectionId": collection_id,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteCollectionResponse,
                    parse_obj_as(
                        type_=DeleteCollectionResponse,  # 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_mongo_collection_sync(
        self,
        *,
        collection_id: uuid.UUID,
        mongo_uri: str,
        config: MongoCollectionSyncConfig,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[MongoCollectionSyncResponse]:
        """
        Credal lets you easily sync your MongoDB data for use in Collections and Agents. Create a new sync from a MongoDB collection to a Credal collection.

        Parameters
        ----------
        collection_id : uuid.UUID

        mongo_uri : str

        config : MongoCollectionSyncConfig

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

        Returns
        -------
        AsyncHttpResponse[MongoCollectionSyncResponse]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v0/documentCollections/mongodb/createMongoSync",
            method="POST",
            json={
                "collectionId": collection_id,
                "mongoURI": mongo_uri,
                "config": convert_and_respect_annotation_metadata(
                    object_=config, annotation=MongoCollectionSyncConfig, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MongoCollectionSyncResponse,
                    parse_obj_as(
                        type_=MongoCollectionSyncResponse,  # 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 update_mongo_collection_sync(
        self,
        *,
        mongo_credential_id: uuid.UUID,
        mongo_uri: str,
        config: MongoCollectionSyncConfig,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[MongoCollectionSyncResponse]:
        """
        Credal lets you easily sync your MongoDB data for use in Collections and Agents. Update an existing sync from a MongoDB collection to a Credal collection via the `mongoCredentialId`, to disambiguate between multiple potential syncs to a given collection.

        Parameters
        ----------
        mongo_credential_id : uuid.UUID

        mongo_uri : str

        config : MongoCollectionSyncConfig

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

        Returns
        -------
        AsyncHttpResponse[MongoCollectionSyncResponse]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v0/documentCollections/mongodb/updateMongoSync",
            method="POST",
            json={
                "mongoCredentialId": mongo_credential_id,
                "mongoURI": mongo_uri,
                "config": convert_and_respect_annotation_metadata(
                    object_=config, annotation=MongoCollectionSyncConfig, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    MongoCollectionSyncResponse,
                    parse_obj_as(
                        type_=MongoCollectionSyncResponse,  # 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)
