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

import typing
import uuid
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 ..core.serialization import convert_and_respect_annotation_metadata
from .types.document_collection_search_options import DocumentCollectionSearchOptions
from .types.search_document_collection_response import SearchDocumentCollectionResponse
from .types.single_field_filter import SingleFieldFilter

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


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

    def search_document_collection(
        self,
        *,
        collection_id: uuid.UUID,
        search_query: str,
        user_email: str,
        structured_query_filters: typing.Optional[typing.Sequence[SingleFieldFilter]] = OMIT,
        search_options: typing.Optional[DocumentCollectionSearchOptions] = OMIT,
        metadata_filter_expression: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[SearchDocumentCollectionResponse]:
        """
        Search across all documents in a document collection using the document metadata and contents.

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

        search_query : str

        user_email : str
            The email of the user making the search request for permissions reduction.

        structured_query_filters : typing.Optional[typing.Sequence[SingleFieldFilter]]
            The structured query filters to apply to the search query.

        search_options : typing.Optional[DocumentCollectionSearchOptions]

        metadata_filter_expression : typing.Optional[str]
            Legacy metadata filter expression to apply to the search query. Use structuredQueryFilters instead.

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

        Returns
        -------
        HttpResponse[SearchDocumentCollectionResponse]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v0/search/searchDocumentCollection",
            method="POST",
            json={
                "collectionId": collection_id,
                "searchQuery": search_query,
                "userEmail": user_email,
                "structuredQueryFilters": convert_and_respect_annotation_metadata(
                    object_=structured_query_filters, annotation=typing.Sequence[SingleFieldFilter], direction="write"
                ),
                "searchOptions": convert_and_respect_annotation_metadata(
                    object_=search_options, annotation=DocumentCollectionSearchOptions, direction="write"
                ),
                "metadataFilterExpression": metadata_filter_expression,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SearchDocumentCollectionResponse,
                    parse_obj_as(
                        type_=SearchDocumentCollectionResponse,  # 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 AsyncRawSearchClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def search_document_collection(
        self,
        *,
        collection_id: uuid.UUID,
        search_query: str,
        user_email: str,
        structured_query_filters: typing.Optional[typing.Sequence[SingleFieldFilter]] = OMIT,
        search_options: typing.Optional[DocumentCollectionSearchOptions] = OMIT,
        metadata_filter_expression: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[SearchDocumentCollectionResponse]:
        """
        Search across all documents in a document collection using the document metadata and contents.

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

        search_query : str

        user_email : str
            The email of the user making the search request for permissions reduction.

        structured_query_filters : typing.Optional[typing.Sequence[SingleFieldFilter]]
            The structured query filters to apply to the search query.

        search_options : typing.Optional[DocumentCollectionSearchOptions]

        metadata_filter_expression : typing.Optional[str]
            Legacy metadata filter expression to apply to the search query. Use structuredQueryFilters instead.

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

        Returns
        -------
        AsyncHttpResponse[SearchDocumentCollectionResponse]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v0/search/searchDocumentCollection",
            method="POST",
            json={
                "collectionId": collection_id,
                "searchQuery": search_query,
                "userEmail": user_email,
                "structuredQueryFilters": convert_and_respect_annotation_metadata(
                    object_=structured_query_filters, annotation=typing.Sequence[SingleFieldFilter], direction="write"
                ),
                "searchOptions": convert_and_respect_annotation_metadata(
                    object_=search_options, annotation=DocumentCollectionSearchOptions, direction="write"
                ),
                "metadataFilterExpression": metadata_filter_expression,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SearchDocumentCollectionResponse,
                    parse_obj_as(
                        type_=SearchDocumentCollectionResponse,  # 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)
