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

from .organization_api_key_with_value_owner import OrganizationApiKeyWithValueOwner


@dataclass(slots=True)
class OrganizationApiKeyWithValue:
    """Organization Api Key With Value model."""

    object: Literal["api_key"]
    """Distinguishes the API Key object."""
    id: str
    """Unique identifier of the API Key."""
    owner: "OrganizationApiKeyWithValueOwner"
    """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: Optional[datetime]
    """Timestamp of when the API Key was last used."""
    expires_at: Optional[datetime]
    """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."""
    value: str
    """The full API Key value. Only returned once at creation time."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "OrganizationApiKeyWithValue":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "api_key"),
                id=data["id"],
                owner=OrganizationApiKeyWithValueOwner.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"]),
                value=data["value"],
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("OrganizationApiKeyWithValue", 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)
        result["value"] = self.value
        return result
