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

from __future__ import annotations
from ascend_sdk import utils
from ascend_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from ascend_sdk.utils import validate_open_enum
from datetime import datetime
from enum import Enum
from pydantic import model_serializer
from pydantic.functional_validators import PlainValidator
from typing import List, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class EntitlementState(str, Enum, metaclass=utils.OpenEnumMeta):
    r"""GRANTED if it was activated via an enrollment, SUSPENDED if a restriction has temporarily disabled it, or DENIED if it has never before been GRANTED"""

    ENTITLEMENT_STATE_UNSPECIFIED = "ENTITLEMENT_STATE_UNSPECIFIED"
    GRANTED = "GRANTED"
    SUSPENDED = "SUSPENDED"
    DENIED = "DENIED"


class EntitlementTypedDict(TypedDict):
    r"""An Entitlement for an Account. Defines what the account is allowed to do."""

    display_name: NotRequired[str]
    r"""A friendly name for the entitlement used primarily for UI display"""
    entitlement_code: NotRequired[str]
    r"""The enum value of the entitlement being described; e.g., BANKING_CHECK"""
    last_modified_reason: NotRequired[str]
    r"""A system-generated reason about why the last modification occurred - typically due to an enrollment or restriction event"""
    last_modified_time: NotRequired[Nullable[datetime]]
    r"""The last time the underlying entitlement had its status updated"""
    parent_entitlements: NotRequired[List[str]]
    r"""The entitlement above the subject; A model of status inheritance exists which provides context on this value"""
    state: NotRequired[EntitlementState]
    r"""GRANTED if it was activated via an enrollment, SUSPENDED if a restriction has temporarily disabled it, or DENIED if it has never before been GRANTED"""


class Entitlement(BaseModel):
    r"""An Entitlement for an Account. Defines what the account is allowed to do."""

    display_name: Optional[str] = None
    r"""A friendly name for the entitlement used primarily for UI display"""

    entitlement_code: Optional[str] = None
    r"""The enum value of the entitlement being described; e.g., BANKING_CHECK"""

    last_modified_reason: Optional[str] = None
    r"""A system-generated reason about why the last modification occurred - typically due to an enrollment or restriction event"""

    last_modified_time: OptionalNullable[datetime] = UNSET
    r"""The last time the underlying entitlement had its status updated"""

    parent_entitlements: Optional[List[str]] = None
    r"""The entitlement above the subject; A model of status inheritance exists which provides context on this value"""

    state: Annotated[
        Optional[EntitlementState], PlainValidator(validate_open_enum(False))
    ] = None
    r"""GRANTED if it was activated via an enrollment, SUSPENDED if a restriction has temporarily disabled it, or DENIED if it has never before been GRANTED"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "display_name",
            "entitlement_code",
            "last_modified_reason",
            "last_modified_time",
            "parent_entitlements",
            "state",
        ]
        nullable_fields = ["last_modified_time"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in self.model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
