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

import typing
import uuid

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from .raw_client import AsyncRawSearchClient, RawSearchClient
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 SearchClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawSearchClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawSearchClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawSearchClient
        """
        return self._raw_client

    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,
    ) -> 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
        -------
        SearchDocumentCollectionResponse

        Examples
        --------
        import uuid

        from credal import CredalApi
        from credal.search import DocumentCollectionSearchOptions, SingleFieldFilter

        client = CredalApi(
            api_key="YOUR_API_KEY",
        )
        client.search.search_document_collection(
            collection_id=uuid.UUID(
                "82e4b12a-6990-45d4-8ebd-85c00e030c24",
            ),
            search_query="ABC Corp",
            structured_query_filters=[
                SingleFieldFilter(
                    field="status",
                    operator="==",
                    value="Open",
                )
            ],
            user_email="jack@credal.ai",
            search_options=DocumentCollectionSearchOptions(
                max_chunks=10,
                merge_contents=True,
                threshold=0.8,
                enable_smart_filtering=True,
                enable_query_extraction=True,
                enable_reranking=True,
            ),
        )
        """
        _response = self._raw_client.search_document_collection(
            collection_id=collection_id,
            search_query=search_query,
            user_email=user_email,
            structured_query_filters=structured_query_filters,
            search_options=search_options,
            metadata_filter_expression=metadata_filter_expression,
            request_options=request_options,
        )
        return _response.data


class AsyncSearchClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawSearchClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawSearchClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawSearchClient
        """
        return self._raw_client

    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,
    ) -> 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
        -------
        SearchDocumentCollectionResponse

        Examples
        --------
        import asyncio
        import uuid

        from credal import AsyncCredalApi
        from credal.search import DocumentCollectionSearchOptions, SingleFieldFilter

        client = AsyncCredalApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.search.search_document_collection(
                collection_id=uuid.UUID(
                    "82e4b12a-6990-45d4-8ebd-85c00e030c24",
                ),
                search_query="ABC Corp",
                structured_query_filters=[
                    SingleFieldFilter(
                        field="status",
                        operator="==",
                        value="Open",
                    )
                ],
                user_email="jack@credal.ai",
                search_options=DocumentCollectionSearchOptions(
                    max_chunks=10,
                    merge_contents=True,
                    threshold=0.8,
                    enable_smart_filtering=True,
                    enable_query_extraction=True,
                    enable_reranking=True,
                ),
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.search_document_collection(
            collection_id=collection_id,
            search_query=search_query,
            user_email=user_email,
            structured_query_filters=structured_query_filters,
            search_options=search_options,
            metadata_filter_expression=metadata_filter_expression,
            request_options=request_options,
        )
        return _response.data
