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

from __future__ import annotations
from datetime import datetime
from paygentic_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
import pydantic
from pydantic import model_serializer
from typing import List, Literal, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


PurchaseGrantResponseObject = Literal["grant_purchase",]


class PaymentSessionTypedDict(TypedDict):
    url: str
    r"""The Stripe checkout URL for the customer to complete payment."""
    expires_at: NotRequired[Nullable[datetime]]
    r"""When the payment session expires."""
    amount: NotRequired[float]
    r"""The payment amount in the currency's minor unit (e.g., cents)."""


class PaymentSession(BaseModel):
    url: str
    r"""The Stripe checkout URL for the customer to complete payment."""

    expires_at: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="expiresAt")
    ] = UNSET
    r"""When the payment session expires."""

    amount: Optional[float] = None
    r"""The payment amount in the currency's minor unit (e.g., cents)."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["expiresAt", "amount"])
        nullable_fields = set(["expiresAt"])
        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


class PurchaseGrantResponseTypedDict(TypedDict):
    invoice_id: str
    r"""The invoice ID for this grant purchase."""
    entitlement_id: str
    r"""The entitlement that will receive the grant."""
    grant_amount: int
    r"""The number of credits that will be granted on payment."""
    price: str
    r"""The price in decimal format (e.g., '5.00')."""
    currency: str
    r"""The three-letter ISO 4217 currency code (e.g., 'usd')."""
    payment_sessions: List[PaymentSessionTypedDict]
    r"""Payment session(s) for the customer to complete the purchase."""
    object: NotRequired[PurchaseGrantResponseObject]


class PurchaseGrantResponse(BaseModel):
    invoice_id: Annotated[str, pydantic.Field(alias="invoiceId")]
    r"""The invoice ID for this grant purchase."""

    entitlement_id: Annotated[str, pydantic.Field(alias="entitlementId")]
    r"""The entitlement that will receive the grant."""

    grant_amount: Annotated[int, pydantic.Field(alias="grantAmount")]
    r"""The number of credits that will be granted on payment."""

    price: str
    r"""The price in decimal format (e.g., '5.00')."""

    currency: str
    r"""The three-letter ISO 4217 currency code (e.g., 'usd')."""

    payment_sessions: Annotated[
        List[PaymentSession], pydantic.Field(alias="paymentSessions")
    ]
    r"""Payment session(s) for the customer to complete the purchase."""

    object: Optional[PurchaseGrantResponseObject] = "grant_purchase"

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


try:
    PaymentSession.model_rebuild()
except NameError:
    pass
try:
    PurchaseGrantResponse.model_rebuild()
except NameError:
    pass
