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

from __future__ import annotations

from dataclasses import dataclass
from enum import Enum
from typing import Any, Literal, cast

from workos._types import _raise_deserialize_error
from workos.common.models.profile_connection_type import ProfileConnectionType
from workos.common.models.slim_role import SlimRole


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

    object: Literal["profile"]
    """Distinguishes the profile object."""
    id: str
    """Unique identifier of the profile."""
    organization_id: str | None
    """The ID of the organization the user belongs to."""
    connection_id: str
    """The ID of the SSO connection used for authentication."""
    connection_type: ProfileConnectionType
    """The type of SSO connection."""
    idp_id: str
    """The user's unique identifier from the identity provider."""
    email: str
    """The user's email address."""
    first_name: str | None
    """The user's first name."""
    last_name: str | None
    """The user's last name."""
    name: str | None
    """The user's full name."""
    raw_attributes: dict[str, Any]
    """The complete set of raw attributes returned by the identity provider."""
    role: SlimRole | None = None
    """The role assigned to the user within the organization, if applicable."""
    roles: list[SlimRole] | None = None
    """The roles assigned to the user within the organization, if applicable."""
    groups: list[str] | None = None
    """The groups the user belongs to, as returned by the identity provider."""
    custom_attributes: dict[str, Any] | None = None
    """Custom attribute mappings defined for the connection, returned as key-value pairs."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> Profile:
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "profile"),
                id=data["id"],
                organization_id=data["organization_id"],
                connection_id=data["connection_id"],
                connection_type=ProfileConnectionType(data["connection_type"]),
                idp_id=data["idp_id"],
                email=data["email"],
                first_name=data["first_name"],
                last_name=data["last_name"],
                name=data["name"],
                raw_attributes=data["raw_attributes"],
                role=SlimRole.from_dict(cast(dict[str, Any], _v_role))
                if (_v_role := data.get("role")) is not None
                else None,
                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,
                groups=data.get("groups"),
                custom_attributes=data.get("custom_attributes"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("Profile", e)

    def to_dict(self) -> dict[str, Any]:
        """Serialize to a dictionary."""
        result: dict[str, Any] = {}
        result["object"] = self.object
        result["id"] = self.id
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        else:
            result["organization_id"] = None
        result["connection_id"] = self.connection_id
        result["connection_type"] = (
            self.connection_type.value
            if isinstance(self.connection_type, Enum)
            else self.connection_type
        )
        result["idp_id"] = self.idp_id
        result["email"] = self.email
        if self.first_name is not None:
            result["first_name"] = self.first_name
        else:
            result["first_name"] = None
        if self.last_name is not None:
            result["last_name"] = self.last_name
        else:
            result["last_name"] = None
        if self.name is not None:
            result["name"] = self.name
        else:
            result["name"] = None
        result["raw_attributes"] = self.raw_attributes
        if self.role is not None:
            result["role"] = self.role.to_dict()
        else:
            result["role"] = None
        if self.roles is not None:
            result["roles"] = [item.to_dict() for item in self.roles]
        else:
            result["roles"] = None
        if self.groups is not None:
            result["groups"] = self.groups
        if self.custom_attributes is not None:
            result["custom_attributes"] = self.custom_attributes
        return result
