"""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
from paygentic_sdk.utils import FieldMetadata, QueryParamMetadata
import pydantic
from pydantic import model_serializer
from typing import List, Literal, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


BucketWidth = Literal[
    "hour",
    "day",
    "week",
]
r"""Time bucket granularity for trend data"""


GroupBy = Literal["plan",]
r"""Group invoice data by dimension. Max 5 groups (top 4 + 'other' when exceeding)."""


class GetRevenueRequestTypedDict(TypedDict):
    start_time: datetime
    r"""Start of the time range (ISO 8601 format)"""
    end_time: datetime
    r"""End of the time range (ISO 8601 format)"""
    bucket_width: NotRequired[BucketWidth]
    r"""Time bucket granularity for trend data"""
    merchant_id: NotRequired[str]
    r"""Filter by merchant ID. At least one of merchantId, subscriptionIds, or customerId must be provided."""
    customer_id: NotRequired[str]
    r"""Filter by customer ID. At least one of merchantId, subscriptionIds, or customerId must be provided."""
    subscription_ids: NotRequired[List[str]]
    r"""Filter by subscription IDs. At least one of merchantId, subscriptionIds, or customerId must be provided."""
    group_by: NotRequired[GroupBy]
    r"""Group invoice data by dimension. Max 5 groups (top 4 + 'other' when exceeding)."""


class GetRevenueRequest(BaseModel):
    start_time: Annotated[
        datetime,
        pydantic.Field(alias="startTime"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ]
    r"""Start of the time range (ISO 8601 format)"""

    end_time: Annotated[
        datetime,
        pydantic.Field(alias="endTime"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ]
    r"""End of the time range (ISO 8601 format)"""

    bucket_width: Annotated[
        Optional[BucketWidth],
        pydantic.Field(alias="bucketWidth"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = "day"
    r"""Time bucket granularity for trend data"""

    merchant_id: Annotated[
        Optional[str],
        pydantic.Field(alias="merchantId"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter by merchant ID. At least one of merchantId, subscriptionIds, or customerId must be provided."""

    customer_id: Annotated[
        Optional[str],
        pydantic.Field(alias="customerId"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter by customer ID. At least one of merchantId, subscriptionIds, or customerId must be provided."""

    subscription_ids: Annotated[
        Optional[List[str]],
        pydantic.Field(alias="subscriptionIds"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter by subscription IDs. At least one of merchantId, subscriptionIds, or customerId must be provided."""

    group_by: Annotated[
        Optional[GroupBy],
        pydantic.Field(alias="groupBy"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Group invoice data by dimension. Max 5 groups (top 4 + 'other' when exceeding)."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            ["bucketWidth", "merchantId", "customerId", "subscriptionIds", "groupBy"]
        )
        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
