"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
# @generated-id: 867f152a4179

from __future__ import annotations
from .balance_card_audience import BalanceCardAudience
from .balance_card_issuer import BalanceCardIssuer
from .balance_card_region import BalanceCardRegion
from .balance_fee_type import BalanceFeeType
from .balance_prepayment_part_type import BalancePrepaymentPartType
from .balance_transaction_type import BalanceTransactionType
from .payment_method import PaymentMethod
from .sub_totals import SubTotals, SubTotalsTypedDict
from mollie import models
from mollie.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
import pydantic
from pydantic import field_serializer, model_serializer
from typing import List, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class ComponentsSubTotalsTypedDict(TypedDict):
    sub_totals: NotRequired[Nullable[List[SubTotalsTypedDict]]]
    count: NotRequired[int]
    r"""Number of transactions of this type"""
    method: NotRequired[Nullable[PaymentMethod]]
    r"""The payment method, if applicable"""
    card_issuer: NotRequired[BalanceCardIssuer]
    card_audience: NotRequired[BalanceCardAudience]
    card_region: NotRequired[BalanceCardRegion]
    fee_type: NotRequired[BalanceFeeType]
    prepayment_part_type: NotRequired[BalancePrepaymentPartType]
    transaction_type: NotRequired[BalanceTransactionType]


class ComponentsSubTotals(BaseModel):
    sub_totals: Annotated[
        OptionalNullable[List[SubTotals]], pydantic.Field(alias="sub-totals")
    ] = UNSET

    count: Optional[int] = None
    r"""Number of transactions of this type"""

    method: OptionalNullable[PaymentMethod] = UNSET
    r"""The payment method, if applicable"""

    card_issuer: Annotated[
        Optional[BalanceCardIssuer], pydantic.Field(alias="cardIssuer")
    ] = None

    card_audience: Annotated[
        Optional[BalanceCardAudience], pydantic.Field(alias="cardAudience")
    ] = None

    card_region: Annotated[
        Optional[BalanceCardRegion], pydantic.Field(alias="cardRegion")
    ] = None

    fee_type: Annotated[Optional[BalanceFeeType], pydantic.Field(alias="feeType")] = (
        None
    )

    prepayment_part_type: Annotated[
        Optional[BalancePrepaymentPartType], pydantic.Field(alias="prepaymentPartType")
    ] = None

    transaction_type: Annotated[
        Optional[BalanceTransactionType], pydantic.Field(alias="transactionType")
    ] = None

    @field_serializer("method")
    def serialize_method(self, value):
        if isinstance(value, str):
            try:
                return models.PaymentMethod(value)
            except ValueError:
                return value
        return value

    @field_serializer("card_issuer")
    def serialize_card_issuer(self, value):
        if isinstance(value, str):
            try:
                return models.BalanceCardIssuer(value)
            except ValueError:
                return value
        return value

    @field_serializer("card_audience")
    def serialize_card_audience(self, value):
        if isinstance(value, str):
            try:
                return models.BalanceCardAudience(value)
            except ValueError:
                return value
        return value

    @field_serializer("card_region")
    def serialize_card_region(self, value):
        if isinstance(value, str):
            try:
                return models.BalanceCardRegion(value)
            except ValueError:
                return value
        return value

    @field_serializer("fee_type")
    def serialize_fee_type(self, value):
        if isinstance(value, str):
            try:
                return models.BalanceFeeType(value)
            except ValueError:
                return value
        return value

    @field_serializer("prepayment_part_type")
    def serialize_prepayment_part_type(self, value):
        if isinstance(value, str):
            try:
                return models.BalancePrepaymentPartType(value)
            except ValueError:
                return value
        return value

    @field_serializer("transaction_type")
    def serialize_transaction_type(self, value):
        if isinstance(value, str):
            try:
                return models.BalanceTransactionType(value)
            except ValueError:
                return value
        return value

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "sub-totals",
                "count",
                "method",
                "cardIssuer",
                "cardAudience",
                "cardRegion",
                "feeType",
                "prepaymentPartType",
                "transactionType",
            ]
        )
        nullable_fields = set(["sub-totals", "method"])
        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))
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

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

        return m


try:
    ComponentsSubTotals.model_rebuild()
except NameError:
    pass
