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

from __future__ import annotations
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from pydantic import model_serializer
from typing import Optional
from typing_extensions import NotRequired, TypedDict


class CompletionTokensDetailsTypedDict(TypedDict):
    reasoning_tokens: NotRequired[Nullable[float]]
    audio_tokens: NotRequired[Nullable[float]]
    accepted_prediction_tokens: NotRequired[Nullable[float]]
    rejected_prediction_tokens: NotRequired[Nullable[float]]


class CompletionTokensDetails(BaseModel):
    reasoning_tokens: OptionalNullable[float] = UNSET

    audio_tokens: OptionalNullable[float] = UNSET

    accepted_prediction_tokens: OptionalNullable[float] = UNSET

    rejected_prediction_tokens: OptionalNullable[float] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "reasoning_tokens",
            "audio_tokens",
            "accepted_prediction_tokens",
            "rejected_prediction_tokens",
        ]
        nullable_fields = [
            "reasoning_tokens",
            "audio_tokens",
            "accepted_prediction_tokens",
            "rejected_prediction_tokens",
        ]
        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


class PromptTokensDetailsTypedDict(TypedDict):
    cached_tokens: NotRequired[float]
    cache_write_tokens: NotRequired[float]
    audio_tokens: NotRequired[float]
    video_tokens: NotRequired[float]


class PromptTokensDetails(BaseModel):
    cached_tokens: Optional[float] = None

    cache_write_tokens: Optional[float] = None

    audio_tokens: Optional[float] = None

    video_tokens: Optional[float] = None


class ChatGenerationTokenUsageTypedDict(TypedDict):
    completion_tokens: float
    prompt_tokens: float
    total_tokens: float
    completion_tokens_details: NotRequired[Nullable[CompletionTokensDetailsTypedDict]]
    prompt_tokens_details: NotRequired[Nullable[PromptTokensDetailsTypedDict]]


class ChatGenerationTokenUsage(BaseModel):
    completion_tokens: float

    prompt_tokens: float

    total_tokens: float

    completion_tokens_details: OptionalNullable[CompletionTokensDetails] = UNSET

    prompt_tokens_details: OptionalNullable[PromptTokensDetails] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["completion_tokens_details", "prompt_tokens_details"]
        nullable_fields = ["completion_tokens_details", "prompt_tokens_details"]
        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
