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

import typing
from ..core.client_wrapper import SyncClientWrapper
from .types.trace_task import TraceTask
from ..types.user import User
from ..types.metadata import Metadata
from ..core.request_options import RequestOptions
from ..types.trace_response import TraceResponse
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.pydantic_utilities import parse_obj_as
from ..errors.method_not_allowed_error import MethodNotAllowedError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def trace(
        self,
        *,
        conversation_id: str,
        interaction_id: str,
        type: str,
        task: TraceTask,
        session_id: typing.Optional[str] = OMIT,
        channel_id: typing.Optional[str] = OMIT,
        input: typing.Optional[str] = OMIT,
        output: typing.Optional[str] = OMIT,
        start_timestamp: typing.Optional[str] = OMIT,
        end_timestamp: typing.Optional[str] = OMIT,
        latency: typing.Optional[int] = OMIT,
        custom: typing.Optional[str] = OMIT,
        user: typing.Optional[User] = OMIT,
        metadata: typing.Optional[Metadata] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TraceResponse:
        """
        Add a new trace

        Parameters
        ----------
        conversation_id : str
            conversation id

        interaction_id : str
            interaction id

        type : str
            type of trace

        task : TraceTask
            task to perform

        session_id : typing.Optional[str]
            session id

        channel_id : typing.Optional[str]
            channel id

        input : typing.Optional[str]
            content to retrieve or generate

        output : typing.Optional[str]
            generated content

        start_timestamp : typing.Optional[str]
            start timestamp of the trace

        end_timestamp : typing.Optional[str]
            end timestamp of the trace

        latency : typing.Optional[int]
            latency of the trace

        custom : typing.Optional[str]
            custom data

        user : typing.Optional[User]

        metadata : typing.Optional[Metadata]

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

        Returns
        -------
        TraceResponse
            Successful operation

        Examples
        --------
        from NeuralTrust import NeuralTrustApi

        client = NeuralTrustApi(
            api_key="YOUR_API_KEY",
        )
        client.trace.trace(
            conversation_id="conversation_id",
            interaction_id="interaction_id",
            type="type",
            task="retrieval",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "trace",
            method="POST",
            json={
                "conversation_id": conversation_id,
                "interaction_id": interaction_id,
                "session_id": session_id,
                "type": type,
                "task": task,
                "channel_id": channel_id,
                "input": input,
                "output": output,
                "start_timestamp": start_timestamp,
                "end_timestamp": end_timestamp,
                "latency": latency,
                "custom": custom,
                "user": convert_and_respect_annotation_metadata(
                    object_=user, annotation=User, direction="write"
                ),
                "metadata": convert_and_respect_annotation_metadata(
                    object_=metadata, annotation=Metadata, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    TraceResponse,
                    parse_obj_as(
                        type_=TraceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 405:
                raise MethodNotAllowedError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


class AsyncTraceClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def trace(
        self,
        *,
        conversation_id: str,
        interaction_id: str,
        type: str,
        task: TraceTask,
        session_id: typing.Optional[str] = OMIT,
        channel_id: typing.Optional[str] = OMIT,
        input: typing.Optional[str] = OMIT,
        output: typing.Optional[str] = OMIT,
        start_timestamp: typing.Optional[str] = OMIT,
        end_timestamp: typing.Optional[str] = OMIT,
        latency: typing.Optional[int] = OMIT,
        custom: typing.Optional[str] = OMIT,
        user: typing.Optional[User] = OMIT,
        metadata: typing.Optional[Metadata] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TraceResponse:
        """
        Add a new trace

        Parameters
        ----------
        conversation_id : str
            conversation id

        interaction_id : str
            interaction id

        type : str
            type of trace

        task : TraceTask
            task to perform

        session_id : typing.Optional[str]
            session id

        channel_id : typing.Optional[str]
            channel id

        input : typing.Optional[str]
            content to retrieve or generate

        output : typing.Optional[str]
            generated content

        start_timestamp : typing.Optional[str]
            start timestamp of the trace

        end_timestamp : typing.Optional[str]
            end timestamp of the trace

        latency : typing.Optional[int]
            latency of the trace

        custom : typing.Optional[str]
            custom data

        user : typing.Optional[User]

        metadata : typing.Optional[Metadata]

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

        Returns
        -------
        TraceResponse
            Successful operation

        Examples
        --------
        import asyncio

        from NeuralTrust import AsyncNeuralTrustApi

        client = AsyncNeuralTrustApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.trace.trace(
                conversation_id="conversation_id",
                interaction_id="interaction_id",
                type="type",
                task="retrieval",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "trace",
            method="POST",
            json={
                "conversation_id": conversation_id,
                "interaction_id": interaction_id,
                "session_id": session_id,
                "type": type,
                "task": task,
                "channel_id": channel_id,
                "input": input,
                "output": output,
                "start_timestamp": start_timestamp,
                "end_timestamp": end_timestamp,
                "latency": latency,
                "custom": custom,
                "user": convert_and_respect_annotation_metadata(
                    object_=user, annotation=User, direction="write"
                ),
                "metadata": convert_and_respect_annotation_metadata(
                    object_=metadata, annotation=Metadata, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    TraceResponse,
                    parse_obj_as(
                        type_=TraceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 405:
                raise MethodNotAllowedError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
