# 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, List, Optional, Union
from workos._types import _raise_deserialize_error
from workos._types import _format_datetime, _parse_datetime

from .audit_log_event_actor import AuditLogEventActor
from .audit_log_event_context import AuditLogEventContext
from .audit_log_event_target import AuditLogEventTarget


@dataclass(slots=True)
class AuditLogEvent:
    """Audit Log Event model."""

    action: str
    """Identifier of what happened."""
    occurred_at: datetime
    """ISO-8601 value of when the action occurred."""
    actor: "AuditLogEventActor"
    """The entity that performed the action."""
    targets: List["AuditLogEventTarget"]
    """The resources affected by the action."""
    context: "AuditLogEventContext"
    """Additional context about where and how the action occurred."""
    metadata: Optional[Dict[str, Union[str, float, bool]]] = None
    """Additional data associated with the event or entity."""
    version: Optional[int] = None
    """What schema version the event is associated with."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "AuditLogEvent":
        """Deserialize from a dictionary."""
        try:
            return cls(
                action=data["action"],
                occurred_at=_parse_datetime(data["occurred_at"]),
                actor=AuditLogEventActor.from_dict(cast(Dict[str, Any], data["actor"])),
                targets=[
                    AuditLogEventTarget.from_dict(cast(Dict[str, Any], item))
                    for item in cast(list[Any], data["targets"])
                ],
                context=AuditLogEventContext.from_dict(
                    cast(Dict[str, Any], data["context"])
                ),
                metadata=data.get("metadata"),
                version=data.get("version"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("AuditLogEvent", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["action"] = self.action
        result["occurred_at"] = _format_datetime(self.occurred_at)
        result["actor"] = self.actor.to_dict()
        result["targets"] = [item.to_dict() for item in self.targets]
        result["context"] = self.context.to_dict()
        if self.metadata is not None:
            result["metadata"] = self.metadata
        if self.version is not None:
            result["version"] = self.version
        return result
