# 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.request_options import RequestOptions
from ..types.batch_progress_response import BatchProgressResponse
from ..types.batch_results_response import BatchResultsResponse
from .raw_client import AsyncRawBatchClient, RawBatchClient
from .types.get_batch_root_request_status import GetBatchRootRequestStatus
from .types.get_batch_root_response import GetBatchRootResponse


class BatchClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawBatchClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawBatchClient
        """
        return self._raw_client

    def get_batch_root(
        self,
        *,
        status: typing.Optional[GetBatchRootRequestStatus] = None,
        date_from: typing.Optional[dt.datetime] = None,
        date_to: typing.Optional[dt.datetime] = None,
        limit: typing.Optional[int] = None,
        page: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetBatchRootResponse:
        """
        Returns a paginated list of batch jobs belonging to the authenticated user, sorted by last modified time descending. Use the `status` filter to narrow results to in-progress or completed batches. Use `date_from` / `date_to` to filter by last modified time range.

        Parameters
        ----------
        status : typing.Optional[GetBatchRootRequestStatus]
            Filter by batch status. `processing` returns in-progress batches; `completed` returns finished batches.

        date_from : typing.Optional[dt.datetime]
            Return only batches last modified at or after this ISO 8601 datetime (e.g. `2024-01-01T00:00:00Z`).

        date_to : typing.Optional[dt.datetime]
            Return only batches last modified at or before this ISO 8601 datetime (e.g. `2024-12-31T23:59:59Z`).

        limit : typing.Optional[int]
            Number of results per page (default 20).

        page : typing.Optional[int]
            Page number.

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

        Returns
        -------
        GetBatchRootResponse
            Paginated list of batch jobs

        Examples
        --------
        from ottimate import Ottimate

        client = Ottimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )
        client.batch.get_batch_root(
            limit=20,
            page=1,
        )
        """
        _response = self._raw_client.get_batch_root(
            status=status, date_from=date_from, date_to=date_to, limit=limit, page=page, request_options=request_options
        )
        return _response.data

    def get_batch_id_progress(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> BatchProgressResponse:
        """
        Returns the current progress of a batch operation. Poll this endpoint until `status` is `completed`. When completed, a `results_url` is included. If all tasks failed, an `error` field is included with the failure reason.

        Parameters
        ----------
        id : str
            The batch ID returned from the initiating bulk operation

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

        Returns
        -------
        BatchProgressResponse
            Successful response with current batch progress

        Examples
        --------
        from ottimate import Ottimate

        client = Ottimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )
        client.batch.get_batch_id_progress(
            id="racc_90832490382904802384",
        )
        """
        _response = self._raw_client.get_batch_id_progress(id, request_options=request_options)
        return _response.data

    def get_batch_id_results(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> BatchResultsResponse:
        """
        Returns the per-task results for a batch operation. Call this endpoint after polling `/progress` until `status` is `completed`. Each entry in `results` corresponds to one task, with the task outcome in `result` and any error message in `message`.

        Parameters
        ----------
        id : str
            The batch ID returned from the initiating bulk operation

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

        Returns
        -------
        BatchResultsResponse
            Successful response with batch results

        Examples
        --------
        from ottimate import Ottimate

        client = Ottimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )
        client.batch.get_batch_id_results(
            id="racc_90832490382904802384",
        )
        """
        _response = self._raw_client.get_batch_id_results(id, request_options=request_options)
        return _response.data


class AsyncBatchClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawBatchClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawBatchClient
        """
        return self._raw_client

    async def get_batch_root(
        self,
        *,
        status: typing.Optional[GetBatchRootRequestStatus] = None,
        date_from: typing.Optional[dt.datetime] = None,
        date_to: typing.Optional[dt.datetime] = None,
        limit: typing.Optional[int] = None,
        page: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetBatchRootResponse:
        """
        Returns a paginated list of batch jobs belonging to the authenticated user, sorted by last modified time descending. Use the `status` filter to narrow results to in-progress or completed batches. Use `date_from` / `date_to` to filter by last modified time range.

        Parameters
        ----------
        status : typing.Optional[GetBatchRootRequestStatus]
            Filter by batch status. `processing` returns in-progress batches; `completed` returns finished batches.

        date_from : typing.Optional[dt.datetime]
            Return only batches last modified at or after this ISO 8601 datetime (e.g. `2024-01-01T00:00:00Z`).

        date_to : typing.Optional[dt.datetime]
            Return only batches last modified at or before this ISO 8601 datetime (e.g. `2024-12-31T23:59:59Z`).

        limit : typing.Optional[int]
            Number of results per page (default 20).

        page : typing.Optional[int]
            Page number.

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

        Returns
        -------
        GetBatchRootResponse
            Paginated list of batch jobs

        Examples
        --------
        import asyncio

        from ottimate import AsyncOttimate

        client = AsyncOttimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.batch.get_batch_root(
                limit=20,
                page=1,
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_batch_root(
            status=status, date_from=date_from, date_to=date_to, limit=limit, page=page, request_options=request_options
        )
        return _response.data

    async def get_batch_id_progress(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> BatchProgressResponse:
        """
        Returns the current progress of a batch operation. Poll this endpoint until `status` is `completed`. When completed, a `results_url` is included. If all tasks failed, an `error` field is included with the failure reason.

        Parameters
        ----------
        id : str
            The batch ID returned from the initiating bulk operation

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

        Returns
        -------
        BatchProgressResponse
            Successful response with current batch progress

        Examples
        --------
        import asyncio

        from ottimate import AsyncOttimate

        client = AsyncOttimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.batch.get_batch_id_progress(
                id="racc_90832490382904802384",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_batch_id_progress(id, request_options=request_options)
        return _response.data

    async def get_batch_id_results(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> BatchResultsResponse:
        """
        Returns the per-task results for a batch operation. Call this endpoint after polling `/progress` until `status` is `completed`. Each entry in `results` corresponds to one task, with the task outcome in `result` and any error message in `message`.

        Parameters
        ----------
        id : str
            The batch ID returned from the initiating bulk operation

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

        Returns
        -------
        BatchResultsResponse
            Successful response with batch results

        Examples
        --------
        import asyncio

        from ottimate import AsyncOttimate

        client = AsyncOttimate(
            api_key="YOUR_API_KEY",
            api_version="YOUR_API_VERSION",
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.batch.get_batch_id_results(
                id="racc_90832490382904802384",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_batch_id_results(id, request_options=request_options)
        return _response.data
