from __future__ import annotations

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

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

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.enrichment_field import EnrichmentField


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


@_attrs_define
class SourceCollection:
    """A source collection for a flat taxonomy.

    Attributes:
        collection_id (str): The ID of the source collection for the taxonomy.
        enrichment_fields (list[EnrichmentField] | None | Unset): Fields to copy from matched taxonomy node when
            enriching (append/replace semantics). If omitted, the full payload is copied.
    """

    collection_id: str
    enrichment_fields: list[EnrichmentField] | None | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        enrichment_fields: list[dict[str, Any]] | None | Unset
        if isinstance(self.enrichment_fields, Unset):
            enrichment_fields = UNSET
        elif isinstance(self.enrichment_fields, list):
            enrichment_fields = []
            for enrichment_fields_type_0_item_data in self.enrichment_fields:
                enrichment_fields_type_0_item = enrichment_fields_type_0_item_data.to_dict()
                enrichment_fields.append(enrichment_fields_type_0_item)

        else:
            enrichment_fields = self.enrichment_fields

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

        return field_dict

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

        d = dict(src_dict)
        collection_id = d.pop("collection_id")

        def _parse_enrichment_fields(data: object) -> list[EnrichmentField] | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, list):
                    raise TypeError()
                enrichment_fields_type_0 = []
                _enrichment_fields_type_0 = data
                for enrichment_fields_type_0_item_data in _enrichment_fields_type_0:
                    enrichment_fields_type_0_item = EnrichmentField.from_dict(enrichment_fields_type_0_item_data)

                    enrichment_fields_type_0.append(enrichment_fields_type_0_item)

                return enrichment_fields_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(list[EnrichmentField] | None | Unset, data)

        enrichment_fields = _parse_enrichment_fields(d.pop("enrichment_fields", UNSET))

        source_collection = cls(
            collection_id=collection_id,
            enrichment_fields=enrichment_fields,
        )

        source_collection.additional_properties = d
        return source_collection

    @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
