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

from __future__ import annotations
from datetime import datetime
from polar_sdk.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from pydantic import model_serializer
from typing import Union
from typing_extensions import NotRequired, TypeAliasType, TypedDict


OrdersModelTypedDict = TypeAliasType("OrdersModelTypedDict", Union[int, float])


OrdersModel = TypeAliasType("OrdersModel", Union[int, float])


RevenueTypedDict = TypeAliasType("RevenueTypedDict", Union[int, float])


Revenue = TypeAliasType("Revenue", Union[int, float])


NetRevenueTypedDict = TypeAliasType("NetRevenueTypedDict", Union[int, float])


NetRevenue = TypeAliasType("NetRevenue", Union[int, float])


CumulativeRevenueTypedDict = TypeAliasType(
    "CumulativeRevenueTypedDict", Union[int, float]
)


CumulativeRevenue = TypeAliasType("CumulativeRevenue", Union[int, float])


NetCumulativeRevenueTypedDict = TypeAliasType(
    "NetCumulativeRevenueTypedDict", Union[int, float]
)


NetCumulativeRevenue = TypeAliasType("NetCumulativeRevenue", Union[int, float])


CostsTypedDict = TypeAliasType("CostsTypedDict", Union[int, float])


Costs = TypeAliasType("Costs", Union[int, float])


CumulativeCostsTypedDict = TypeAliasType("CumulativeCostsTypedDict", Union[int, float])


CumulativeCosts = TypeAliasType("CumulativeCosts", Union[int, float])


AverageOrderValueTypedDict = TypeAliasType(
    "AverageOrderValueTypedDict", Union[int, float]
)


AverageOrderValue = TypeAliasType("AverageOrderValue", Union[int, float])


NetAverageOrderValueTypedDict = TypeAliasType(
    "NetAverageOrderValueTypedDict", Union[int, float]
)


NetAverageOrderValue = TypeAliasType("NetAverageOrderValue", Union[int, float])


AverageRevenuePerUserTypedDict = TypeAliasType(
    "AverageRevenuePerUserTypedDict", Union[int, float]
)


AverageRevenuePerUser = TypeAliasType("AverageRevenuePerUser", Union[int, float])


CostPerUserTypedDict = TypeAliasType("CostPerUserTypedDict", Union[int, float])


CostPerUser = TypeAliasType("CostPerUser", Union[int, float])


ActiveUserByEventTypedDict = TypeAliasType(
    "ActiveUserByEventTypedDict", Union[int, float]
)


ActiveUserByEvent = TypeAliasType("ActiveUserByEvent", Union[int, float])


OneTimeProductsTypedDict = TypeAliasType("OneTimeProductsTypedDict", Union[int, float])


OneTimeProducts = TypeAliasType("OneTimeProducts", Union[int, float])


OneTimeProductsRevenueTypedDict = TypeAliasType(
    "OneTimeProductsRevenueTypedDict", Union[int, float]
)


OneTimeProductsRevenue = TypeAliasType("OneTimeProductsRevenue", Union[int, float])


OneTimeProductsNetRevenueTypedDict = TypeAliasType(
    "OneTimeProductsNetRevenueTypedDict", Union[int, float]
)


OneTimeProductsNetRevenue = TypeAliasType(
    "OneTimeProductsNetRevenue", Union[int, float]
)


NewSubscriptionsTypedDict = TypeAliasType(
    "NewSubscriptionsTypedDict", Union[int, float]
)


NewSubscriptions = TypeAliasType("NewSubscriptions", Union[int, float])


NewSubscriptionsRevenueTypedDict = TypeAliasType(
    "NewSubscriptionsRevenueTypedDict", Union[int, float]
)


NewSubscriptionsRevenue = TypeAliasType("NewSubscriptionsRevenue", Union[int, float])


NewSubscriptionsNetRevenueTypedDict = TypeAliasType(
    "NewSubscriptionsNetRevenueTypedDict", Union[int, float]
)


NewSubscriptionsNetRevenue = TypeAliasType(
    "NewSubscriptionsNetRevenue", Union[int, float]
)


RenewedSubscriptionsTypedDict = TypeAliasType(
    "RenewedSubscriptionsTypedDict", Union[int, float]
)


RenewedSubscriptions = TypeAliasType("RenewedSubscriptions", Union[int, float])


RenewedSubscriptionsRevenueTypedDict = TypeAliasType(
    "RenewedSubscriptionsRevenueTypedDict", Union[int, float]
)


