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

from __future__ import annotations

from dataclasses import dataclass
from enum import Enum
from typing import Any, Literal

from workos._types import _raise_deserialize_error

from .connected_account_auth_method import ConnectedAccountAuthMethod
from .connected_account_state import ConnectedAccountState


@dataclass(slots=True)
class ConnectedAccount:
    """Connected Account model."""

    object: Literal["connected_account"]
    """Distinguishes the connected account object."""
    id: str
    """The unique identifier of the connected account."""
    user_id: str | None
    """The [User](https://workos.com/docs/reference/authkit/user) identifier associated with this connection."""
    organization_id: str | None
    """The [Organization](https://workos.com/docs/reference/organization) identifier associated with this connection, or `null` if not scoped to an organization."""
    scopes: list[str]
    """The OAuth scopes granted for this connection."""
    state: ConnectedAccountState
    """The state of the connected account:
- `connected`: The connection is active and tokens are valid.
- `needs_reauthorization`: The user needs to reauthorize the connection, typically because required scopes have changed.
- `disconnected`: The connection has been disconnected."""
    created_at: str
    """The timestamp when the connection was created."""
    updated_at: str
    """The timestamp when the connection was last updated."""
    auth_method: ConnectedAccountAuthMethod | None = None
    """The authentication method used for this connection (`oauth`, `api_key`, or `client_credentials`). Defaults to `oauth` if absent."""
    api_key_last_4: str | None = None
    """The last four characters of the API key, or `null` for OAuth connections."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> ConnectedAccount:
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "connected_account"),
                id=data["id"],
                user_id=data["user_id"],
                organization_id=data["organization_id"],
                scopes=data["scopes"],
                state=ConnectedAccountState(data["state"]),
                created_at=data["created_at"],
                updated_at=data["updated_at"],
                auth_method=ConnectedAccountAuthMethod(_v_auth_method)
                if (_v_auth_method := data.get("auth_method")) is not None
                else None,
                api_key_last_4=data.get("api_key_last_4"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("ConnectedAccount", e)

    def to_dict(self) -> dict[str, Any]:
        """Serialize to a dictionary."""
        result: dict[str, Any] = {}
        result["object"] = self.object
        result["id"] = self.id
        if self.user_id is not None:
            result["user_id"] = self.user_id
        else:
            result["user_id"] = None
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        else:
            result["organization_id"] = None
        result["scopes"] = self.scopes
        result["state"] = (
            self.state.value if isinstance(self.state, Enum) else self.state
        )
        result["created_at"] = self.created_at
        result["updated_at"] = self.updated_at
        if self.auth_method is not None:
            result["auth_method"] = (
                self.auth_method.value
                if isinstance(self.auth_method, Enum)
                else self.auth_method
            )
        if self.api_key_last_4 is not None:
            result["api_key_last_4"] = self.api_key_last_4
        else:
            result["api_key_last_4"] = None
        return result
