# 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

from workos._types import _format_datetime, _parse_datetime, _raise_deserialize_error
from workos.common.models.connected_account_input_state import (
    ConnectedAccountInputState,
)


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

    access_token: str | None = None
    """The OAuth access token for the connected account."""
    refresh_token: str | None = None
    """The OAuth refresh token for the connected account."""
    expires_at: datetime | None = None
    """The ISO-8601 timestamp when the access token expires. Required when `access_token` is provided for tokens that expire."""
    scopes: list[str] | None = None
    """The OAuth scopes granted for this connection."""
    state: ConnectedAccountInputState | None = None
    """Explicitly set the state of the connected account. When omitted, the state is derived from the token combination provided."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> ConnectedAccountInput:
        """Deserialize from a dictionary."""
        try:
            return cls(
                access_token=data.get("access_token"),
                refresh_token=data.get("refresh_token"),
                expires_at=_parse_datetime(_v_expires_at)
                if (_v_expires_at := data.get("expires_at")) is not None
                else None,
                scopes=data.get("scopes"),
                state=ConnectedAccountInputState(_v_state)
                if (_v_state := data.get("state")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("ConnectedAccountInput", e)

    def to_dict(self) -> dict[str, Any]:
        """Serialize to a dictionary."""
        result: dict[str, Any] = {}
        if self.access_token is not None:
            result["access_token"] = self.access_token
        if self.refresh_token is not None:
            result["refresh_token"] = self.refresh_token
        if self.expires_at is not None:
            result["expires_at"] = _format_datetime(self.expires_at)
        if self.scopes is not None:
            result["scopes"] = self.scopes
        if self.state is not None:
            result["state"] = (
                self.state.value if isinstance(self.state, Enum) else self.state
            )
        return result
