"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .spanschema import SpanSchema, SpanSchemaTypedDict
from datetime import datetime
from opperai.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from pydantic import model_serializer
from typing import List
from typing_extensions import NotRequired, TypedDict


class GetTraceResponseTypedDict(TypedDict):
    id: str
    r"""The id of the trace"""
    start_time: NotRequired[Nullable[datetime]]
    r"""The start time of the trace"""
    end_time: NotRequired[Nullable[datetime]]
    r"""The end time of the trace"""
    duration_ms: NotRequired[Nullable[int]]
    r"""The duration of the trace"""
    status: NotRequired[Nullable[str]]
    r"""The status of the trace"""
    name: NotRequired[Nullable[str]]
    r"""The name of the trace, set to the name of the root span of the trace"""
    input: NotRequired[Nullable[str]]
    r"""The input of the trace, set to the input of the root span of the trace"""
    output: NotRequired[Nullable[str]]
    r"""The output of the trace, set to the output of the root span of the trace"""
    total_tokens: NotRequired[Nullable[int]]
    r"""The total tokens of the trace"""
    spans: NotRequired[Nullable[List[SpanSchemaTypedDict]]]
    r"""The spans of the trace"""


class GetTraceResponse(BaseModel):
    id: str
    r"""The id of the trace"""

    start_time: OptionalNullable[datetime] = UNSET
    r"""The start time of the trace"""

    end_time: OptionalNullable[datetime] = UNSET
    r"""The end time of the trace"""

    duration_ms: OptionalNullable[int] = UNSET
    r"""The duration of the trace"""

    status: OptionalNullable[str] = UNSET
    r"""The status of the trace"""

    name: OptionalNullable[str] = UNSET
    r"""The name of the trace, set to the name of the root span of the trace"""

    input: OptionalNullable[str] = UNSET
    r"""The input of the trace, set to the input of the root span of the trace"""

    output: OptionalNullable[str] = UNSET
    r"""The output of the trace, set to the output of the root span of the trace"""

    total_tokens: OptionalNullable[int] = UNSET
    r"""The total tokens of the trace"""

    spans: OptionalNullable[List[SpanSchema]] = UNSET
    r"""The spans of the trace"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "start_time",
            "end_time",
            "duration_ms",
            "status",
            "name",
            "input",
            "output",
            "total_tokens",
            "spans",
        ]
        nullable_fields = [
            "start_time",
            "end_time",
            "duration_ms",
            "status",
            "name",
            "input",
            "output",
            "total_tokens",
            "spans",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
