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

from __future__ import annotations
from .defaultparameters import DefaultParameters, DefaultParametersTypedDict
from .modelarchitecture import ModelArchitecture, ModelArchitectureTypedDict
from .modelbenchmarks import ModelBenchmarks, ModelBenchmarksTypedDict
from .modellinks import ModelLinks, ModelLinksTypedDict
from .modelreasoning import ModelReasoning, ModelReasoningTypedDict
from .parameter import Parameter
from .perrequestlimits import PerRequestLimits, PerRequestLimitsTypedDict
from .publicpricing import PublicPricing, PublicPricingTypedDict
from .topproviderinfo import TopProviderInfo, TopProviderInfoTypedDict
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from openrouter.utils import validate_open_enum
from pydantic import model_serializer
from pydantic.functional_validators import PlainValidator
from typing import List, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class ModelTypedDict(TypedDict):
    r"""Information about an AI model available on OpenRouter"""

    architecture: ModelArchitectureTypedDict
    r"""Model architecture information"""
    canonical_slug: str
    r"""Canonical slug for the model"""
    context_length: Nullable[int]
    r"""Maximum context length in tokens"""
    created: int
    r"""Unix timestamp of when the model was created"""
    default_parameters: Nullable[DefaultParametersTypedDict]
    r"""Default parameters for this model"""
    id: str
    r"""Unique identifier for the model"""
    links: ModelLinksTypedDict
    r"""Related API endpoints and resources for this model."""
    name: str
    r"""Display name of the model"""
    per_request_limits: Nullable[PerRequestLimitsTypedDict]
    r"""Per-request token limits"""
    pricing: PublicPricingTypedDict
    r"""Pricing information for the model"""
    supported_parameters: List[Parameter]
    r"""List of supported parameters for this model"""
    supported_voices: Nullable[List[str]]
    r"""List of supported voice identifiers for TTS models. Null for non-TTS models."""
    top_provider: TopProviderInfoTypedDict
    r"""Information about the top provider for this model"""
    benchmarks: NotRequired[ModelBenchmarksTypedDict]
    r"""Third-party benchmark rankings for this model. Omitted when no benchmark data is available."""
    description: NotRequired[str]
    r"""Description of the model"""
    expiration_date: NotRequired[Nullable[str]]
    r"""The date after which the model may be removed. ISO 8601 date string (YYYY-MM-DD) or null if no expiration."""
    hugging_face_id: NotRequired[Nullable[str]]
    r"""Hugging Face model identifier, if applicable"""
    knowledge_cutoff: NotRequired[Nullable[str]]
    r"""The date up to which the model was trained on data. ISO 8601 date string (YYYY-MM-DD) or null if unknown."""
    reasoning: NotRequired[ModelReasoningTypedDict]
    r"""Reasoning effort configuration. Omitted for non-reasoning models and dynamic router models."""


class Model(BaseModel):
    r"""Information about an AI model available on OpenRouter"""

    architecture: ModelArchitecture
    r"""Model architecture information"""

    canonical_slug: str
    r"""Canonical slug for the model"""

    context_length: Nullable[int]
    r"""Maximum context length in tokens"""

    created: int
    r"""Unix timestamp of when the model was created"""

    default_parameters: Nullable[DefaultParameters]
    r"""Default parameters for this model"""

    id: str
    r"""Unique identifier for the model"""

    links: ModelLinks
    r"""Related API endpoints and resources for this model."""

    name: str
    r"""Display name of the model"""

    per_request_limits: Nullable[PerRequestLimits]
    r"""Per-request token limits"""

    pricing: PublicPricing
    r"""Pricing information for the model"""

    supported_parameters: List[
        Annotated[Parameter, PlainValidator(validate_open_enum(False))]
    ]
    r"""List of supported parameters for this model"""

    supported_voices: Nullable[List[str]]
    r"""List of supported voice identifiers for TTS models. Null for non-TTS models."""

    top_provider: TopProviderInfo
    r"""Information about the top provider for this model"""

    benchmarks: Optional[ModelBenchmarks] = None
    r"""Third-party benchmark rankings for this model. Omitted when no benchmark data is available."""

    description: Optional[str] = None
    r"""Description of the model"""

    expiration_date: OptionalNullable[str] = UNSET
    r"""The date after which the model may be removed. ISO 8601 date string (YYYY-MM-DD) or null if no expiration."""

    hugging_face_id: OptionalNullable[str] = UNSET
    r"""Hugging Face model identifier, if applicable"""

    knowledge_cutoff: OptionalNullable[str] = UNSET
    r"""The date up to which the model was trained on data. ISO 8601 date string (YYYY-MM-DD) or null if unknown."""

    reasoning: Optional[ModelReasoning] = None
    r"""Reasoning effort configuration. Omitted for non-reasoning models and dynamic router models."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "benchmarks",
            "description",
            "expiration_date",
            "hugging_face_id",
            "knowledge_cutoff",
            "reasoning",
        ]
        nullable_fields = [
            "context_length",
            "default_parameters",
            "expiration_date",
            "hugging_face_id",
            "knowledge_cutoff",
            "per_request_limits",
            "supported_voices",
        ]
        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
