# 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 .connection_activated_data_domain import ConnectionActivatedDataDomain
from .connection_activated_data_connection_type import (
    ConnectionActivatedDataConnectionType,
)
from .connection_activated_data_state import ConnectionActivatedDataState
from .connection_activated_data_status import ConnectionActivatedDataStatus


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

    object: Literal["connection"]
    """Distinguishes the connection object."""
    id: str
    """Unique identifier of the connection."""
    state: "ConnectionActivatedDataState"
    """The current state of the connection."""
    name: str
    """The name of the connection."""
    connection_type: "ConnectionActivatedDataConnectionType"
    """The type of the connection."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    external_key: str
    """The external key of the connection."""
    status: "ConnectionActivatedDataStatus"
    """Deprecated. Use state instead."""
    domains: List["ConnectionActivatedDataDomain"]
    """The domains associated with the connection."""
    organization_id: Optional[str] = None
    """The ID of the organization the connection belongs to."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "ConnectionActivatedData":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "connection"),
                id=data["id"],
                state=ConnectionActivatedDataState(data["state"]),
                name=data["name"],
                connection_type=ConnectionActivatedDataConnectionType(
                    data["connection_type"]
                ),
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                external_key=data["external_key"],
                status=ConnectionActivatedDataStatus(data["status"]),
                domains=[
                    ConnectionActivatedDataDomain.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("ConnectionActivatedData", 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["state"] = (
            self.state.value if isinstance(self.state, Enum) else self.state
        )
        result["name"] = self.name
        result["connection_type"] = (
            self.connection_type.value
            if isinstance(self.connection_type, Enum)
            else self.connection_type
        )
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        result["external_key"] = self.external_key
        result["status"] = (
            self.status.value if isinstance(self.status, Enum) else self.status
        )
        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
