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

from __future__ import annotations

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

from .dsync_activated_data_domain import DsyncActivatedDataDomain
from .dsync_activated_data_state import DsyncActivatedDataState
from .dsync_activated_data_type import DsyncActivatedDataType


@dataclass(slots=True)
class DsyncActivatedData:
    """The event payload."""

    object: Literal["directory"]
    """Distinguishes the directory object."""
    id: str
    """Unique identifier of the directory."""
    type: "DsyncActivatedDataType"
    """The type of the directory."""
    state: "DsyncActivatedDataState"
    """The current state of the directory."""
    name: str
    """The name of the directory."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    external_key: str
    """The external key of the directory."""
    domains: List["DsyncActivatedDataDomain"]
    """The domains associated with the directory."""
    organization_id: Optional[str] = None
    """The ID of the organization the directory belongs to."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "DsyncActivatedData":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "directory"),
                id=data["id"],
                type=DsyncActivatedDataType(data["type"]),
                state=DsyncActivatedDataState(data["state"]),
                name=data["name"],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                external_key=data["external_key"],
                domains=[
                    DsyncActivatedDataDomain.from_dict(cast(Dict[str, Any], item))
                    for item in cast(list[Any], data["domains"])
                ],
                organization_id=data.get("organization_id"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("DsyncActivatedData", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["object"] = self.object
        result["id"] = self.id
        result["type"] = self.type.value if isinstance(self.type, Enum) else self.type
        result["state"] = (
            self.state.value if isinstance(self.state, Enum) else self.state
        )
        result["name"] = self.name
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        result["external_key"] = self.external_key
        result["domains"] = [item.to_dict() for item in self.domains]
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        return result
