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

from workos._types import _format_datetime, _parse_datetime, _raise_deserialize_error

from .user_sessions_auth_method import UserSessionsAuthMethod
from .user_sessions_impersonator import UserSessionsImpersonator
from .user_sessions_status import UserSessionsStatus


@dataclass(slots=True)
class UserSessionsListItem:
    """User Sessions List Item model."""

    object: Literal["session"]
    """Distinguishes the session object."""
    id: str
    """The unique ID of the session."""
    ip_address: str | None
    """The IP address from which the session was created."""
    user_agent: str | None
    """The user agent string from the device that created the session."""
    user_id: str
    """The ID of the user this session belongs to."""
    auth_method: UserSessionsAuthMethod
    """The authentication method used to create this session."""
    status: UserSessionsStatus
    """The current status of the session."""
    expires_at: datetime
    """The timestamp when the session expires."""
    ended_at: datetime | None
    """The timestamp when the session ended."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    impersonator: UserSessionsImpersonator | None = None
    """Information about the impersonator if this session was created via impersonation."""
    organization_id: str | None = None
    """The ID of the organization this session is associated with."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> UserSessionsListItem:
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "session"),
                id=data["id"],
                ip_address=data["ip_address"],
                user_agent=data["user_agent"],
                user_id=data["user_id"],
                auth_method=UserSessionsAuthMethod(data["auth_method"]),
                status=UserSessionsStatus(data["status"]),
                expires_at=_parse_datetime(data["expires_at"]),
                ended_at=_parse_datetime(_v_ended_at)
                if (_v_ended_at := data["ended_at"]) is not None
                else None,
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                impersonator=UserSessionsImpersonator.from_dict(
                    cast(dict[str, Any], _v_impersonator)
                )
                if (_v_impersonator := data.get("impersonator")) is not None
                else None,
                organization_id=data.get("organization_id"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("UserSessionsListItem", 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.ip_address is not None:
            result["ip_address"] = self.ip_address
        else:
            result["ip_address"] = None
        if self.user_agent is not None:
            result["user_agent"] = self.user_agent
        else:
            result["user_agent"] = None
        result["user_id"] = self.user_id
        result["auth_method"] = (
            self.auth_method.value
            if isinstance(self.auth_method, Enum)
            else self.auth_method
        )
        result["status"] = (
            self.status.value if isinstance(self.status, Enum) else self.status
        )
        result["expires_at"] = _format_datetime(self.expires_at)
        if self.ended_at is not None:
            result["ended_at"] = _format_datetime(self.ended_at)
        else:
            result["ended_at"] = None
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        if self.impersonator is not None:
            result["impersonator"] = self.impersonator.to_dict()
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        return result
