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

from __future__ import annotations
from .entitlementtemplate import EntitlementTemplate, EntitlementTemplateTypedDict
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 Dict, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class IssueEntitlementRequestTypedDict(TypedDict):
    customer_id: str
    r"""Unique identifier for a customer"""
    feature_id: str
    r"""The feature to grant access to."""
    template: EntitlementTemplateTypedDict
    r"""Template for the entitlement. Boolean for simple on/off features, static for features with configuration, metered for usage-based features."""
    active_from: NotRequired[datetime]
    r"""When the entitlement becomes active. Defaults to now."""
    active_to: NotRequired[Nullable[datetime]]
    r"""When the entitlement expires. Null means no expiration."""
    subscription_id: NotRequired[Nullable[str]]
    r"""Optional subscription ID to associate with this entitlement."""
    metadata: NotRequired[Dict[str, str]]
    r"""Additional metadata for the entitlement."""


class IssueEntitlementRequest(BaseModel):
    customer_id: Annotated[str, pydantic.Field(alias="customerId")]
    r"""Unique identifier for a customer"""

    feature_id: Annotated[str, pydantic.Field(alias="featureId")]
    r"""The feature to grant access to."""

    template: EntitlementTemplate
    r"""Template for the entitlement. Boolean for simple on/off features, static for features with configuration, metered for usage-based features."""

    active_from: Annotated[Optional[datetime], pydantic.Field(alias="activeFrom")] = (
        None
    )
    r"""When the entitlement becomes active. Defaults to now."""

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

    subscription_id: Annotated[
        OptionalNullable[str], pydantic.Field(alias="subscriptionId")
    ] = UNSET
    r"""Optional subscription ID to associate with this entitlement."""

    metadata: Optional[Dict[str, str]] = None
    r"""Additional metadata for the entitlement."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["activeFrom", "activeTo", "subscriptionId", "metadata"])
        nullable_fields = set(["activeTo", "subscriptionId"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            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:
    IssueEntitlementRequest.model_rebuild()
except NameError:
    pass
