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

from __future__ import annotations
from .processingstatus import ProcessingStatus
from .subject import Subject, SubjectTypedDict
from datetime import datetime
from factify.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from pydantic import model_serializer
from typing_extensions import NotRequired, TypedDict


class VersionTypedDict(TypedDict):
    r"""Version represents a document version."""

    created_at: datetime
    r"""Timestamp when version was created."""
    created_by: SubjectTypedDict
    r"""Subject is a lightweight reference to either a user or a bot.
    Used in embedded fields like created_by.
    For full details, call GetUser or GetBot with the subject's id.
    subject_type_matches_id_prefix // id prefix must match type (user_ for user, bot_ for bot)

    """
    document_id: str
    r"""Parent document ID.
    Pattern: doc_[0-9a-hjkmnp-tv-z]{26}
    """
    id: str
    r"""Unique ID for the version.
    Pattern: ver_[0-9a-hjkmnp-tv-z]{26}
    """
    processing_status: ProcessingStatus
    url: str
    r"""URL for accessing this version on Factify."""
    description: NotRequired[Nullable[str]]
    r"""Description of changes in this version."""
    title: NotRequired[Nullable[str]]
    r"""Optional version title."""


class Version(BaseModel):
    r"""Version represents a document version."""

    created_at: datetime
    r"""Timestamp when version was created."""

    created_by: Subject
    r"""Subject is a lightweight reference to either a user or a bot.
    Used in embedded fields like created_by.
    For full details, call GetUser or GetBot with the subject's id.
    subject_type_matches_id_prefix // id prefix must match type (user_ for user, bot_ for bot)

    """

    document_id: str
    r"""Parent document ID.
    Pattern: doc_[0-9a-hjkmnp-tv-z]{26}
    """

    id: str
    r"""Unique ID for the version.
    Pattern: ver_[0-9a-hjkmnp-tv-z]{26}
    """

    processing_status: ProcessingStatus

    url: str
    r"""URL for accessing this version on Factify."""

    description: OptionalNullable[str] = UNSET
    r"""Description of changes in this version."""

    title: OptionalNullable[str] = UNSET
    r"""Optional version title."""

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