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

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from typing import Any, Dict, Literal, Optional
from workos._types import _raise_deserialize_error
from workos._types import _format_datetime, _parse_datetime


@dataclass(slots=True)
class AuthenticationChallenge:
    """Authentication Challenge model."""

    object: Literal["authentication_challenge"]
    """Distinguishes the authentication challenge object."""
    id: str
    """The unique ID of the authentication challenge."""
    authentication_factor_id: str
    """The unique ID of the authentication factor the challenge belongs to."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    expires_at: Optional[datetime] = None
    """The timestamp when the challenge will expire. Does not apply to TOTP factors."""
    code: Optional[str] = None
    """The one-time code for the challenge."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "AuthenticationChallenge":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "authentication_challenge"),
                id=data["id"],
                authentication_factor_id=data["authentication_factor_id"],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                expires_at=_parse_datetime(_v_expires_at)
                if (_v_expires_at := data.get("expires_at")) is not None
                else None,
                code=data.get("code"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("AuthenticationChallenge", 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["authentication_factor_id"] = self.authentication_factor_id
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        if self.expires_at is not None:
            result["expires_at"] = _format_datetime(self.expires_at)
        if self.code is not None:
            result["code"] = self.code
        return result
