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

from __future__ import annotations
from .types_aggregationtype import TypesAggregationType
from .types_windowsize import TypesWindowSize
from flexprice_py.types import BaseModel, UNSET_SENTINEL
from pydantic import model_serializer
from typing import Optional
from typing_extensions import NotRequired, TypedDict


class MeterAggregationTypedDict(TypedDict):
    bucket_size: NotRequired[TypesWindowSize]
    field: NotRequired[str]
    r"""Field is the key in $event.properties on which the aggregation is to be applied
    For ex if the aggregation type is sum for API usage, the field could be \"duration_ms\" 
    """
    group_by: NotRequired[str]
    r"""GroupBy is the property name in event.properties to group by before aggregating.
    Currently only supported for MAX aggregation with bucket_size.
    When set, aggregation is applied per unique value of this property within each bucket,
    then the per-group results are summed to produce the bucket total.
    """
    multiplier: NotRequired[str]
    r"""Multiplier is the multiplier for the aggregation
    For ex if the aggregation type is sum_with_multiplier for API usage, the multiplier could be 1000
    to scale up by a factor of 1000. If not provided, it will be null.
    """
    type: NotRequired[TypesAggregationType]


class MeterAggregation(BaseModel):
    bucket_size: Optional[TypesWindowSize] = None

    field: Optional[str] = None
    r"""Field is the key in $event.properties on which the aggregation is to be applied
    For ex if the aggregation type is sum for API usage, the field could be \"duration_ms\" 
    """

    group_by: Optional[str] = None
    r"""GroupBy is the property name in event.properties to group by before aggregating.
    Currently only supported for MAX aggregation with bucket_size.
    When set, aggregation is applied per unique value of this property within each bucket,
    then the per-group results are summed to produce the bucket total.
    """

    multiplier: Optional[str] = None
    r"""Multiplier is the multiplier for the aggregation
    For ex if the aggregation type is sum_with_multiplier for API usage, the multiplier could be 1000
    to scale up by a factor of 1000. If not provided, it will be null.
    """

    type: Optional[TypesAggregationType] = None

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            ["bucket_size", "field", "group_by", "multiplier", "type"]
        )
        serialized = handler(self)
        m = {}

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

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

        return m
