# 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, cast

from workos._types import _raise_deserialize_error
from workos.common.models.authenticate_response_authentication_method import (
    AuthenticateResponseAuthenticationMethod,
)
from workos.common.models.authenticate_response_impersonator import (
    AuthenticateResponseImpersonator,
)
from workos.common.models.user import User

from .authenticate_response_oauth_token import AuthenticateResponseOAuthToken


@dataclass(slots=True)
class AuthenticateResponse:
    """Authenticate Response model."""

    user: User
    """The corresponding [user](https://workos.com/docs/reference/authkit/user) object."""
    access_token: str
    """A JWT containing information about the current session."""
    refresh_token: str
    """[Exchange this token](https://workos.com/docs/reference/authkit/authentication/refresh-token) for a new access token."""
    organization_id: str | None = None
    """The ID of the organization the user selected to sign in to."""
    authkit_authorization_code: str | None = None
    """An authorization code that can be exchanged for tokens by a different application."""
    authentication_method: AuthenticateResponseAuthenticationMethod | None = None
    """The authentication method used to initiate the session."""
    impersonator: AuthenticateResponseImpersonator | None = None
    """Information about the impersonator if this session was created via impersonation."""
    oauth_tokens: AuthenticateResponseOAuthToken | None = None
    """The OAuth tokens from the identity provider, if applicable."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> AuthenticateResponse:
        """Deserialize from a dictionary."""
        try:
            return cls(
                user=User.from_dict(cast(dict[str, Any], data["user"])),
                access_token=data["access_token"],
                refresh_token=data["refresh_token"],
                organization_id=data.get("organization_id"),
                authkit_authorization_code=data.get("authkit_authorization_code"),
                authentication_method=AuthenticateResponseAuthenticationMethod(
                    _v_authentication_method
                )
                if (_v_authentication_method := data.get("authentication_method"))
                is not None
                else None,
                impersonator=AuthenticateResponseImpersonator.from_dict(
                    cast(dict[str, Any], _v_impersonator)
                )
                if (_v_impersonator := data.get("impersonator")) is not None
                else None,
                oauth_tokens=AuthenticateResponseOAuthToken.from_dict(
                    cast(dict[str, Any], _v_oauth_tokens)
                )
                if (_v_oauth_tokens := data.get("oauth_tokens")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("AuthenticateResponse", e)

    def to_dict(self) -> dict[str, Any]:
        """Serialize to a dictionary."""
        result: dict[str, Any] = {}
        result["user"] = self.user.to_dict()
        result["access_token"] = self.access_token
        result["refresh_token"] = self.refresh_token
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        if self.authkit_authorization_code is not None:
            result["authkit_authorization_code"] = self.authkit_authorization_code
        if self.authentication_method is not None:
            result["authentication_method"] = (
                self.authentication_method.value
                if isinstance(self.authentication_method, Enum)
                else self.authentication_method
            )
        if self.impersonator is not None:
            result["impersonator"] = self.impersonator.to_dict()
        if self.oauth_tokens is not None:
            result["oauth_tokens"] = self.oauth_tokens.to_dict()
        return result
