# 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, Optional
from workos._types import _raise_deserialize_error

from .flag_rule_updated_context_previous_attribute_context_configured_target import (
    FlagRuleUpdatedContextPreviousAttributeContextConfiguredTarget,
)
from .flag_rule_updated_context_previous_attribute_context_access_type import (
    FlagRuleUpdatedContextPreviousAttributeContextAccessType,
)


@dataclass(slots=True)
class FlagRuleUpdatedContextPreviousAttributeContext:
    """The previous context attributes of the flag rule."""

    access_type: Optional[
        "FlagRuleUpdatedContextPreviousAttributeContextAccessType"
    ] = None
    """The previous access type of the flag rule."""
    configured_targets: Optional[
        "FlagRuleUpdatedContextPreviousAttributeContextConfiguredTarget"
    ] = None
    """The previous configured targets for the flag rule."""

    @classmethod
    def from_dict(
        cls, data: Dict[str, Any]
    ) -> "FlagRuleUpdatedContextPreviousAttributeContext":
        """Deserialize from a dictionary."""
        try:
            return cls(
                access_type=FlagRuleUpdatedContextPreviousAttributeContextAccessType(
                    _v_access_type
                )
                if (_v_access_type := data.get("access_type")) is not None
                else None,
                configured_targets=FlagRuleUpdatedContextPreviousAttributeContextConfiguredTarget.from_dict(
                    cast(Dict[str, Any], _v_configured_targets)
                )
                if (_v_configured_targets := data.get("configured_targets")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error(
                "FlagRuleUpdatedContextPreviousAttributeContext", e
            )

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