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


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

    object: Literal["connected_account"]
    """The connected account object."""
    id: str
    """The unique ID of the connected account."""
    data_integration_id: str
    """The unique ID of the data integration."""
    provider_slug: str
    """The provider slug for this connected account."""
    user_id: Optional[str]
    """The ID of the User the connected account belongs to."""
    organization_id: Optional[str]
    """The ID of the Organization the connected account belongs to."""
    scopes: List[str]
    """The OAuth scopes granted for this connected account."""
    state: "PipeConnectedAccountState"
    """The state of the connected account."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "PipeConnectedAccount":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "connected_account"),
                id=data["id"],
                data_integration_id=data["data_integration_id"],
                provider_slug=data["provider_slug"],
                user_id=data["user_id"],
                organization_id=data["organization_id"],
                scopes=data["scopes"],
                state=PipeConnectedAccountState(data["state"]),
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("PipeConnectedAccount", 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["data_integration_id"] = self.data_integration_id
        result["provider_slug"] = self.provider_slug
        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"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        return result
