"""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 Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class AuditType(str, Enum, metaclass=utils.OpenEnumMeta):
    r"""The type of audit that was performed on the investigation"""

    AUDIT_TYPE_UNSPECIFIED = "AUDIT_TYPE_UNSPECIFIED"
    INVESTIGATION_REQUEST_UPDATE = "INVESTIGATION_REQUEST_UPDATE"
    INVESTIGATION_STATE = "INVESTIGATION_STATE"
    COMMENT = "COMMENT"


class AuditTrailTypedDict(TypedDict):
    r"""Audit trail details"""

    audit_type: NotRequired[AuditType]
    r"""The type of audit that was performed on the investigation"""
    comment: NotRequired[str]
    r"""Comment relating to why the audit was saved"""
    field: NotRequired[str]
    r"""The name of the field that has been updated"""
    new_value: NotRequired[str]
    r"""The new value for the field that was updated"""
    old_value: NotRequired[str]
    r"""The prior value for the field that was updated"""
    update_time: NotRequired[Nullable[datetime]]
    r"""The date the user updated the investigation"""
    update_user: NotRequired[str]
    r"""The user that made the update to the investigation"""


class AuditTrail(BaseModel):
    r"""Audit trail details"""

    audit_type: Annotated[
        Optional[AuditType], PlainValidator(validate_open_enum(False))
    ] = None
    r"""The type of audit that was performed on the investigation"""

    comment: Optional[str] = None
    r"""Comment relating to why the audit was saved"""

    field: Optional[str] = None
    r"""The name of the field that has been updated"""

    new_value: Optional[str] = None
    r"""The new value for the field that was updated"""

    old_value: Optional[str] = None
    r"""The prior value for the field that was updated"""

    update_time: OptionalNullable[datetime] = UNSET
    r"""The date the user updated the investigation"""

    update_user: Optional[str] = None
    r"""The user that made the update to the investigation"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "audit_type",
            "comment",
            "field",
            "new_value",
            "old_value",
            "update_time",
            "update_user",
        ]
        nullable_fields = ["update_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