RenewedSubscriptionsRevenue = TypeAliasType(
    "RenewedSubscriptionsRevenue", Union[int, float]
)


RenewedSubscriptionsNetRevenueTypedDict = TypeAliasType(
    "RenewedSubscriptionsNetRevenueTypedDict", Union[int, float]
)


RenewedSubscriptionsNetRevenue = TypeAliasType(
    "RenewedSubscriptionsNetRevenue", Union[int, float]
)


ActiveSubscriptionsTypedDict = TypeAliasType(
    "ActiveSubscriptionsTypedDict", Union[int, float]
)


ActiveSubscriptions = TypeAliasType("ActiveSubscriptions", Union[int, float])


CommittedSubscriptionsTypedDict = TypeAliasType(
    "CommittedSubscriptionsTypedDict", Union[int, float]
)


CommittedSubscriptions = TypeAliasType("CommittedSubscriptions", Union[int, float])


MonthlyRecurringRevenueTypedDict = TypeAliasType(
    "MonthlyRecurringRevenueTypedDict", Union[int, float]
)


MonthlyRecurringRevenue = TypeAliasType("MonthlyRecurringRevenue", Union[int, float])


CommittedMonthlyRecurringRevenueTypedDict = TypeAliasType(
    "CommittedMonthlyRecurringRevenueTypedDict", Union[int, float]
)


CommittedMonthlyRecurringRevenue = TypeAliasType(
    "CommittedMonthlyRecurringRevenue", Union[int, float]
)


CheckoutsModelTypedDict = TypeAliasType("CheckoutsModelTypedDict", Union[int, float])


CheckoutsModel = TypeAliasType("CheckoutsModel", Union[int, float])


SucceededCheckoutsTypedDict = TypeAliasType(
    "SucceededCheckoutsTypedDict", Union[int, float]
)


SucceededCheckouts = TypeAliasType("SucceededCheckouts", Union[int, float])


CheckoutsConversionTypedDict = TypeAliasType(
    "CheckoutsConversionTypedDict", Union[int, float]
)


CheckoutsConversion = TypeAliasType("CheckoutsConversion", Union[int, float])


CanceledSubscriptionsTypedDict = TypeAliasType(
    "CanceledSubscriptionsTypedDict", Union[int, float]
)


CanceledSubscriptions = TypeAliasType("CanceledSubscriptions", Union[int, float])


CanceledSubscriptionsCustomerServiceTypedDict = TypeAliasType(
    "CanceledSubscriptionsCustomerServiceTypedDict", Union[int, float]
)


CanceledSubscriptionsCustomerService = TypeAliasType(
    "CanceledSubscriptionsCustomerService", Union[int, float]
)


CanceledSubscriptionsLowQualityTypedDict = TypeAliasType(
    "CanceledSubscriptionsLowQualityTypedDict", Union[int, float]
)


CanceledSubscriptionsLowQuality = TypeAliasType(
    "CanceledSubscriptionsLowQuality", Union[int, float]
)


CanceledSubscriptionsMissingFeaturesTypedDict = TypeAliasType(
    "CanceledSubscriptionsMissingFeaturesTypedDict", Union[int, float]
)


CanceledSubscriptionsMissingFeatures = TypeAliasType(
    "CanceledSubscriptionsMissingFeatures", Union[int, float]
)


CanceledSubscriptionsSwitchedServiceTypedDict = TypeAliasType(
    "CanceledSubscriptionsSwitchedServiceTypedDict", Union[int, float]
)


CanceledSubscriptionsSwitchedService = TypeAliasType(
    "CanceledSubscriptionsSwitchedService", Union[int, float]
)


CanceledSubscriptionsTooComplexTypedDict = TypeAliasType(
    "CanceledSubscriptionsTooComplexTypedDict", Union[int, float]
)


CanceledSubscriptionsTooComplex = TypeAliasType(
    "CanceledSubscriptionsTooComplex", Union[int, float]
)


CanceledSubscriptionsTooExpensiveTypedDict = TypeAliasType(
    "CanceledSubscriptionsTooExpensiveTypedDict", Union[int, float]
)


CanceledSubscriptionsTooExpensive = TypeAliasType(
    "CanceledSubscriptionsTooExpensive", Union[int, float]
)


CanceledSubscriptionsUnusedTypedDict = TypeAliasType(
    "CanceledSubscriptionsUnusedTypedDict", Union[int, float]
)


