# 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 cast
from typing import Any, Dict, Literal
from workos._types import _raise_deserialize_error
from workos._types import _format_datetime, _parse_datetime

from workos.common.models.user import User


@dataclass(slots=True)
class EmailChange:
    """Email Change model."""

    object: Literal["email_change"]
    """Distinguishes the email change object."""
    user: "User"
    new_email: str
    """The new email address the user is changing to."""
    expires_at: datetime
    """The timestamp when the email change code expires."""
    created_at: datetime
    """The timestamp when the email change challenge was created."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "EmailChange":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "email_change"),
                user=User.from_dict(cast(Dict[str, Any], data["user"])),
                new_email=data["new_email"],
                expires_at=_parse_datetime(data["expires_at"]),
                created_at=_parse_datetime(data["created_at"]),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("EmailChange", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["object"] = self.object
        result["user"] = self.user.to_dict()
        result["new_email"] = self.new_email
        result["expires_at"] = _format_datetime(self.expires_at)
        result["created_at"] = _format_datetime(self.created_at)
        return result
