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

from __future__ import annotations
from .endpointstatus import EndpointStatus
from .parameter import Parameter
from .percentilestats import PercentileStats, PercentileStatsTypedDict
from .providername import ProviderName
from openrouter.types import BaseModel, Nullable, UNSET_SENTINEL, UnrecognizedStr
from openrouter.utils import validate_open_enum
from pydantic import model_serializer
from pydantic.functional_validators import PlainValidator
from typing import List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypedDict


class PricingTypedDict(TypedDict):
    completion: str
    prompt: str
    audio: NotRequired[str]
    audio_output: NotRequired[str]
    discount: NotRequired[float]
    image: NotRequired[str]
    image_output: NotRequired[str]
    image_token: NotRequired[str]
    input_audio_cache: NotRequired[str]
    input_cache_read: NotRequired[str]
    input_cache_write: NotRequired[str]
    internal_reasoning: NotRequired[str]
    request: NotRequired[str]
    web_search: NotRequired[str]


class Pricing(BaseModel):
    completion: str

    prompt: str

    audio: Optional[str] = None

    audio_output: Optional[str] = None

    discount: Optional[float] = None

    image: Optional[str] = None

    image_output: Optional[str] = None

    image_token: Optional[str] = None

    input_audio_cache: Optional[str] = None

    input_cache_read: Optional[str] = None

    input_cache_write: Optional[str] = None

    internal_reasoning: Optional[str] = None

    request: Optional[str] = None

    web_search: Optional[str] = None


PublicEndpointQuantization = Union[
    Literal[
        "int4",
        "int8",
        "fp4",
        "fp6",
        "fp8",
        "fp16",
        "bf16",
        "fp32",
        "unknown",
    ],
    UnrecognizedStr,
]


class PublicEndpointTypedDict(TypedDict):
    r"""Information about a specific model endpoint"""

    context_length: int
    latency_last_30m: Nullable[PercentileStatsTypedDict]
    r"""Latency percentiles in milliseconds over the last 30 minutes. Latency measures time to first token. Only visible when authenticated with an API key or cookie; returns null for unauthenticated requests."""
    max_completion_tokens: Nullable[int]
    max_prompt_tokens: Nullable[int]
    model_id: str
    r"""The unique identifier for the model (permaslug)"""
    model_name: str
    name: str
    pricing: PricingTypedDict
    provider_name: ProviderName
    quantization: Nullable[PublicEndpointQuantization]
    supported_parameters: List[Parameter]
    supports_implicit_caching: bool
    tag: str
    throughput_last_30m: Nullable[PercentileStatsTypedDict]
    uptime_last_1d: Nullable[float]
    r"""Uptime percentage over the last 1 day, calculated as successful requests / (successful + error requests) * 100. Rate-limited requests are excluded. Returns null if insufficient data."""
    uptime_last_30m: Nullable[float]
    uptime_last_5m: Nullable[float]
    r"""Uptime percentage over the last 5 minutes, calculated as successful requests / (successful + error requests) * 100. Rate-limited requests are excluded. Returns null if insufficient data."""
    status: NotRequired[EndpointStatus]


class PublicEndpoint(BaseModel):
    r"""Information about a specific model endpoint"""

    context_length: int

    latency_last_30m: Nullable[PercentileStats]
    r"""Latency percentiles in milliseconds over the last 30 minutes. Latency measures time to first token. Only visible when authenticated with an API key or cookie; returns null for unauthenticated requests."""

    max_completion_tokens: Nullable[int]

    max_prompt_tokens: Nullable[int]

    model_id: str
    r"""The unique identifier for the model (permaslug)"""

    model_name: str

    name: str

    pricing: Pricing

    provider_name: Annotated[ProviderName, PlainValidator(validate_open_enum(False))]

    quantization: Annotated[
        Nullable[PublicEndpointQuantization], PlainValidator(validate_open_enum(False))
    ]

    supported_parameters: List[
        Annotated[Parameter, PlainValidator(validate_open_enum(False))]
    ]

    supports_implicit_caching: bool

    tag: str

    throughput_last_30m: Nullable[PercentileStats]

    uptime_last_1d: Nullable[float]
    r"""Uptime percentage over the last 1 day, calculated as successful requests / (successful + error requests) * 100. Rate-limited requests are excluded. Returns null if insufficient data."""

    uptime_last_30m: Nullable[float]

    uptime_last_5m: Nullable[float]
    r"""Uptime percentage over the last 5 minutes, calculated as successful requests / (successful + error requests) * 100. Rate-limited requests are excluded. Returns null if insufficient data."""

    status: Annotated[
        Optional[EndpointStatus], PlainValidator(validate_open_enum(True))
    ] = None

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["status"]
        nullable_fields = [
            "latency_last_30m",
            "max_completion_tokens",
            "max_prompt_tokens",
            "quantization",
            "throughput_last_30m",
            "uptime_last_1d",
            "uptime_last_30m",
            "uptime_last_5m",
        ]
        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
