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

from __future__ import annotations
from .billingcadence import BillingCadence
from .billingcycle import BillingCycle
from .billingperiod import BillingPeriod
from .dto_couponassociationresponse import (
    DtoCouponAssociationResponse,
    DtoCouponAssociationResponseTypedDict,
)
from .dto_creditgrantresponse import (
    DtoCreditGrantResponse,
    DtoCreditGrantResponseTypedDict,
)
from .dto_customerresponse import DtoCustomerResponse, DtoCustomerResponseTypedDict
from .dto_planresponse import DtoPlanResponse, DtoPlanResponseTypedDict
from .dto_subscriptionphaseresponse import (
    DtoSubscriptionPhaseResponse,
    DtoSubscriptionPhaseResponseTypedDict,
)
from .pausestatus import PauseStatus
from .paymentterms import PaymentTerms
from .prorationbehavior import ProrationBehavior
from .status import Status
from .subscription_subscriptionlineitem import (
    SubscriptionSubscriptionLineItem,
    SubscriptionSubscriptionLineItemTypedDict,
)
from .subscription_subscriptionpause import (
    SubscriptionSubscriptionPause,
    SubscriptionSubscriptionPauseTypedDict,
)
from .subscriptionstatus import SubscriptionStatus
from flexprice.types import BaseModel, UNSET_SENTINEL
from pydantic import model_serializer
from typing import Dict, List, Optional, TYPE_CHECKING
from typing_extensions import NotRequired, TypedDict

if TYPE_CHECKING:
    from .dto_invoiceresponse import DtoInvoiceResponse, DtoInvoiceResponseTypedDict


class DtoSubscriptionResponseTypedDict(TypedDict):
    active_pause_id: NotRequired[str]
    r"""ActivePauseID references the current active pause configuration
    This will be null if no pause is active or scheduled
    """
    billing_anchor: NotRequired[str]
    r"""BillingAnchor is the reference point that aligns future billing cycle dates.
    It sets the day of week for week intervals, the day of month for month and year intervals,
    and the month of year for year intervals. The timestamp is in UTC format.
    """
    billing_cadence: NotRequired[BillingCadence]
    billing_cycle: NotRequired[BillingCycle]
    billing_period: NotRequired[BillingPeriod]
    billing_period_count: NotRequired[int]
    r"""BillingPeriodCount is the total number units of the billing period."""
    cancel_at: NotRequired[str]
    r"""CancelAt is the date the subscription will be canceled"""
    cancel_at_period_end: NotRequired[bool]
    r"""CancelAtPeriodEnd is whether the subscription was canceled at the end of the current period"""
    cancelled_at: NotRequired[str]
    r"""CanceledAt is the date the subscription was canceled"""
    collection_method: NotRequired[str]
    r"""CollectionMethod determines how invoices are collected"""
    commitment_amount: NotRequired[str]
    r"""CommitmentAmount is the minimum amount a customer commits to paying for a billing period"""
    commitment_duration: NotRequired[BillingPeriod]
    coupon_associations: NotRequired[List[DtoCouponAssociationResponseTypedDict]]
    r"""CouponAssociations are the coupon associations for this subscription"""
    created_at: NotRequired[str]
    created_by: NotRequired[str]
    credit_grants: NotRequired[List[DtoCreditGrantResponseTypedDict]]
    r"""Credit grants are the credit grants for this subscription"""
    currency: NotRequired[str]
    r"""Currency is the currency of the subscription in lowercase 3 digit ISO codes"""
    current_period_end: NotRequired[str]
    r"""CurrentPeriodEnd is the end of the current period that the subscription has been invoiced for.
    At the end of this period, a new invoice will be created.
    """
    current_period_start: NotRequired[str]
    r"""CurrentPeriodStart is the end of the current period that the subscription has been invoiced for.
    At the end of this period, a new invoice will be created.
    """
    customer: NotRequired[DtoCustomerResponseTypedDict]
    r"""Customer response object containing all customer information"""
    customer_id: NotRequired[str]
    r"""CustomerID is the identifier for the customer in our system"""
    customer_timezone: NotRequired[str]
    enable_true_up: NotRequired[bool]
    end_date: NotRequired[str]
    r"""EndDate is the end date of the subscription"""
    environment_id: NotRequired[str]
    r"""EnvironmentID is the environment identifier for the subscription"""
    gateway_payment_method_id: NotRequired[str]
    r"""GatewayPaymentMethodID is the gateway payment method ID for this subscription"""
    id: NotRequired[str]
    r"""ID is the unique identifier for the subscription"""
    invoicing_customer_id: NotRequired[str]
    r"""InvoicingCustomerID is the customer ID to use for invoicing
    This can differ from the subscription customer (e.g., parent company invoicing for child company)
    """
    latest_invoice: NotRequired["DtoInvoiceResponseTypedDict"]
    line_items: NotRequired[List[SubscriptionSubscriptionLineItemTypedDict]]
    lookup_key: NotRequired[str]
    r"""LookupKey is the key used to lookup the subscription in our system"""
    metadata: NotRequired[Dict[str, str]]
    overage_factor: NotRequired[str]
    r"""OverageFactor is a multiplier applied to usage beyond the commitment amount"""
    parent_subscription_id: NotRequired[str]
    r"""ParentSubscriptionID is the parent subscription ID for hierarchy (e.g. child subscription under a parent)"""
    pause_status: NotRequired[PauseStatus]
    pauses: NotRequired[List[SubscriptionSubscriptionPauseTypedDict]]
    payment_behavior: NotRequired[str]
    r"""PaymentBehavior determines how subscription payments are handled"""
    payment_terms: NotRequired[PaymentTerms]
    phases: NotRequired[List[DtoSubscriptionPhaseResponseTypedDict]]
    r"""Phases are the subscription phases for this subscription"""
    plan: NotRequired[DtoPlanResponseTypedDict]
    plan_id: NotRequired[str]
    r"""PlanID is the identifier for the plan in our system"""
    proration_behavior: NotRequired[ProrationBehavior]
    start_date: NotRequired[str]
    r"""StartDate is the start date of the subscription"""
    status: NotRequired[Status]
    subscription_status: NotRequired[SubscriptionStatus]
    tenant_id: NotRequired[str]
    trial_end: NotRequired[str]
    r"""TrialEnd is the end date of the trial period"""
    trial_start: NotRequired[str]
    r"""TrialStart is the start date of the trial period"""
    updated_at: NotRequired[str]
    updated_by: NotRequired[str]
    version: NotRequired[int]
    r"""Version is used for optimistic locking"""


