# 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 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 .slim_role import SlimRole
from .organization_membership_created_data_status import (
    OrganizationMembershipCreatedDataStatus,
)


@dataclass(slots=True)
class OrganizationMembershipCreatedData:
    """The event payload."""

    object: Literal["organization_membership"]
    """Distinguishes the organization membership object."""
    id: str
    """Unique identifier of the organization membership."""
    user_id: str
    """The ID of the user."""
    organization_id: str
    """The ID of the organization."""
    status: "OrganizationMembershipCreatedDataStatus"
    """The status of the organization membership."""
    role: "SlimRole"
    """The role associated with the membership."""
    custom_attributes: Dict[str, Any]
    """Custom attributes associated with the membership."""
    directory_managed: bool
    """Whether the membership is managed by a directory sync provider."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    roles: Optional[List["SlimRole"]] = None
    """The roles associated with the membership."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "OrganizationMembershipCreatedData":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "organization_membership"),
                id=data["id"],
                user_id=data["user_id"],
                organization_id=data["organization_id"],
                status=OrganizationMembershipCreatedDataStatus(data["status"]),
                role=SlimRole.from_dict(cast(Dict[str, Any], data["role"])),
                custom_attributes=data["custom_attributes"],
                directory_managed=data["directory_managed"],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                roles=[
                    SlimRole.from_dict(cast(Dict[str, Any], item))
                    for item in cast(list[Any], _v_roles)
                ]
                if (_v_roles := data.get("roles")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("OrganizationMembershipCreatedData", 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["user_id"] = self.user_id
        result["organization_id"] = self.organization_id
        result["status"] = (
            self.status.value if isinstance(self.status, Enum) else self.status
        )
        result["role"] = self.role.to_dict()
        result["custom_attributes"] = self.custom_attributes
        result["directory_managed"] = self.directory_managed
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        if self.roles is not None:
            result["roles"] = [item.to_dict() for item in self.roles]
        return result
