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

from __future__ import annotations
from .accesslevel import AccessLevel
from .documentpermissionset import DocumentPermissionSet, DocumentPermissionSetTypedDict
from .processingstatus import ProcessingStatus
from .subject import Subject, SubjectTypedDict
from .versionref import VersionRef, VersionRefTypedDict
from datetime import datetime
from factify.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from pydantic import model_serializer
from typing import Optional
from typing_extensions import NotRequired, TypedDict


class DocumentTypedDict(TypedDict):
    r"""Document represents a Factify document."""

    access_level: AccessLevel
    created_at: datetime
    r"""Timestamp when document 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)

    """
    id: str
    r"""Unique ID for the document.
    Pattern: doc_[0-9a-hjkmnp-tv-z]{26}
    """
    processing_status: ProcessingStatus
    title: str
    r"""Document title."""
    url: str
    r"""URL for accessing the document on Factify."""
    current_version: NotRequired[Nullable[VersionRefTypedDict]]
    r"""Reference to the current version, if any."""
    description: NotRequired[Nullable[str]]
    r"""Optional document description."""
    general_access: NotRequired[AccessLevel]
    is_demo: NotRequired[bool]
    r"""Whether this document is a demo document created during FTUE onboarding."""
    last_viewed_at: NotRequired[datetime]
    r"""Timestamp when the authenticated user last viewed this document.
    Absent if the user has never viewed it.
    """
    owner_organization_id: NotRequired[str]
    r"""Organization that owns this document (via the document creator).
    Empty if the owner's organization could not be resolved.
    """
    permission_set: NotRequired[DocumentPermissionSetTypedDict]
    r"""DocumentPermissionSet contains the permissions the authenticated user has on a document."""
    shared_at: NotRequired[datetime]
    r"""Timestamp when this document was last shared to the authenticated user.
    Absent if the document was never shared to them.
    """
    thumbnail_url: NotRequired[str]
    r"""URL of the document thumbnail image."""
    trashed_at: NotRequired[datetime]
    r"""Timestamp when the document was trashed, if applicable.
    Absent if the document is not trashed.
    """
    updated_at: NotRequired[datetime]
    r"""Timestamp when document was last updated."""


class Document(BaseModel):
    r"""Document represents a Factify document."""

    access_level: AccessLevel

    created_at: datetime
    r"""Timestamp when document 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)

    """

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

    processing_status: ProcessingStatus

    title: str
    r"""Document title."""

    url: str
    r"""URL for accessing the document on Factify."""

    current_version: OptionalNullable[VersionRef] = UNSET
    r"""Reference to the current version, if any."""

    description: OptionalNullable[str] = UNSET
    r"""Optional document description."""

    general_access: Optional[AccessLevel] = None

    is_demo: Optional[bool] = None
    r"""Whether this document is a demo document created during FTUE onboarding."""

    last_viewed_at: Optional[datetime] = None
    r"""Timestamp when the authenticated user last viewed this document.
    Absent if the user has never viewed it.
    """

    owner_organization_id: Optional[str] = None
    r"""Organization that owns this document (via the document creator).
    Empty if the owner's organization could not be resolved.
    """

    permission_set: Optional[DocumentPermissionSet] = None
    r"""DocumentPermissionSet contains the permissions the authenticated user has on a document."""

    shared_at: Optional[datetime] = None
    r"""Timestamp when this document was last shared to the authenticated user.
    Absent if the document was never shared to them.
    """

    thumbnail_url: Optional[str] = None
    r"""URL of the document thumbnail image."""

    trashed_at: Optional[datetime] = None
    r"""Timestamp when the document was trashed, if applicable.
    Absent if the document is not trashed.
    """

    updated_at: Optional[datetime] = None
    r"""Timestamp when document was last updated."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "current_version",
                "description",
                "general_access",
                "is_demo",
                "last_viewed_at",
                "owner_organization_id",
                "permission_set",
                "shared_at",
                "thumbnail_url",
                "trashed_at",
                "updated_at",
            ]
        )
        nullable_fields = set(["current_version", "description"])
        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
