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

from __future__ import annotations
from .aggregationtype import AggregationType
from .windowsize import WindowSize
from flexprice.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[WindowSize]
    expression: NotRequired[str]
    r"""Expression is an optional CEL expression to compute per-event quantity from event.properties.
    When set, it replaces Field-based extraction. Property names are used directly (e.g., token * duration * pixel).
    """
    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\" 
    Ignored when Expression is set.
    """
    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[AggregationType]


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

    expression: Optional[str] = None
    r"""Expression is an optional CEL expression to compute per-event quantity from event.properties.
    When set, it replaces Field-based extraction. Property names are used directly (e.g., token * duration * pixel).
    """

    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\" 
    Ignored when Expression is set.
    """

    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[AggregationType] = None

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            ["bucket_size", "expression", "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, serialized.get(n))

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

        return m
