# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from typing import cast
from typing import Any, Dict, Optional
from workos._types import _raise_deserialize_error
from workos._types import _format_datetime, _parse_datetime

from workos.common.models.actor import Actor


@dataclass(slots=True)
class ObjectMetadata:
    """Metadata for a stored encrypted object."""

    context: Dict[str, str]
    """Map of values used to determine the encryption key."""
    environment_id: str
    """Environment the object belongs to."""
    id: str
    """Unique identifier of the object."""
    key_id: str
    """Encryption key identifier."""
    updated_at: datetime
    """Timestamp of the last update."""
    updated_by: "Actor"
    version_id: Optional[str] = None
    """Current version identifier of the object."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "ObjectMetadata":
        """Deserialize from a dictionary."""
        try:
            return cls(
                context=data["context"],
                environment_id=data["environment_id"],
                id=data["id"],
                key_id=data["key_id"],
                updated_at=_parse_datetime(data["updated_at"]),
                updated_by=Actor.from_dict(cast(Dict[str, Any], data["updated_by"])),
                version_id=data.get("version_id"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("ObjectMetadata", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["context"] = self.context
        result["environment_id"] = self.environment_id
        result["id"] = self.id
        result["key_id"] = self.key_id
        result["updated_at"] = _format_datetime(self.updated_at)
        result["updated_by"] = self.updated_by.to_dict()
        if self.version_id is not None:
            result["version_id"] = self.version_id
        else:
            result["version_id"] = None
        return result
