# 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, Literal, cast

from workos._types import _format_datetime, _parse_datetime, _raise_deserialize_error

from .user_api_key_owner import UserApiKeyOwner


@dataclass(slots=True)
class UserApiKey:
    """User Api Key model."""

    object: Literal["api_key"]
    """Distinguishes the API Key object."""
    id: str
    """Unique identifier of the API Key."""
    owner: UserApiKeyOwner
    """The entity that owns the API Key."""
    name: str
    """A descriptive name for the API Key."""
    obfuscated_value: str
    """An obfuscated representation of the API Key value."""
    last_used_at: datetime | None
    """Timestamp of when the API Key was last used."""
    expires_at: datetime | None
    """Timestamp when the API Key expires. Null means the key does not expire."""
    permissions: list[str]
    """The permission slugs assigned to the API Key."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> UserApiKey:
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "api_key"),
                id=data["id"],
                owner=UserApiKeyOwner.from_dict(cast(dict[str, Any], data["owner"])),
                name=data["name"],
                obfuscated_value=data["obfuscated_value"],
                last_used_at=_parse_datetime(_v_last_used_at)
                if (_v_last_used_at := data["last_used_at"]) is not None
                else None,
                expires_at=_parse_datetime(_v_expires_at)
                if (_v_expires_at := data["expires_at"]) is not None
                else None,
                permissions=data["permissions"],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("UserApiKey", 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["owner"] = self.owner.to_dict()
        result["name"] = self.name
        result["obfuscated_value"] = self.obfuscated_value
        if self.last_used_at is not None:
            result["last_used_at"] = _format_datetime(self.last_used_at)
        else:
            result["last_used_at"] = None
        if self.expires_at is not None:
            result["expires_at"] = _format_datetime(self.expires_at)
        else:
            result["expires_at"] = None
        result["permissions"] = self.permissions
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        return result
