# 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 Any, Literal, cast

from workos._types import _format_datetime, _parse_datetime, _raise_deserialize_error
from workos.common.models.connection_state import ConnectionState
from workos.common.models.connection_status import ConnectionStatus
from workos.common.models.connection_type import ConnectionType

from .connection_domain import ConnectionDomain


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

    object: Literal["connection"]
    """Distinguishes the Connection object."""
    id: str
    """Unique identifier for the Connection."""
    connection_type: ConnectionType
    """The type of the SSO Connection used to authenticate the user. The Connection type may be used to dynamically generate authorization URLs."""
    name: str
    """A human-readable name for the Connection. This will most commonly be the organization's name."""
    state: ConnectionState
    """Indicates whether a Connection is able to authenticate users."""
    domains: list[ConnectionDomain]
    """List of Organization Domains."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    organization_id: str | None = None
    """Unique identifier for the Organization in which the Connection resides."""
    status: ConnectionStatus | None = None
    """Deprecated. Use `state` instead.

    .. deprecated:: This field is deprecated."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> Connection:
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "connection"),
                id=data["id"],
                connection_type=ConnectionType(data["connection_type"]),
                name=data["name"],
                state=ConnectionState(data["state"]),
                domains=[
                    ConnectionDomain.from_dict(cast(dict[str, Any], item))
                    for item in cast(list[Any], data["domains"])
                ],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                organization_id=data.get("organization_id"),
                status=ConnectionStatus(_v_status)
                if (_v_status := data.get("status")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("Connection", 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["connection_type"] = (
            self.connection_type.value
            if isinstance(self.connection_type, Enum)
            else self.connection_type
        )
        result["name"] = self.name
        result["state"] = (
            self.state.value if isinstance(self.state, Enum) else self.state
        )
        result["domains"] = [item.to_dict() for item in self.domains]
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        if self.status is not None:
            result["status"] = (
                self.status.value if isinstance(self.status, Enum) else self.status
            )
        return result
