# 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.span import Span
from .raw_client import AsyncRawSpansClient, RawSpansClient

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


class SpansClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawSpansClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawSpansClient
        """
        return self._raw_client

    def list(
        self,
        *,
        task_id: str,
        trace_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,
    ) -> typing.List[Span]:
        """
        List all spans for a given task.
        Requires read permission on the task.
        Optionally filter by trace_id within the task.

        Parameters
        ----------
        task_id : str
            The task ID

        trace_id : typing.Optional[str]

        limit : typing.Optional[int]

        page_number : typing.Optional[int]

        order_by : typing.Optional[str]

        order_direction : typing.Optional[str]

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

        Returns
        -------
        typing.List[Span]
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.spans.list(
            task_id="task_id",
        )
        """
        _response = self._raw_client.list(
            task_id=task_id,
            trace_id=trace_id,
            limit=limit,
            page_number=page_number,
            order_by=order_by,
            order_direction=order_direction,
            request_options=request_options,
        )
        return _response.data

    def create(
        self,
        *,
        name: str,
        start_time: dt.datetime,
        task_id: str,
        trace_id: str,
        data: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        end_time: typing.Optional[dt.datetime] = OMIT,
        id: typing.Optional[str] = OMIT,
        input: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        output: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        parent_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Span:
        """
        Create a new span with the provided parameters.
        Requires update (edit) permission on the associated task.

        Parameters
        ----------
        name : str
            Name that describes what operation this span represents

        start_time : dt.datetime
            The time the span started

        task_id : str
            ID of the task this span is associated with, used for authorization

        trace_id : str
            Unique identifier for the trace this span belongs to

        data : typing.Optional[typing.Dict[str, typing.Any]]
            Any additional metadata or context for the span

        end_time : typing.Optional[dt.datetime]
            The time the span ended

        id : typing.Optional[str]
            Unique identifier for the span. If not provided, an ID will be generated.

        input : typing.Optional[typing.Dict[str, typing.Any]]
            Input parameters or data for the operation

        output : typing.Optional[typing.Dict[str, typing.Any]]
            Output data resulting from the operation

        parent_id : typing.Optional[str]
            ID of the parent span if this is a child span in a trace

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

        Returns
        -------
        Span
            Successful Response

        Examples
        --------
        import datetime

        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.spans.create(
            name="name",
            start_time=datetime.datetime.fromisoformat(
                "2024-01-15 09:30:00+00:00",
            ),
            task_id="task_id",
            trace_id="trace_id",
        )
        """
        _response = self._raw_client.create(
            name=name,
            start_time=start_time,
            task_id=task_id,
            trace_id=trace_id,
            data=data,
            end_time=end_time,
            id=id,
            input=input,
            output=output,
            parent_id=parent_id,
            request_options=request_options,
        )
        return _response.data

    def retrieve(self, span_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Span:
        """
        Get a span by ID.
        Requires read permission on the span's parent task.

        Parameters
        ----------
        span_id : str

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

        Returns
        -------
        Span
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.spans.retrieve(
            span_id="span_id",
        )
        """
        _response = self._raw_client.retrieve(span_id, request_options=request_options)
        return _response.data

    def update(
        self,
        span_id: str,
        *,
        data: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        end_time: typing.Optional[dt.datetime] = OMIT,
        input: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        name: typing.Optional[str] = OMIT,
        output: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        parent_id: typing.Optional[str] = OMIT,
        start_time: typing.Optional[dt.datetime] = OMIT,
        trace_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Span:
        """
        Update a span with the provided output data and mark it as complete.
        Requires update (edit) permission on the span's parent task.

        Parameters
        ----------
        span_id : str

        data : typing.Optional[typing.Dict[str, typing.Any]]
            Any additional metadata or context for the span

        end_time : typing.Optional[dt.datetime]
            The time the span ended

        input : typing.Optional[typing.Dict[str, typing.Any]]
            Input parameters or data for the operation

        name : typing.Optional[str]
            Name that describes what operation this span represents

        output : typing.Optional[typing.Dict[str, typing.Any]]
            Output data resulting from the operation

        parent_id : typing.Optional[str]
            ID of the parent span if this is a child span in a trace

        start_time : typing.Optional[dt.datetime]
            The time the span started

        trace_id : typing.Optional[str]
            Unique identifier for the trace this span belongs to

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

        Returns
        -------
        Span
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.spans.update(
            span_id="span_id",
        )
        """
        _response = self._raw_client.update(
            span_id,
            data=data,
            end_time=end_time,
            input=input,
            name=name,
            output=output,
            parent_id=parent_id,
            start_time=start_time,
            trace_id=trace_id,
            request_options=request_options,
        )
        return _response.data


class AsyncSpansClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawSpansClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawSpansClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        task_id: str,
        trace_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,
    ) -> typing.List[Span]:
        """
        List all spans for a given task.
        Requires read permission on the task.
        Optionally filter by trace_id within the task.

        Parameters
        ----------
        task_id : str
            The task ID

        trace_id : typing.Optional[str]

        limit : typing.Optional[int]

        page_number : typing.Optional[int]

        order_by : typing.Optional[str]

        order_direction : typing.Optional[str]

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

        Returns
        -------
        typing.List[Span]
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.spans.list(
                task_id="task_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list(
            task_id=task_id,
            trace_id=trace_id,
            limit=limit,
            page_number=page_number,
            order_by=order_by,
            order_direction=order_direction,
            request_options=request_options,
        )
        return _response.data

    async def create(
        self,
        *,
        name: str,
        start_time: dt.datetime,
        task_id: str,
        trace_id: str,
        data: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        end_time: typing.Optional[dt.datetime] = OMIT,
        id: typing.Optional[str] = OMIT,
        input: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        output: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        parent_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Span:
        """
        Create a new span with the provided parameters.
        Requires update (edit) permission on the associated task.

        Parameters
        ----------
        name : str
            Name that describes what operation this span represents

        start_time : dt.datetime
            The time the span started

        task_id : str
            ID of the task this span is associated with, used for authorization

        trace_id : str
            Unique identifier for the trace this span belongs to

        data : typing.Optional[typing.Dict[str, typing.Any]]
            Any additional metadata or context for the span

        end_time : typing.Optional[dt.datetime]
            The time the span ended

        id : typing.Optional[str]
            Unique identifier for the span. If not provided, an ID will be generated.

        input : typing.Optional[typing.Dict[str, typing.Any]]
            Input parameters or data for the operation

        output : typing.Optional[typing.Dict[str, typing.Any]]
            Output data resulting from the operation

        parent_id : typing.Optional[str]
            ID of the parent span if this is a child span in a trace

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

        Returns
        -------
        Span
            Successful Response

        Examples
        --------
        import asyncio
        import datetime

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.spans.create(
                name="name",
                start_time=datetime.datetime.fromisoformat(
                    "2024-01-15 09:30:00+00:00",
                ),
                task_id="task_id",
                trace_id="trace_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create(
            name=name,
            start_time=start_time,
            task_id=task_id,
            trace_id=trace_id,
            data=data,
            end_time=end_time,
            id=id,
            input=input,
            output=output,
            parent_id=parent_id,
            request_options=request_options,
        )
        return _response.data

    async def retrieve(self, span_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Span:
        """
        Get a span by ID.
        Requires read permission on the span's parent task.

        Parameters
        ----------
        span_id : str

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

        Returns
        -------
        Span
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.spans.retrieve(
                span_id="span_id",
            )


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

    async def update(
        self,
        span_id: str,
        *,
        data: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        end_time: typing.Optional[dt.datetime] = OMIT,
        input: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        name: typing.Optional[str] = OMIT,
        output: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        parent_id: typing.Optional[str] = OMIT,
        start_time: typing.Optional[dt.datetime] = OMIT,
        trace_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Span:
        """
        Update a span with the provided output data and mark it as complete.
        Requires update (edit) permission on the span's parent task.

        Parameters
        ----------
        span_id : str

        data : typing.Optional[typing.Dict[str, typing.Any]]
            Any additional metadata or context for the span

        end_time : typing.Optional[dt.datetime]
            The time the span ended

        input : typing.Optional[typing.Dict[str, typing.Any]]
            Input parameters or data for the operation

        name : typing.Optional[str]
            Name that describes what operation this span represents

        output : typing.Optional[typing.Dict[str, typing.Any]]
            Output data resulting from the operation

        parent_id : typing.Optional[str]
            ID of the parent span if this is a child span in a trace

        start_time : typing.Optional[dt.datetime]
            The time the span started

        trace_id : typing.Optional[str]
            Unique identifier for the trace this span belongs to

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

        Returns
        -------
        Span
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.spans.update(
                span_id="span_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update(
            span_id,
            data=data,
            end_time=end_time,
            input=input,
            name=name,
            output=output,
            parent_id=parent_id,
            start_time=start_time,
            trace_id=trace_id,
            request_options=request_options,
        )
        return _response.data
