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

from __future__ import annotations

import typing

import httpx
from .core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from .core.request_options import RequestOptions
from .environment import tropicaliaEnvironment
from .raw_client import AsyncRawtropicalia, Rawtropicalia
from .types.retrieval_strategy import RetrievalStrategy
from .types.search_response import SearchResponse

if typing.TYPE_CHECKING:
    from .projects.client import AsyncProjectsClient, ProjectsClient
    from .upload.client import AsyncUploadClient, UploadClient
# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class tropicalia:
    """
    Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.

    Parameters
    ----------
    base_url : typing.Optional[str]
        The base url to use for requests from the client.

    environment : tropicaliaEnvironment
        The environment to use for requests from the client. from .environment import tropicaliaEnvironment



        Defaults to tropicaliaEnvironment.PRODUCTION



    token : typing.Union[str, typing.Callable[[], str]]
    headers : typing.Optional[typing.Dict[str, str]]
        Additional headers to send with every request.

    timeout : typing.Optional[float]
        The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

    follow_redirects : typing.Optional[bool]
        Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.

    httpx_client : typing.Optional[httpx.Client]
        The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.

    Examples
    --------
    from tropicalia import tropicalia

    client = tropicalia(
        token="YOUR_TOKEN",
    )
    """

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: tropicaliaEnvironment = tropicaliaEnvironment.PRODUCTION,
        token: typing.Union[str, typing.Callable[[], str]],
        headers: typing.Optional[typing.Dict[str, str]] = None,
        timeout: typing.Optional[float] = None,
        follow_redirects: typing.Optional[bool] = True,
        httpx_client: typing.Optional[httpx.Client] = None,
    ):
        _defaulted_timeout = (
            timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
        )
        self._client_wrapper = SyncClientWrapper(
            base_url=_get_base_url(base_url=base_url, environment=environment),
            token=token,
            headers=headers,
            httpx_client=httpx_client
            if httpx_client is not None
            else httpx.Client(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
            if follow_redirects is not None
            else httpx.Client(timeout=_defaulted_timeout),
            timeout=_defaulted_timeout,
        )
        self._raw_client = Rawtropicalia(client_wrapper=self._client_wrapper)
        self._projects: typing.Optional[ProjectsClient] = None
        self._upload: typing.Optional[UploadClient] = None

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

        Returns
        -------
        Rawtropicalia
        """
        return self._raw_client

    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,
    ) -> 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
        -------
        SearchResponse
            Search results

        Examples
        --------
        from tropicalia import tropicalia

        client = tropicalia(
            token="YOUR_TOKEN",
        )
        client.search(
            project_id="proj_9f8b7c6d",
            query="Best practices for sustainable tropical agriculture",
            retrieval_strategy="hybrid",
            expand_query=False,
            rerank=True,
            generate_answer=True,
            include_sources=True,
            limit=50,
        )
        """
        _response = self._raw_client.search(
            project_id,
            query=query,
            retrieval_strategy=retrieval_strategy,
            expand_query=expand_query,
            rerank=rerank,
            generate_answer=generate_answer,
            include_sources=include_sources,
            limit=limit,
            request_options=request_options,
        )
        return _response.data

    @property
    def projects(self):
        if self._projects is None:
            from .projects.client import ProjectsClient  # noqa: E402

            self._projects = ProjectsClient(client_wrapper=self._client_wrapper)
        return self._projects

    @property
    def upload(self):
        if self._upload is None:
            from .upload.client import UploadClient  # noqa: E402

            self._upload = UploadClient(client_wrapper=self._client_wrapper)
        return self._upload


class Asynctropicalia:
    """
    Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.

    Parameters
    ----------
    base_url : typing.Optional[str]
        The base url to use for requests from the client.

    environment : tropicaliaEnvironment
        The environment to use for requests from the client. from .environment import tropicaliaEnvironment



        Defaults to tropicaliaEnvironment.PRODUCTION



    token : typing.Union[str, typing.Callable[[], str]]
    headers : typing.Optional[typing.Dict[str, str]]
        Additional headers to send with every request.

    timeout : typing.Optional[float]
        The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

    follow_redirects : typing.Optional[bool]
        Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.

    httpx_client : typing.Optional[httpx.AsyncClient]
        The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.

    Examples
    --------
    from tropicalia import Asynctropicalia

    client = Asynctropicalia(
        token="YOUR_TOKEN",
    )
    """

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: tropicaliaEnvironment = tropicaliaEnvironment.PRODUCTION,
        token: typing.Union[str, typing.Callable[[], str]],
        headers: typing.Optional[typing.Dict[str, str]] = None,
        timeout: typing.Optional[float] = None,
        follow_redirects: typing.Optional[bool] = True,
        httpx_client: typing.Optional[httpx.AsyncClient] = None,
    ):
        _defaulted_timeout = (
            timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
        )
        self._client_wrapper = AsyncClientWrapper(
            base_url=_get_base_url(base_url=base_url, environment=environment),
            token=token,
            headers=headers,
            httpx_client=httpx_client
            if httpx_client is not None
            else httpx.AsyncClient(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
            if follow_redirects is not None
            else httpx.AsyncClient(timeout=_defaulted_timeout),
            timeout=_defaulted_timeout,
        )
        self._raw_client = AsyncRawtropicalia(client_wrapper=self._client_wrapper)
        self._projects: typing.Optional[AsyncProjectsClient] = None
        self._upload: typing.Optional[AsyncUploadClient] = None

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

        Returns
        -------
        AsyncRawtropicalia
        """
        return self._raw_client

    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,
    ) -> 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
        -------
        SearchResponse
            Search results

        Examples
        --------
        import asyncio

        from tropicalia import Asynctropicalia

        client = Asynctropicalia(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.search(
                project_id="proj_9f8b7c6d",
                query="Best practices for sustainable tropical agriculture",
                retrieval_strategy="hybrid",
                expand_query=False,
                rerank=True,
                generate_answer=True,
                include_sources=True,
                limit=50,
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.search(
            project_id,
            query=query,
            retrieval_strategy=retrieval_strategy,
            expand_query=expand_query,
            rerank=rerank,
            generate_answer=generate_answer,
            include_sources=include_sources,
            limit=limit,
            request_options=request_options,
        )
        return _response.data

    @property
    def projects(self):
        if self._projects is None:
            from .projects.client import AsyncProjectsClient  # noqa: E402

            self._projects = AsyncProjectsClient(client_wrapper=self._client_wrapper)
        return self._projects

    @property
    def upload(self):
        if self._upload is None:
            from .upload.client import AsyncUploadClient  # noqa: E402

            self._upload = AsyncUploadClient(client_wrapper=self._client_wrapper)
        return self._upload


def _get_base_url(*, base_url: typing.Optional[str] = None, environment: tropicaliaEnvironment) -> str:
    if base_url is not None:
        return base_url
    elif environment is not None:
        return environment.value
    else:
        raise Exception("Please pass in either base_url or environment to construct the client")
