"""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 CreateGrantRequestTypedDict(TypedDict):
    amount: float
    r"""The number of credits to grant."""
    idempotency_key: str
    r"""Idempotency key to prevent duplicate grants. Must be unique per entitlement."""
    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."""


class CreateGrantRequest(BaseModel):
    amount: float
    r"""The number of credits to grant."""

    idempotency_key: Annotated[str, pydantic.Field(alias="idempotencyKey")]
    r"""Idempotency key to prevent duplicate grants. Must be unique per entitlement."""

    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."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["effectiveAt", "expiresAt"])
        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:
    CreateGrantRequest.model_rebuild()
except NameError:
    pass
