# 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 .group_role_assignment_resource import GroupRoleAssignmentResource
from workos.common.models.slim_role import SlimRole


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

    object: Literal["group_role_assignment"]
    """Distinguishes the group role assignment object."""
    id: str
    """Unique identifier of the group role assignment."""
    group_id: str
    """The ID of the group the role is assigned to."""
    role: "SlimRole"
    """The role included in the assignment."""
    resource: "GroupRoleAssignmentResource"
    """The resource the role is assigned on."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""

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