# 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 cast
from typing import Any, Dict, List, Optional
from workos._types import _raise_deserialize_error
from workos.common.models.widget_session_token_scopes import WidgetSessionTokenScopes


@dataclass(slots=True)
class WidgetSessionToken:
    """Widget Session Token model."""

    organization_id: Optional[str] = None
    """The ID of the organization to scope the widget session to. Required when scopes are provided. Optional when issuing a token for user-only widgets (e.g. `UserProfile`, `UserSecurity`) that do not require organization context."""
    user_id: Optional[str] = None
    """The ID of the user to issue the widget session token for."""
    scopes: Optional[List["WidgetSessionTokenScopes"]] = None
    """The scopes to grant the widget session."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "WidgetSessionToken":
        """Deserialize from a dictionary."""
        try:
            return cls(
                organization_id=data.get("organization_id"),
                user_id=data.get("user_id"),
                scopes=[
                    WidgetSessionTokenScopes(item)
                    for item in cast(list[Any], _v_scopes)
                ]
                if (_v_scopes := data.get("scopes")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("WidgetSessionToken", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        if self.user_id is not None:
            result["user_id"] = self.user_id
        if self.scopes is not None:
            result["scopes"] = [
                item.value if isinstance(item, Enum) else item for item in self.scopes
            ]
        return result
