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

from __future__ import annotations

from dataclasses import dataclass
from typing import cast
from typing import Any, Dict, List, Optional
from workos._types import _raise_deserialize_error

from .event_context_actor import EventContextActor
from .event_context_google_analytics_session import EventContextGoogleAnalyticsSession


@dataclass(slots=True)
class EventContext:
    """Additional context about the event."""

    google_analytics_client_id: Optional[str] = None
    """The Google Analytics client ID."""
    google_analytics_sessions: Optional[List["EventContextGoogleAnalyticsSession"]] = (
        None
    )
    """The Google Analytics sessions associated with the event."""
    ajs_anonymous_id: Optional[str] = None
    """The anonymous ID from analytics."""
    client_id: Optional[str] = None
    """The client ID associated with the event."""
    actor: Optional["EventContextActor"] = None
    previous_attributes: Optional[Dict[str, Any]] = None
    """Attributes that changed from their previous values."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "EventContext":
        """Deserialize from a dictionary."""
        try:
            return cls(
                google_analytics_client_id=data.get("google_analytics_client_id"),
                google_analytics_sessions=[
                    EventContextGoogleAnalyticsSession.from_dict(
                        cast(Dict[str, Any], item)
                    )
                    for item in cast(list[Any], _v_google_analytics_sessions)
                ]
                if (
                    _v_google_analytics_sessions := data.get(
                        "google_analytics_sessions"
                    )
                )
                is not None
                else None,
                ajs_anonymous_id=data.get("ajs_anonymous_id"),
                client_id=data.get("client_id"),
                actor=EventContextActor.from_dict(cast(Dict[str, Any], _v_actor))
                if (_v_actor := data.get("actor")) is not None
                else None,
                previous_attributes=data.get("previous_attributes"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("EventContext", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        if self.google_analytics_client_id is not None:
            result["google_analytics_client_id"] = self.google_analytics_client_id
        if self.google_analytics_sessions is not None:
            result["google_analytics_sessions"] = [
                item.to_dict() for item in self.google_analytics_sessions
            ]
        if self.ajs_anonymous_id is not None:
            result["ajs_anonymous_id"] = self.ajs_anonymous_id
        if self.client_id is not None:
            result["client_id"] = self.client_id
        if self.actor is not None:
            result["actor"] = self.actor.to_dict()
        if self.previous_attributes is not None:
            result["previous_attributes"] = self.previous_attributes
        return result
