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

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Literal, Optional
from workos._types import _raise_deserialize_error
from workos._types import _format_datetime, _parse_datetime
from workos.common.models.role_type import RoleType


@dataclass(slots=True)
class Role:
    """Role model."""

    slug: str
    """A unique slug for the role."""
    object: Literal["role"]
    """Distinguishes the role object."""
    id: str
    """Unique identifier of the role."""
    name: str
    """A descriptive name for the role."""
    description: Optional[str]
    """An optional description of the role."""
    type: "RoleType"
    """Whether the role is scoped to the environment or an organization (custom role)."""
    resource_type_slug: str
    """The slug of the resource type the role is scoped to."""
    permissions: List[str]
    """The permission slugs assigned to the role."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "Role":
        """Deserialize from a dictionary."""
        try:
            return cls(
                slug=data["slug"],
                object=data.get("object", "role"),
                id=data["id"],
                name=data["name"],
                description=data["description"],
                type=RoleType(data["type"]),
                resource_type_slug=data["resource_type_slug"],
                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("Role", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["slug"] = self.slug
        result["object"] = self.object
        result["id"] = self.id
        result["name"] = self.name
        if self.description is not None:
            result["description"] = self.description
        else:
            result["description"] = None
        result["type"] = self.type.value if isinstance(self.type, Enum) else self.type
        result["resource_type_slug"] = self.resource_type_slug
        result["permissions"] = self.permissions
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        return result