CanceledSubscriptionsUnused = TypeAliasType(
    "CanceledSubscriptionsUnused", Union[int, float]
)


CanceledSubscriptionsOtherTypedDict = TypeAliasType(
    "CanceledSubscriptionsOtherTypedDict", Union[int, float]
)


CanceledSubscriptionsOther = TypeAliasType(
    "CanceledSubscriptionsOther", Union[int, float]
)


ChurnedSubscriptionsTypedDict = TypeAliasType(
    "ChurnedSubscriptionsTypedDict", Union[int, float]
)


ChurnedSubscriptions = TypeAliasType("ChurnedSubscriptions", Union[int, float])


ChurnRateTypedDict = TypeAliasType("ChurnRateTypedDict", Union[int, float])


ChurnRate = TypeAliasType("ChurnRate", Union[int, float])


LtvTypedDict = TypeAliasType("LtvTypedDict", Union[int, float])


Ltv = TypeAliasType("Ltv", Union[int, float])


GrossMarginTypedDict = TypeAliasType("GrossMarginTypedDict", Union[int, float])


GrossMargin = TypeAliasType("GrossMargin", Union[int, float])


GrossMarginPercentageTypedDict = TypeAliasType(
    "GrossMarginPercentageTypedDict", Union[int, float]
)


GrossMarginPercentage = TypeAliasType("GrossMarginPercentage", Union[int, float])


CashflowTypedDict = TypeAliasType("CashflowTypedDict", Union[int, float])


Cashflow = TypeAliasType("Cashflow", Union[int, float])


class MetricPeriodTypedDict(TypedDict):
    timestamp: datetime
    r"""Timestamp of this period data."""
    orders: NotRequired[Nullable[OrdersModelTypedDict]]
    revenue: NotRequired[Nullable[RevenueTypedDict]]
    net_revenue: NotRequired[Nullable[NetRevenueTypedDict]]
    cumulative_revenue: NotRequired[Nullable[CumulativeRevenueTypedDict]]
    net_cumulative_revenue: NotRequired[Nullable[NetCumulativeRevenueTypedDict]]
    costs: NotRequired[Nullable[CostsTypedDict]]
    cumulative_costs: NotRequired[Nullable[CumulativeCostsTypedDict]]
    average_order_value: NotRequired[Nullable[AverageOrderValueTypedDict]]
    net_average_order_value: NotRequired[Nullable[NetAverageOrderValueTypedDict]]
    average_revenue_per_user: NotRequired[Nullable[AverageRevenuePerUserTypedDict]]
    cost_per_user: NotRequired[Nullable[CostPerUserTypedDict]]
    active_user_by_event: NotRequired[Nullable[ActiveUserByEventTypedDict]]
    one_time_products: NotRequired[Nullable[OneTimeProductsTypedDict]]
    one_time_products_revenue: NotRequired[Nullable[OneTimeProductsRevenueTypedDict]]
    one_time_products_net_revenue: NotRequired[
        Nullable[OneTimeProductsNetRevenueTypedDict]
    ]
    new_subscriptions: NotRequired[Nullable[NewSubscriptionsTypedDict]]
    new_subscriptions_revenue: NotRequired[Nullable[NewSubscriptionsRevenueTypedDict]]
    new_subscriptions_net_revenue: NotRequired[
        Nullable[NewSubscriptionsNetRevenueTypedDict]
    ]
    renewed_subscriptions: NotRequired[Nullable[RenewedSubscriptionsTypedDict]]
    renewed_subscriptions_revenue: NotRequired[
        Nullable[RenewedSubscriptionsRevenueTypedDict]
    ]
    renewed_subscriptions_net_revenue: NotRequired[
        Nullable[RenewedSubscriptionsNetRevenueTypedDict]
    ]
    active_subscriptions: NotRequired[Nullable[ActiveSubscriptionsTypedDict]]
    committed_subscriptions: NotRequired[Nullable[CommittedSubscriptionsTypedDict]]
    monthly_recurring_revenue: NotRequired[Nullable[MonthlyRecurringRevenueTypedDict]]
    committed_monthly_recurring_revenue: NotRequired[
        Nullable[CommittedMonthlyRecurringRevenueTypedDict]
    ]
    checkouts: NotRequired[Nullable[CheckoutsModelTypedDict]]
    succeeded_checkouts: NotRequired[Nullable[SucceededCheckoutsTypedDict]]
    checkouts_conversion: NotRequired[Nullable[CheckoutsConversionTypedDict]]
    canceled_subscriptions: NotRequired[Nullable[CanceledSubscriptionsTypedDict]]
    canceled_subscriptions_customer_service: NotRequired[
        Nullable[CanceledSubscriptionsCustomerServiceTypedDict]
    ]
    canceled_subscriptions_low_quality: NotRequired[
        Nullable[CanceledSubscriptionsLowQualityTypedDict]
    ]
    canceled_subscriptions_missing_features: NotRequired[
        Nullable[CanceledSubscriptionsMissingFeaturesTypedDict]
    ]
    canceled_subscriptions_switched_service: NotRequired[
        Nullable[CanceledSubscriptionsSwitchedServiceTypedDict]
    ]
    canceled_subscriptions_too_complex: NotRequired[
        Nullable[CanceledSubscriptionsTooComplexTypedDict]
    ]
    canceled_subscriptions_too_expensive: NotRequired[
        Nullable[CanceledSubscriptionsTooExpensiveTypedDict]
    ]
    canceled_subscriptions_unused: NotRequired[
        Nullable[CanceledSubscriptionsUnusedTypedDict]
    ]
    canceled_subscriptions_other: NotRequired[
        Nullable[CanceledSubscriptionsOtherTypedDict]
    ]
    churned_subscriptions: NotRequired[Nullable[ChurnedSubscriptionsTypedDict]]
    churn_rate: NotRequired[Nullable[ChurnRateTypedDict]]
    ltv: NotRequired[Nullable[LtvTypedDict]]
    gross_margin: NotRequired[Nullable[GrossMarginTypedDict]]
    gross_margin_percentage: NotRequired[Nullable[GrossMarginPercentageTypedDict]]
    cashflow: NotRequired[Nullable[CashflowTypedDict]]


