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

from __future__ import annotations
from .directoryextension import DirectoryExtension, DirectoryExtensionTypedDict
from datetime import datetime
from pydantic import model_serializer
from syllable_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from typing import Dict, List, Optional
from typing_extensions import NotRequired, TypedDict


class DirectoryMemberHistoryResponseTypedDict(TypedDict):
    r"""API response for one version in a directory member's history (GET /{member_id}/history)."""

    directory_member_id: int
    r"""ID of the directory member"""
    version: int
    r"""1-based ordinal in the history list (1 = oldest)"""
    timestamp: datetime
    r"""When this version was recorded"""
    name: str
    r"""Name at this version"""
    type: str
    r"""Type at this version"""
    user_email: str
    r"""User who made the change"""
    extensions: NotRequired[List[DirectoryExtensionTypedDict]]
    r"""Extensions at this version"""
    contact_tags: NotRequired[Dict[str, List[str]]]
    r"""Contact tags at this version"""
    comments: NotRequired[Nullable[str]]
    r"""Optional comment for this version"""
    operation: NotRequired[Nullable[str]]
    r"""What produced this row: create, update, delete, restore, bulk_load. Null for rows created before this field existed."""


class DirectoryMemberHistoryResponse(BaseModel):
    r"""API response for one version in a directory member's history (GET /{member_id}/history)."""

    directory_member_id: int
    r"""ID of the directory member"""

    version: int
    r"""1-based ordinal in the history list (1 = oldest)"""

    timestamp: datetime
    r"""When this version was recorded"""

    name: str
    r"""Name at this version"""

    type: str
    r"""Type at this version"""

    user_email: str
    r"""User who made the change"""

    extensions: Optional[List[DirectoryExtension]] = None
    r"""Extensions at this version"""

    contact_tags: Optional[Dict[str, List[str]]] = None
    r"""Contact tags at this version"""

    comments: OptionalNullable[str] = UNSET
    r"""Optional comment for this version"""

    operation: OptionalNullable[str] = UNSET
    r"""What produced this row: create, update, delete, restore, bulk_load. Null for rows created before this field existed."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["extensions", "contact_tags", "comments", "operation"])
        nullable_fields = set(["comments", "operation"])
        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
