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

from __future__ import annotations
from datetime import datetime
from paygentic_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
    UnrecognizedStr,
)
import pydantic
from pydantic import model_serializer
from typing import Dict, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypedDict


BillableMetricObject = Literal["billableMetric",]


Aggregation = Union[
    Literal[
        "SUM",
        "COUNT",
        "AVG",
        "MIN",
        "MAX",
        "UNIQUE_COUNT",
        "LATEST",
    ],
    UnrecognizedStr,
]


class BillableMetricTypedDict(TypedDict):
    id: str
    r"""Unique identifier for a billable metric"""
    aggregation: Aggregation
    created_at: datetime
    description: str
    merchant_id: str
    r"""Unique identifier for an organization"""
    name: str
    product_id: str
    r"""Unique identifier for a product"""
    unit: str
    updated_at: datetime
    object: NotRequired[BillableMetricObject]
    event_type: NotRequired[Nullable[str]]
    value_property: NotRequired[Nullable[str]]
    group_by: NotRequired[Nullable[Dict[str, str]]]
    event_from: NotRequired[Nullable[datetime]]


class BillableMetric(BaseModel):
    id: str
    r"""Unique identifier for a billable metric"""

    aggregation: Aggregation

    created_at: Annotated[datetime, pydantic.Field(alias="createdAt")]

    description: str

    merchant_id: Annotated[str, pydantic.Field(alias="merchantId")]
    r"""Unique identifier for an organization"""

    name: str

    product_id: Annotated[str, pydantic.Field(alias="productId")]
    r"""Unique identifier for a product"""

    unit: str

    updated_at: Annotated[datetime, pydantic.Field(alias="updatedAt")]

    object: Optional[BillableMetricObject] = "billableMetric"

    event_type: Annotated[OptionalNullable[str], pydantic.Field(alias="eventType")] = (
        UNSET
    )

    value_property: Annotated[
        OptionalNullable[str], pydantic.Field(alias="valueProperty")
    ] = UNSET

    group_by: Annotated[
        OptionalNullable[Dict[str, str]], pydantic.Field(alias="groupBy")
    ] = UNSET

    event_from: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="eventFrom")
    ] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            ["object", "eventType", "valueProperty", "groupBy", "eventFrom"]
        )
        nullable_fields = set(["eventType", "valueProperty", "groupBy", "eventFrom"])
        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))
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

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

        return m


try:
    BillableMetric.model_rebuild()
except NameError:
    pass
