# 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 workos.common.models.slim_role import SlimRole
from workos.common.models.user import User
from workos.common.models.organization_membership_status import (
    OrganizationMembershipStatus,
)


@dataclass(slots=True)
class OrganizationMembership:
    """Organization Membership model."""

    object: Literal["organization_membership"]
    """Distinguishes the organization membership object."""
    id: str
    """The unique ID of the organization membership."""
    user_id: str
    """The ID of the user."""
    organization_id: str
    """The ID of the organization which the user belongs to."""
    status: "OrganizationMembershipStatus"
    """The status of the organization membership. One of `active`, `inactive`, or `pending`."""
    directory_managed: bool
    """Whether this organization membership is managed by a directory sync connection."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    role: "SlimRole"
    """The primary role assigned to the user within the organization."""
    roles: List["SlimRole"]
    """The list of roles assigned to the user within the organization."""
    user: "User"
    """The user that belongs to the organization through this membership."""
    organization_name: Optional[str] = None
    """The name of the organization which the user belongs to."""
    custom_attributes: Optional[Dict[str, Any]] = None
    """An object containing IdP-sourced attributes from the linked [Directory User](https://workos.com/docs/reference/directory-sync/directory-user) or [SSO Profile](https://workos.com/docs/reference/sso/profile). Directory User attributes take precedence when both are linked."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "OrganizationMembership":
        """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=OrganizationMembershipStatus(data["status"]),
                directory_managed=data["directory_managed"],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                role=SlimRole.from_dict(cast(Dict[str, Any], data["role"])),
                roles=[
                    SlimRole.from_dict(cast(Dict[str, Any], item))
                    for item in cast(list[Any], data["roles"])
                ],
                user=User.from_dict(cast(Dict[str, Any], data["user"])),
                organization_name=data.get("organization_name"),
                custom_attributes=data.get("custom_attributes"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("OrganizationMembership", 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["directory_managed"] = self.directory_managed
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        result["role"] = self.role.to_dict()
        result["roles"] = [item.to_dict() for item in self.roles]
        result["user"] = self.user.to_dict()
        if self.organization_name is not None:
            result["organization_name"] = self.organization_name
        if self.custom_attributes is not None:
            result["custom_attributes"] = self.custom_attributes
        return result