class MetricPeriod(BaseModel):
    timestamp: datetime
    r"""Timestamp of this period data."""

    orders: OptionalNullable[OrdersModel] = UNSET

    revenue: OptionalNullable[Revenue] = UNSET

    net_revenue: OptionalNullable[NetRevenue] = UNSET

    cumulative_revenue: OptionalNullable[CumulativeRevenue] = UNSET

    net_cumulative_revenue: OptionalNullable[NetCumulativeRevenue] = UNSET

    costs: OptionalNullable[Costs] = UNSET

    cumulative_costs: OptionalNullable[CumulativeCosts] = UNSET

    average_order_value: OptionalNullable[AverageOrderValue] = UNSET

    net_average_order_value: OptionalNullable[NetAverageOrderValue] = UNSET

    average_revenue_per_user: OptionalNullable[AverageRevenuePerUser] = UNSET

    cost_per_user: OptionalNullable[CostPerUser] = UNSET

    active_user_by_event: OptionalNullable[ActiveUserByEvent] = UNSET

    one_time_products: OptionalNullable[OneTimeProducts] = UNSET

    one_time_products_revenue: OptionalNullable[OneTimeProductsRevenue] = UNSET

    one_time_products_net_revenue: OptionalNullable[OneTimeProductsNetRevenue] = UNSET

    new_subscriptions: OptionalNullable[NewSubscriptions] = UNSET

    new_subscriptions_revenue: OptionalNullable[NewSubscriptionsRevenue] = UNSET

    new_subscriptions_net_revenue: OptionalNullable[NewSubscriptionsNetRevenue] = UNSET

    renewed_subscriptions: OptionalNullable[RenewedSubscriptions] = UNSET

    renewed_subscriptions_revenue: OptionalNullable[RenewedSubscriptionsRevenue] = UNSET

    renewed_subscriptions_net_revenue: OptionalNullable[
        RenewedSubscriptionsNetRevenue
    ] = UNSET

    active_subscriptions: OptionalNullable[ActiveSubscriptions] = UNSET

    committed_subscriptions: OptionalNullable[CommittedSubscriptions] = UNSET

    monthly_recurring_revenue: OptionalNullable[MonthlyRecurringRevenue] = UNSET

    committed_monthly_recurring_revenue: OptionalNullable[
        CommittedMonthlyRecurringRevenue
    ] = UNSET

    checkouts: OptionalNullable[CheckoutsModel] = UNSET

    succeeded_checkouts: OptionalNullable[SucceededCheckouts] = UNSET

    checkouts_conversion: OptionalNullable[CheckoutsConversion] = UNSET

    canceled_subscriptions: OptionalNullable[CanceledSubscriptions] = UNSET

    canceled_subscriptions_customer_service: OptionalNullable[
        CanceledSubscriptionsCustomerService
    ] = UNSET

    canceled_subscriptions_low_quality: OptionalNullable[
        CanceledSubscriptionsLowQuality
    ] = UNSET

    canceled_subscriptions_missing_features: OptionalNullable[
        CanceledSubscriptionsMissingFeatures
    ] = UNSET

    canceled_subscriptions_switched_service: OptionalNullable[
        CanceledSubscriptionsSwitchedService
    ] = UNSET

    canceled_subscriptions_too_complex: OptionalNullable[
        CanceledSubscriptionsTooComplex
    ] = UNSET

    canceled_subscriptions_too_expensive: OptionalNullable[
        CanceledSubscriptionsTooExpensive
    ] = UNSET

    canceled_subscriptions_unused: OptionalNullable[CanceledSubscriptionsUnused] = UNSET

    canceled_subscriptions_other: OptionalNullable[CanceledSubscriptionsOther] = UNSET

    churned_subscriptions: OptionalNullable[ChurnedSubscriptions] = UNSET

    churn_rate: OptionalNullable[ChurnRate] = UNSET

    ltv: OptionalNullable[Ltv] = UNSET

    gross_margin: OptionalNullable[GrossMargin] = UNSET

    gross_margin_percentage: OptionalNullable[GrossMarginPercentage] = UNSET

    cashflow: OptionalNullable[Cashflow] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "orders",
            "revenue",
            "net_revenue",
            "cumulative_revenue",
            "net_cumulative_revenue",
            "costs",
            "cumulative_costs",
            "average_order_value",
            "net_average_order_value",
            "average_revenue_per_user",
            "cost_per_user",
            "active_user_by_event",
            "one_time_products",
            "one_time_products_revenue",
            "one_time_products_net_revenue",
            "new_subscriptions",
            "new_subscriptions_revenue",
            "new_subscriptions_net_revenue",
            "renewed_subscriptions",
            "renewed_subscriptions_revenue",
            "renewed_subscriptions_net_revenue",
            "active_subscriptions",
            "committed_subscriptions",
            "monthly_recurring_revenue",
            "committed_monthly_recurring_revenue",
            "checkouts",
            "succeeded_checkouts",
            "checkouts_conversion",
            "canceled_subscriptions",
            "canceled_subscriptions_customer_service",
            "canceled_subscriptions_low_quality",
            "canceled_subscriptions_missing_features",
            "canceled_subscriptions_switched_service",
            "canceled_subscriptions_too_complex",
            "canceled_subscriptions_too_expensive",
            "canceled_subscriptions_unused",
            "canceled_subscriptions_other",
            "churned_subscriptions",
            "churn_rate",
            "ltv",
            "gross_margin",
            "gross_margin_percentage",
            "cashflow",
        ]
        nullable_fields = [
            "orders",
            "revenue",
            "net_revenue",
            "cumulative_revenue",
            "net_cumulative_revenue",
            "costs",
            "cumulative_costs",
            "average_order_value",
            "net_average_order_value",
            "average_revenue_per_user",
            "cost_per_user",
            "active_user_by_event",
            "one_time_products",
            "one_time_products_revenue",
            "one_time_products_net_revenue",
            "new_subscriptions",
            "new_subscriptions_revenue",
            "new_subscriptions_net_revenue",
            "renewed_subscriptions",
            "renewed_subscriptions_revenue",
            "renewed_subscriptions_net_revenue",
            "active_subscriptions",
            "committed_subscriptions",
            "monthly_recurring_revenue",
            "committed_monthly_recurring_revenue",
            "checkouts",
            "succeeded_checkouts",
            "checkouts_conversion",
            "canceled_subscriptions",
            "canceled_subscriptions_customer_service",
            "canceled_subscriptions_low_quality",
            "canceled_subscriptions_missing_features",
            "canceled_subscriptions_switched_service",
            "canceled_subscriptions_too_complex",
            "canceled_subscriptions_too_expensive",
            "canceled_subscriptions_unused",
            "canceled_subscriptions_other",
            "churned_subscriptions",
            "churn_rate",
            "ltv",
            "gross_margin",
            "gross_margin_percentage",
            "cashflow",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

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