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

import datetime as dt
import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.pagination import AsyncPager, SyncPager
from ..core.request_options import RequestOptions
from ..types.query_history import QueryHistory
from ..types.query_history_summary import QueryHistorySummary
from .raw_client import AsyncRawQueryHistoryClient, RawQueryHistoryClient


class QueryHistoryClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawQueryHistoryClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawQueryHistoryClient
        """
        return self._raw_client

    def get(
        self,
        query_id: str,
        *,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> QueryHistory:
        """
        Retrieve a detailed history of previously executed query.

        Parameters
        ----------
        query_id : str
            The ID of the query history

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        QueryHistory
            The query history.

        Examples
        --------
        from vectara import Vectara
        client = Vectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        client.query_history.get(query_id='query_id', )
        """
        response = self._raw_client.get(
            query_id,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data

    def list(
        self,
        *,
        corpus_key: typing.Optional[str] = None,
        started_after: typing.Optional[dt.datetime] = None,
        started_before: typing.Optional[dt.datetime] = None,
        chat_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page_key: typing.Optional[str] = None,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[QueryHistorySummary]:
        """
        Retrieve query histories.

        Parameters
        ----------
        corpus_key : typing.Optional[str]
            Specifies the `corpus_key` used in the query.

        started_after : typing.Optional[dt.datetime]
            Queries that started after a particular date-time.

        started_before : typing.Optional[dt.datetime]
            Queries that started before a particular date-time.

        chat_id : typing.Optional[str]
            Specifies the chat_id of the query, this will return all queries in the specified chat.

        limit : typing.Optional[int]
            Specifies the maximum number of query history listed.

        page_key : typing.Optional[str]
            Used to retrieve the next page of query histories after the limit has been reached.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        SyncPager[QueryHistorySummary]
            An array of Query Histories.

        Examples
        --------
        from vectara import Vectara
        client = Vectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        response = client.query_history.list()
        for item in response:
            yield item
        # alternatively, you can paginate page-by-page
        for page in response.iter_pages():
            yield page
        """
        response = self._raw_client.list(
            corpus_key=corpus_key,
            started_after=started_after,
            started_before=started_before,
            chat_id=chat_id,
            limit=limit,
            page_key=page_key,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data


class AsyncQueryHistoryClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawQueryHistoryClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawQueryHistoryClient
        """
        return self._raw_client

    async def get(
        self,
        query_id: str,
        *,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> QueryHistory:
        """
        Retrieve a detailed history of previously executed query.

        Parameters
        ----------
        query_id : str
            The ID of the query history

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        QueryHistory
            The query history.

        Examples
        --------
        from vectara import AsyncVectara
        import asyncio
        client = AsyncVectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        async def main() -> None:
            await client.query_history.get(query_id='query_id', )
        asyncio.run(main())
        """
        response = await self._raw_client.get(
            query_id,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data

    async def list(
        self,
        *,
        corpus_key: typing.Optional[str] = None,
        started_after: typing.Optional[dt.datetime] = None,
        started_before: typing.Optional[dt.datetime] = None,
        chat_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page_key: typing.Optional[str] = None,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[QueryHistorySummary]:
        """
        Retrieve query histories.

        Parameters
        ----------
        corpus_key : typing.Optional[str]
            Specifies the `corpus_key` used in the query.

        started_after : typing.Optional[dt.datetime]
            Queries that started after a particular date-time.

        started_before : typing.Optional[dt.datetime]
            Queries that started before a particular date-time.

        chat_id : typing.Optional[str]
            Specifies the chat_id of the query, this will return all queries in the specified chat.

        limit : typing.Optional[int]
            Specifies the maximum number of query history listed.

        page_key : typing.Optional[str]
            Used to retrieve the next page of query histories after the limit has been reached.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        AsyncPager[QueryHistorySummary]
            An array of Query Histories.

        Examples
        --------
        from vectara import AsyncVectara
        import asyncio
        client = AsyncVectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        async def main() -> None:
            response = await client.query_history.list()
            async for item in response:
                yield item

            # alternatively, you can paginate page-by-page
            async for page in response.iter_pages():
                yield page
        asyncio.run(main())
        """
        response = await self._raw_client.list(
            corpus_key=corpus_key,
            started_after=started_after,
            started_before=started_before,
            chat_id=chat_id,
            limit=limit,
            page_key=page_key,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data
