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

from .directory_metadata import DirectoryMetadata
from workos.common.models.directory_state import DirectoryState
from workos.common.models.directory_type import DirectoryType


@dataclass(slots=True)
class Directory:
    """Directory model."""

    object: Literal["directory"]
    """Distinguishes the Directory object."""
    id: str
    """Unique identifier for the Directory."""
    organization_id: str
    """The unique identifier for the Organization in which the directory resides."""
    external_key: str
    """External Key for the Directory."""
    type: "DirectoryType"
    """The type of external Directory Provider integrated with."""
    state: "DirectoryState"
    """Describes whether the Directory has been successfully connected to an external provider."""
    name: str
    """The name of the directory."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    domain: Optional[str] = None
    """The URL associated with an Enterprise Client."""
    metadata: Optional["DirectoryMetadata"] = None
    """Aggregate counts of directory users and groups synced from the provider."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "Directory":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "directory"),
                id=data["id"],
                organization_id=data["organization_id"],
                external_key=data["external_key"],
                type=DirectoryType(data["type"]),
                state=DirectoryState(data["state"]),
                name=data["name"],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                domain=data.get("domain"),
                metadata=DirectoryMetadata.from_dict(cast(Dict[str, Any], _v_metadata))
                if (_v_metadata := data.get("metadata")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("Directory", 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["organization_id"] = self.organization_id
        result["external_key"] = self.external_key
        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)
        if self.domain is not None:
            result["domain"] = self.domain
        if self.metadata is not None:
            result["metadata"] = self.metadata.to_dict()
        return result
