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

from __future__ import annotations
from datetime import datetime
from paygentic_sdk.types import BaseModel, UNSET_SENTINEL
import pydantic
from pydantic import model_serializer
from typing import Dict, List, Literal, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


UsageResponseObject = Literal["meter",]


class WindowedValueTypedDict(TypedDict):
    window_start: datetime
    window_end: datetime
    value: float


class WindowedValue(BaseModel):
    window_start: Annotated[datetime, pydantic.Field(alias="windowStart")]

    window_end: Annotated[datetime, pydantic.Field(alias="windowEnd")]

    value: float


class GroupedValueTypedDict(TypedDict):
    group_by: Dict[str, str]
    value: float


class GroupedValue(BaseModel):
    group_by: Annotated[Dict[str, str], pydantic.Field(alias="groupBy")]

    value: float


class UsageResponseTypedDict(TypedDict):
    billable_metric_id: str
    r"""Unique identifier for a billable metric"""
    total_value: float
    r"""Total aggregated value across the query window"""
    object: NotRequired[UsageResponseObject]
    windowed_values: NotRequired[List[WindowedValueTypedDict]]
    r"""Time-bucketed values. Only present when windowSize is specified."""
    grouped_values: NotRequired[List[GroupedValueTypedDict]]
    r"""Dimension-grouped values. Only present when groupBy is specified."""


class UsageResponse(BaseModel):
    billable_metric_id: Annotated[str, pydantic.Field(alias="billableMetricId")]
    r"""Unique identifier for a billable metric"""

    total_value: Annotated[float, pydantic.Field(alias="totalValue")]
    r"""Total aggregated value across the query window"""

    object: Optional[UsageResponseObject] = "meter"

    windowed_values: Annotated[
        Optional[List[WindowedValue]], pydantic.Field(alias="windowedValues")
    ] = None
    r"""Time-bucketed values. Only present when windowSize is specified."""

    grouped_values: Annotated[
        Optional[List[GroupedValue]], pydantic.Field(alias="groupedValues")
    ] = None
    r"""Dimension-grouped values. Only present when groupBy is specified."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["object", "windowedValues", "groupedValues"])
        serialized = handler(self)
        m = {}

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

            if val != UNSET_SENTINEL:
                if val is not None or k not in optional_fields:
                    m[k] = val

        return m


try:
    WindowedValue.model_rebuild()
except NameError:
    pass
try:
    GroupedValue.model_rebuild()
except NameError:
    pass
try:
    UsageResponse.model_rebuild()
except NameError:
    pass
