from collections.abc import Mapping
from typing import Any, TypeVar, TYPE_CHECKING

from attrs import define as _attrs_define
from attrs import field as _attrs_field

from ..types import UNSET, Unset

from typing import cast
from typing import Union

if TYPE_CHECKING:
    from ..models.flag_rule import FlagRule


T = TypeVar("T", bound="FlagEnvironment")


@_attrs_define
class FlagEnvironment:
    """
    Attributes:
        enabled (Union[Unset, bool]):  Default: False.
        default (Union[Any, None, Unset]):
        rules (Union[Unset, list['FlagRule']]):
    """

    enabled: Union[Unset, bool] = False
    default: Union[Any, None, Unset] = UNSET
    rules: Union[Unset, list["FlagRule"]] = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        enabled = self.enabled

        default: Union[Any, None, Unset]
        if isinstance(self.default, Unset):
            default = UNSET
        else:
            default = self.default

        rules: Union[Unset, list[dict[str, Any]]] = UNSET
        if not isinstance(self.rules, Unset):
            rules = []
            for rules_item_data in self.rules:
                rules_item = rules_item_data.to_dict()
                rules.append(rules_item)

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if enabled is not UNSET:
            field_dict["enabled"] = enabled
        if default is not UNSET:
            field_dict["default"] = default
        if rules is not UNSET:
            field_dict["rules"] = rules

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.flag_rule import FlagRule

        d = dict(src_dict)
        enabled = d.pop("enabled", UNSET)

        def _parse_default(data: object) -> Union[Any, None, Unset]:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(Union[Any, None, Unset], data)

        default = _parse_default(d.pop("default", UNSET))

        rules = []
        _rules = d.pop("rules", UNSET)
        for rules_item_data in _rules or []:
            rules_item = FlagRule.from_dict(rules_item_data)

            rules.append(rules_item)

        flag_environment = cls(
            enabled=enabled,
            default=default,
            rules=rules,
        )

        flag_environment.additional_properties = d
        return flag_environment

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
