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


GrantObject = Literal["grant",]


class GrantTypedDict(TypedDict):
    id: str
    r"""Unique identifier for the grant."""
    entitlement_id: str
    r"""The entitlement this grant belongs to."""
    amount: float
    r"""The number of credits granted."""
    effective_at: datetime
    r"""When the grant becomes effective."""
    created_at: datetime
    r"""When the grant was created."""
    object: NotRequired[GrantObject]
    expires_at: NotRequired[Nullable[datetime]]
    r"""When the grant expires. Null means no expiration."""
    voided_at: NotRequired[Nullable[datetime]]
    r"""When the grant was voided. Null means the grant is active."""


class Grant(BaseModel):
    id: str
    r"""Unique identifier for the grant."""

    entitlement_id: Annotated[str, pydantic.Field(alias="entitlementId")]
    r"""The entitlement this grant belongs to."""

    amount: float
    r"""The number of credits granted."""

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

    created_at: Annotated[datetime, pydantic.Field(alias="createdAt")]
    r"""When the grant was created."""

    object: Optional[GrantObject] = "grant"

    expires_at: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="expiresAt")
    ] = UNSET
    r"""When the grant expires. Null means no expiration."""

    voided_at: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="voidedAt")
    ] = UNSET
    r"""When the grant was voided. Null means the grant is active."""

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