# 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.slim_role import SlimRole
from .user_role_assignment_resource import UserRoleAssignmentResource
from .user_role_assignment_source import UserRoleAssignmentSource


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

    object: Literal["role_assignment"]
    """Distinguishes the role assignment object."""
    id: str
    """Unique identifier of the role assignment."""
    organization_membership_id: str
    """The ID of the organization membership the role is assigned to."""
    role: "SlimRole"
    """The role included in the assignment."""
    resource: "UserRoleAssignmentResource"
    """The resource the role is assigned on."""
    source: "UserRoleAssignmentSource"
    """The origin of the role assignment."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "UserRoleAssignment":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "role_assignment"),
                id=data["id"],
                organization_membership_id=data["organization_membership_id"],
                role=SlimRole.from_dict(cast(Dict[str, Any], data["role"])),
                resource=UserRoleAssignmentResource.from_dict(
                    cast(Dict[str, Any], data["resource"])
                ),
                source=UserRoleAssignmentSource.from_dict(
                    cast(Dict[str, Any], data["source"])
                ),
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("UserRoleAssignment", 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["organization_membership_id"] = self.organization_membership_id
        result["role"] = self.role.to_dict()
        result["resource"] = self.resource.to_dict()
        result["source"] = self.source.to_dict()
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        return result
