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

from __future__ import annotations
from .timeinterval import TimeInterval
from datetime import datetime
from polar_sdk.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from polar_sdk.utils import FieldMetadata, PathParamMetadata, QueryParamMetadata
from pydantic import model_serializer
from typing import List, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


MetersQuantitiesQueryParamCustomerIDFilterTypedDict = TypeAliasType(
    "MetersQuantitiesQueryParamCustomerIDFilterTypedDict", Union[str, List[str]]
)
r"""Filter by customer ID."""


MetersQuantitiesQueryParamCustomerIDFilter = TypeAliasType(
    "MetersQuantitiesQueryParamCustomerIDFilter", Union[str, List[str]]
)
r"""Filter by customer ID."""


QueryParamExternalCustomerIDFilterTypedDict = TypeAliasType(
    "QueryParamExternalCustomerIDFilterTypedDict", Union[str, List[str]]
)
r"""Filter by external customer ID."""


QueryParamExternalCustomerIDFilter = TypeAliasType(
    "QueryParamExternalCustomerIDFilter", Union[str, List[str]]
)
r"""Filter by external customer ID."""


class MetersQuantitiesRequestTypedDict(TypedDict):
    id: str
    r"""The meter ID."""
    start_timestamp: datetime
    r"""Start timestamp."""
    end_timestamp: datetime
    r"""End timestamp."""
    interval: TimeInterval
    r"""Interval between two timestamps."""
    customer_id: NotRequired[
        Nullable[MetersQuantitiesQueryParamCustomerIDFilterTypedDict]
    ]
    r"""Filter by customer ID."""
    external_customer_id: NotRequired[
        Nullable[QueryParamExternalCustomerIDFilterTypedDict]
    ]
    r"""Filter by external customer ID."""


class MetersQuantitiesRequest(BaseModel):
    id: Annotated[
        str, FieldMetadata(path=PathParamMetadata(style="simple", explode=False))
    ]
    r"""The meter ID."""

    start_timestamp: Annotated[
        datetime, FieldMetadata(query=QueryParamMetadata(style="form", explode=True))
    ]
    r"""Start timestamp."""

    end_timestamp: Annotated[
        datetime, FieldMetadata(query=QueryParamMetadata(style="form", explode=True))
    ]
    r"""End timestamp."""

    interval: Annotated[
        TimeInterval,
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ]
    r"""Interval between two timestamps."""

    customer_id: Annotated[
        OptionalNullable[MetersQuantitiesQueryParamCustomerIDFilter],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Filter by customer ID."""

    external_customer_id: Annotated[
        OptionalNullable[QueryParamExternalCustomerIDFilter],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Filter by external customer ID."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["customer_id", "external_customer_id"]
        nullable_fields = ["customer_id", "external_customer_id"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in 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