class DtoSubscriptionResponse(BaseModel):
    active_pause_id: Optional[str] = None
    r"""ActivePauseID references the current active pause configuration
    This will be null if no pause is active or scheduled
    """

    billing_anchor: Optional[str] = None
    r"""BillingAnchor is the reference point that aligns future billing cycle dates.
    It sets the day of week for week intervals, the day of month for month and year intervals,
    and the month of year for year intervals. The timestamp is in UTC format.
    """

    billing_cadence: Optional[BillingCadence] = None

    billing_cycle: Optional[BillingCycle] = None

    billing_period: Optional[BillingPeriod] = None

    billing_period_count: Optional[int] = None
    r"""BillingPeriodCount is the total number units of the billing period."""

    cancel_at: Optional[str] = None
    r"""CancelAt is the date the subscription will be canceled"""

    cancel_at_period_end: Optional[bool] = None
    r"""CancelAtPeriodEnd is whether the subscription was canceled at the end of the current period"""

    cancelled_at: Optional[str] = None
    r"""CanceledAt is the date the subscription was canceled"""

    collection_method: Optional[str] = None
    r"""CollectionMethod determines how invoices are collected"""

    commitment_amount: Optional[str] = None
    r"""CommitmentAmount is the minimum amount a customer commits to paying for a billing period"""

    commitment_duration: Optional[BillingPeriod] = None

    coupon_associations: Optional[List[DtoCouponAssociationResponse]] = None
    r"""CouponAssociations are the coupon associations for this subscription"""

    created_at: Optional[str] = None

    created_by: Optional[str] = None

    credit_grants: Optional[List[DtoCreditGrantResponse]] = None
    r"""Credit grants are the credit grants for this subscription"""

    currency: Optional[str] = None
    r"""Currency is the currency of the subscription in lowercase 3 digit ISO codes"""

    current_period_end: Optional[str] = None
    r"""CurrentPeriodEnd is the end of the current period that the subscription has been invoiced for.
    At the end of this period, a new invoice will be created.
    """

    current_period_start: Optional[str] = None
    r"""CurrentPeriodStart is the end of the current period that the subscription has been invoiced for.
    At the end of this period, a new invoice will be created.
    """

    customer: Optional[DtoCustomerResponse] = None
    r"""Customer response object containing all customer information"""

    customer_id: Optional[str] = None
    r"""CustomerID is the identifier for the customer in our system"""

    customer_timezone: Optional[str] = None

    enable_true_up: Optional[bool] = None

    end_date: Optional[str] = None
    r"""EndDate is the end date of the subscription"""

    environment_id: Optional[str] = None
    r"""EnvironmentID is the environment identifier for the subscription"""

    gateway_payment_method_id: Optional[str] = None
    r"""GatewayPaymentMethodID is the gateway payment method ID for this subscription"""

    id: Optional[str] = None
    r"""ID is the unique identifier for the subscription"""

    invoicing_customer_id: Optional[str] = None
    r"""InvoicingCustomerID is the customer ID to use for invoicing
    This can differ from the subscription customer (e.g., parent company invoicing for child company)
    """

    latest_invoice: Optional["DtoInvoiceResponse"] = None

    line_items: Optional[List[SubscriptionSubscriptionLineItem]] = None

    lookup_key: Optional[str] = None
    r"""LookupKey is the key used to lookup the subscription in our system"""

    metadata: Optional[Dict[str, str]] = None

    overage_factor: Optional[str] = None
    r"""OverageFactor is a multiplier applied to usage beyond the commitment amount"""

    parent_subscription_id: Optional[str] = None
    r"""ParentSubscriptionID is the parent subscription ID for hierarchy (e.g. child subscription under a parent)"""

    pause_status: Optional[PauseStatus] = None

    pauses: Optional[List[SubscriptionSubscriptionPause]] = None

    payment_behavior: Optional[str] = None
    r"""PaymentBehavior determines how subscription payments are handled"""

    payment_terms: Optional[PaymentTerms] = None

    phases: Optional[List[DtoSubscriptionPhaseResponse]] = None
    r"""Phases are the subscription phases for this subscription"""

    plan: Optional[DtoPlanResponse] = None

    plan_id: Optional[str] = None
    r"""PlanID is the identifier for the plan in our system"""

    proration_behavior: Optional[ProrationBehavior] = None

    start_date: Optional[str] = None
    r"""StartDate is the start date of the subscription"""

    status: Optional[Status] = None

    subscription_status: Optional[SubscriptionStatus] = None

    tenant_id: Optional[str] = None

    trial_end: Optional[str] = None
    r"""TrialEnd is the end date of the trial period"""

    trial_start: Optional[str] = None
    r"""TrialStart is the start date of the trial period"""

    updated_at: Optional[str] = None

    updated_by: Optional[str] = None

    version: Optional[int] = None
    r"""Version is used for optimistic locking"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "active_pause_id",
                "billing_anchor",
                "billing_cadence",
                "billing_cycle",
                "billing_period",
                "billing_period_count",
                "cancel_at",
                "cancel_at_period_end",
                "cancelled_at",
                "collection_method",
                "commitment_amount",
                "commitment_duration",
                "coupon_associations",
                "created_at",
                "created_by",
                "credit_grants",
                "currency",
                "current_period_end",
                "current_period_start",
                "customer",
                "customer_id",
                "customer_timezone",
                "enable_true_up",
                "end_date",
                "environment_id",
                "gateway_payment_method_id",
                "id",
                "invoicing_customer_id",
                "latest_invoice",
                "line_items",
                "lookup_key",
                "metadata",
                "overage_factor",
                "parent_subscription_id",
                "pause_status",
                "pauses",
                "payment_behavior",
                "payment_terms",
                "phases",
                "plan",
                "plan_id",
                "proration_behavior",
                "start_date",
                "status",
                "subscription_status",
                "tenant_id",
                "trial_end",
                "trial_start",
                "updated_at",
                "updated_by",
                "version",
            ]
        )
        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
