"""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 Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class PurchaseGrantRequestTypedDict(TypedDict):
    amount: float
    r"""The number of credits to grant upon payment completion."""
    price: str
    r"""The price in decimal format (e.g., '5.00' for $5.00 USD). Must be at least $0.50."""
    idempotency_key: str
    r"""Caller-provided deduplication key. Retrying with the same key returns the existing invoice."""
    effective_at: NotRequired[datetime]
    r"""When the grant becomes effective. Defaults to now."""
    expires_at: NotRequired[Nullable[datetime]]
    r"""When the grant expires. If omitted, the grant does not expire."""
    success_url: NotRequired[str]
    r"""URL to redirect the customer to after successful payment."""
    cancel_url: NotRequired[str]
    r"""URL to redirect the customer to if payment is cancelled."""
    payment_expires_at: NotRequired[datetime]
    r"""When the payment session expires. If omitted, uses the default expiry."""


class PurchaseGrantRequest(BaseModel):
    amount: float
    r"""The number of credits to grant upon payment completion."""

    price: str
    r"""The price in decimal format (e.g., '5.00' for $5.00 USD). Must be at least $0.50."""

    idempotency_key: Annotated[str, pydantic.Field(alias="idempotencyKey")]
    r"""Caller-provided deduplication key. Retrying with the same key returns the existing invoice."""

    effective_at: Annotated[Optional[datetime], pydantic.Field(alias="effectiveAt")] = (
        None
    )
    r"""When the grant becomes effective. Defaults to now."""

    expires_at: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="expiresAt")
    ] = UNSET
    r"""When the grant expires. If omitted, the grant does not expire."""

    success_url: Annotated[Optional[str], pydantic.Field(alias="successUrl")] = None
    r"""URL to redirect the customer to after successful payment."""

    cancel_url: Annotated[Optional[str], pydantic.Field(alias="cancelUrl")] = None
    r"""URL to redirect the customer to if payment is cancelled."""

    payment_expires_at: Annotated[
        Optional[datetime], pydantic.Field(alias="paymentExpiresAt")
    ] = None
    r"""When the payment session expires. If omitted, uses the default expiry."""

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


try:
    PurchaseGrantRequest.model_rebuild()
except NameError:
    pass
