# 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

from workos._types import _format_datetime, _parse_datetime, _raise_deserialize_error


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

    name: str
    """A descriptive name for the API key."""
    organization_id: str
    """The ID of the organization the user API key is associated with. The user must have an active membership in this organization."""
    permissions: list[str] | None = None
    """The permission slugs to assign to the API key. Each permission must be enabled for user API keys."""
    expires_at: datetime | None = None
    """The timestamp when the API key should expire. Must be a future timestamp. If omitted, the key does not expire."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> CreateUserApiKey:
        """Deserialize from a dictionary."""
        try:
            return cls(
                name=data["name"],
                organization_id=data["organization_id"],
                permissions=data.get("permissions"),
                expires_at=_parse_datetime(_v_expires_at)
                if (_v_expires_at := data.get("expires_at")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("CreateUserApiKey", e)

    def to_dict(self) -> dict[str, Any]:
        """Serialize to a dictionary."""
        result: dict[str, Any] = {}
        result["name"] = self.name
        result["organization_id"] = self.organization_id
        if self.permissions is not None:
            result["permissions"] = self.permissions
        if self.expires_at is not None:
            result["expires_at"] = _format_datetime(self.expires_at)
        return result
