# 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.retrieval_strategy import RetrievalStrategy
from .types.search_response import SearchResponse

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


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

    def search(
        self,
        project_id: str,
        *,
        query: str,
        retrieval_strategy: typing.Optional[RetrievalStrategy] = OMIT,
        expand_query: typing.Optional[bool] = OMIT,
        rerank: typing.Optional[bool] = OMIT,
        generate_answer: typing.Optional[bool] = OMIT,
        include_sources: typing.Optional[bool] = OMIT,
        limit: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[SearchResponse]:
        """
        Perform a semantic search across project documents

        Parameters
        ----------
        project_id : str
            Project ID to search within

        query : str
            Search query text (max 2048 tokens)

        retrieval_strategy : typing.Optional[RetrievalStrategy]

        expand_query : typing.Optional[bool]
            Generate query variations to improve recall

        rerank : typing.Optional[bool]
            Rerank results using Cohere for improved relevance

        generate_answer : typing.Optional[bool]
            Generate a natural-language answer

        include_sources : typing.Optional[bool]
            Include source documents in response. Set false to reduce payload when only the answer is needed.

        limit : typing.Optional[int]
            Maximum results to return

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

        Returns
        -------
        HttpResponse[SearchResponse]
            Search results
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/projects/{jsonable_encoder(project_id)}/search",
            method="POST",
            json={
                "query": query,
                "retrieval_strategy": retrieval_strategy,
                "expand_query": expand_query,
                "rerank": rerank,
                "generate_answer": generate_answer,
                "include_sources": include_sources,
                "limit": limit,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SearchResponse,
                    parse_obj_as(
                        type_=SearchResponse,  # 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 AsyncRawtropicalia:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def search(
        self,
        project_id: str,
        *,
        query: str,
        retrieval_strategy: typing.Optional[RetrievalStrategy] = OMIT,
        expand_query: typing.Optional[bool] = OMIT,
        rerank: typing.Optional[bool] = OMIT,
        generate_answer: typing.Optional[bool] = OMIT,
        include_sources: typing.Optional[bool] = OMIT,
        limit: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[SearchResponse]:
        """
        Perform a semantic search across project documents

        Parameters
        ----------
        project_id : str
            Project ID to search within

        query : str
            Search query text (max 2048 tokens)

        retrieval_strategy : typing.Optional[RetrievalStrategy]

        expand_query : typing.Optional[bool]
            Generate query variations to improve recall

        rerank : typing.Optional[bool]
            Rerank results using Cohere for improved relevance

        generate_answer : typing.Optional[bool]
            Generate a natural-language answer

        include_sources : typing.Optional[bool]
            Include source documents in response. Set false to reduce payload when only the answer is needed.

        limit : typing.Optional[int]
            Maximum results to return

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

        Returns
        -------
        AsyncHttpResponse[SearchResponse]
            Search results
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/projects/{jsonable_encoder(project_id)}/search",
            method="POST",
            json={
                "query": query,
                "retrieval_strategy": retrieval_strategy,
                "expand_query": expand_query,
                "rerank": rerank,
                "generate_answer": generate_answer,
                "include_sources": include_sources,
                "limit": limit,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SearchResponse,
                    parse_obj_as(
                        type_=SearchResponse,  # 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)
