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

from .authentication_factor_enrolled_sms import AuthenticationFactorEnrolledSms
from .authentication_factor_enrolled_totp import AuthenticationFactorEnrolledTotp
from .authentication_factor_enrolled_type import AuthenticationFactorEnrolledType


@dataclass(slots=True)
class AuthenticationFactorEnrolled:
    """Authentication Factor Enrolled model."""

    object: Literal["authentication_factor"]
    """Distinguishes the authentication factor object."""
    id: str
    """The unique ID of the factor."""
    type: "AuthenticationFactorEnrolledType"
    """The type of the factor to enroll."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    user_id: Optional[str] = None
    """The ID of the [user](https://workos.com/docs/reference/authkit/user)."""
    sms: Optional["AuthenticationFactorEnrolledSms"] = None
    """SMS-based authentication factor details."""
    totp: Optional["AuthenticationFactorEnrolledTotp"] = None
    """TOTP-based authentication factor details. Includes enrollment secrets only available at creation time."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "AuthenticationFactorEnrolled":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "authentication_factor"),
                id=data["id"],
                type=AuthenticationFactorEnrolledType(data["type"]),
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                user_id=data.get("user_id"),
                sms=AuthenticationFactorEnrolledSms.from_dict(
                    cast(Dict[str, Any], _v_sms)
                )
                if (_v_sms := data.get("sms")) is not None
                else None,
                totp=AuthenticationFactorEnrolledTotp.from_dict(
                    cast(Dict[str, Any], _v_totp)
                )
                if (_v_totp := data.get("totp")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("AuthenticationFactorEnrolled", 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["type"] = self.type.value if isinstance(self.type, Enum) else self.type
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        if self.user_id is not None:
            result["user_id"] = self.user_id
        if self.sms is not None:
            result["sms"] = self.sms.to_dict()
        if self.totp is not None:
            result["totp"] = self.totp.to_dict()
        return result
