# 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.directory_group import DirectoryGroup
from .directory_user_with_groups_email import DirectoryUserWithGroupsEmail
from workos.common.models.slim_role import SlimRole
from workos.common.models.directory_user_with_groups_state import (
    DirectoryUserWithGroupsState,
)


@dataclass(slots=True)
class DirectoryUserWithGroups:
    """Directory User With Groups model."""

    object: Literal["directory_user"]
    """Distinguishes the Directory User object."""
    id: str
    """Unique identifier for the Directory User."""
    directory_id: str
    """The identifier of the Directory the Directory User belongs to."""
    organization_id: str
    """The identifier for the Organization in which the Directory resides."""
    idp_id: str
    """Unique identifier for the user, assigned by the Directory Provider. Different Directory Providers use different ID formats."""
    email: Optional[str]
    """The email address of the user."""
    state: "DirectoryUserWithGroupsState"
    """The state of the user."""
    custom_attributes: Dict[str, Any]
    """An object containing the custom attribute mapping for the Directory Provider."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    first_name: Optional[str] = None
    """The first name of the user."""
    last_name: Optional[str] = None
    """The last name of the user."""
    name: Optional[str] = None
    """The full name of the user."""
    emails: Optional[List["DirectoryUserWithGroupsEmail"]] = None
    """A list of email addresses for the user.

    .. deprecated:: This field is deprecated."""
    job_title: Optional[str] = None
    """The job title of the user.

    .. deprecated:: This field is deprecated."""
    username: Optional[str] = None
    """The username of the user.

    .. deprecated:: This field is deprecated."""
    raw_attributes: Optional[Dict[str, Any]] = None
    """The raw attributes received from the directory provider.

    .. deprecated:: This field is deprecated."""
    role: Optional["SlimRole"] = None
    roles: Optional[List["SlimRole"]] = None
    """All roles assigned to the user."""
    groups: Optional[List["DirectoryGroup"]] = None
    """The directory groups the user belongs to. Deprecated: starting May 1, 2026, this field returns an empty array by default for newly created teams. Existing teams currently depending on this field should migrate to the new access pattern for better throughput performance — the field is unbounded by user, so users with many group memberships produce large, slow response payloads. Use the List Directory Groups endpoint with a `user` filter to fetch a user's group memberships.

    .. deprecated:: This field is deprecated."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "DirectoryUserWithGroups":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "directory_user"),
                id=data["id"],
                directory_id=data["directory_id"],
                organization_id=data["organization_id"],
                idp_id=data["idp_id"],
                email=data["email"],
                state=DirectoryUserWithGroupsState(data["state"]),
                custom_attributes=data["custom_attributes"],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                first_name=data.get("first_name"),
                last_name=data.get("last_name"),
                name=data.get("name"),
                emails=[
                    DirectoryUserWithGroupsEmail.from_dict(cast(Dict[str, Any], item))
                    for item in cast(list[Any], _v_emails)
                ]
                if (_v_emails := data.get("emails")) is not None
                else None,
                job_title=data.get("job_title"),
                username=data.get("username"),
                raw_attributes=data.get("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=[
                    DirectoryGroup.from_dict(cast(Dict[str, Any], item))
                    for item in cast(list[Any], _v_groups)
                ]
                if (_v_groups := data.get("groups")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("DirectoryUserWithGroups", 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["directory_id"] = self.directory_id
        result["organization_id"] = self.organization_id
        result["idp_id"] = self.idp_id
        if self.email is not None:
            result["email"] = self.email
        else:
            result["email"] = None
        result["state"] = (
            self.state.value if isinstance(self.state, Enum) else self.state
        )
        result["custom_attributes"] = self.custom_attributes
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        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
        if self.emails is not None:
            result["emails"] = [item.to_dict() for item in self.emails]
        if self.job_title is not None:
            result["job_title"] = self.job_title
        else:
            result["job_title"] = None
        if self.username is not None:
            result["username"] = self.username
        else:
            result["username"] = None
        if self.raw_attributes is not None:
            result["raw_attributes"] = self.raw_attributes
        if self.role is not None:
            result["role"] = self.role.to_dict()
        if self.roles is not None:
            result["roles"] = [item.to_dict() for item in self.roles]
        if self.groups is not None:
            result["groups"] = [item.to_dict() for item in self.groups]
        return result
