# 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 ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.agent_task_tracker import AgentTaskTracker
from ..types.http_validation_error import HttpValidationError

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


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

    def list(
        self,
        *,
        task_id: str,
        agent_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page_number: typing.Optional[int] = None,
        order_by: typing.Optional[str] = None,
        order_direction: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.List[AgentTaskTracker]]:
        """
        List agent task trackers for a task, optionally filtered by agent.

        Parameters
        ----------
        task_id : str
            Task ID (required for authorization)

        agent_id : typing.Optional[str]
            Agent ID

        limit : typing.Optional[int]
            Limit

        page_number : typing.Optional[int]
            Page number

        order_by : typing.Optional[str]
            Field to order by

        order_direction : typing.Optional[str]
            Order direction (asc or desc)

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

        Returns
        -------
        HttpResponse[typing.List[AgentTaskTracker]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "tracker",
            method="GET",
            params={
                "agent_id": agent_id,
                "limit": limit,
                "page_number": page_number,
                "order_by": order_by,
                "order_direction": order_direction,
                "task_id": task_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[AgentTaskTracker],
                    parse_obj_as(
                        type_=typing.List[AgentTaskTracker],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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)

    def retrieve(
        self, tracker_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[AgentTaskTracker]:
        """
        Get agent task tracker by tracker ID

        Parameters
        ----------
        tracker_id : str

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

        Returns
        -------
        HttpResponse[AgentTaskTracker]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"tracker/{jsonable_encoder(tracker_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AgentTaskTracker,
                    parse_obj_as(
                        type_=AgentTaskTracker,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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)

    def update(
        self,
        tracker_id: str,
        *,
        last_processed_event_id: typing.Optional[str] = OMIT,
        status: typing.Optional[str] = OMIT,
        status_reason: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[AgentTaskTracker]:
        """
        Update agent task tracker by tracker ID

        Parameters
        ----------
        tracker_id : str

        last_processed_event_id : typing.Optional[str]
            The most recent processed event ID (omit to leave unchanged)

        status : typing.Optional[str]
            Processing status

        status_reason : typing.Optional[str]
            Optional status reason

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

        Returns
        -------
        HttpResponse[AgentTaskTracker]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"tracker/{jsonable_encoder(tracker_id)}",
            method="PUT",
            json={
                "last_processed_event_id": last_processed_event_id,
                "status": status,
                "status_reason": status_reason,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AgentTaskTracker,
                    parse_obj_as(
                        type_=AgentTaskTracker,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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 AsyncRawTrackerClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        *,
        task_id: str,
        agent_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page_number: typing.Optional[int] = None,
        order_by: typing.Optional[str] = None,
        order_direction: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.List[AgentTaskTracker]]:
        """
        List agent task trackers for a task, optionally filtered by agent.

        Parameters
        ----------
        task_id : str
            Task ID (required for authorization)

        agent_id : typing.Optional[str]
            Agent ID

        limit : typing.Optional[int]
            Limit

        page_number : typing.Optional[int]
            Page number

        order_by : typing.Optional[str]
            Field to order by

        order_direction : typing.Optional[str]
            Order direction (asc or desc)

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

        Returns
        -------
        AsyncHttpResponse[typing.List[AgentTaskTracker]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "tracker",
            method="GET",
            params={
                "agent_id": agent_id,
                "limit": limit,
                "page_number": page_number,
                "order_by": order_by,
                "order_direction": order_direction,
                "task_id": task_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[AgentTaskTracker],
                    parse_obj_as(
                        type_=typing.List[AgentTaskTracker],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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)

    async def retrieve(
        self, tracker_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[AgentTaskTracker]:
        """
        Get agent task tracker by tracker ID

        Parameters
        ----------
        tracker_id : str

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

        Returns
        -------
        AsyncHttpResponse[AgentTaskTracker]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"tracker/{jsonable_encoder(tracker_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AgentTaskTracker,
                    parse_obj_as(
                        type_=AgentTaskTracker,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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)

    async def update(
        self,
        tracker_id: str,
        *,
        last_processed_event_id: typing.Optional[str] = OMIT,
        status: typing.Optional[str] = OMIT,
        status_reason: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[AgentTaskTracker]:
        """
        Update agent task tracker by tracker ID

        Parameters
        ----------
        tracker_id : str

        last_processed_event_id : typing.Optional[str]
            The most recent processed event ID (omit to leave unchanged)

        status : typing.Optional[str]
            Processing status

        status_reason : typing.Optional[str]
            Optional status reason

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

        Returns
        -------
        AsyncHttpResponse[AgentTaskTracker]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"tracker/{jsonable_encoder(tracker_id)}",
            method="PUT",
            json={
                "last_processed_event_id": last_processed_event_id,
                "status": status,
                "status_reason": status_reason,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AgentTaskTracker,
                    parse_obj_as(
                        type_=AgentTaskTracker,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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)
