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.graph_rag_search_data_graph_visualization_type_0 import GraphRAGSearchDataGraphVisualizationType0
    from ..models.source_chunk import SourceChunk


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


@_attrs_define
class GraphRAGSearchData:
    """
    Attributes:
        sources (list[SourceChunk]):
        graph_visualization (GraphRAGSearchDataGraphVisualizationType0 | None | Unset):
    """

    sources: list[SourceChunk]
    graph_visualization: GraphRAGSearchDataGraphVisualizationType0 | None | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.graph_rag_search_data_graph_visualization_type_0 import GraphRAGSearchDataGraphVisualizationType0

        sources = []
        for sources_item_data in self.sources:
            sources_item = sources_item_data.to_dict()
            sources.append(sources_item)

        graph_visualization: dict[str, Any] | None | Unset
        if isinstance(self.graph_visualization, Unset):
            graph_visualization = UNSET
        elif isinstance(self.graph_visualization, GraphRAGSearchDataGraphVisualizationType0):
            graph_visualization = self.graph_visualization.to_dict()
        else:
            graph_visualization = self.graph_visualization

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

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.graph_rag_search_data_graph_visualization_type_0 import GraphRAGSearchDataGraphVisualizationType0
        from ..models.source_chunk import SourceChunk

        d = dict(src_dict)
        sources = []
        _sources = d.pop("sources")
        for sources_item_data in _sources:
            sources_item = SourceChunk.from_dict(sources_item_data)

            sources.append(sources_item)

        def _parse_graph_visualization(data: object) -> GraphRAGSearchDataGraphVisualizationType0 | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                graph_visualization_type_0 = GraphRAGSearchDataGraphVisualizationType0.from_dict(data)

                return graph_visualization_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(GraphRAGSearchDataGraphVisualizationType0 | None | Unset, data)

        graph_visualization = _parse_graph_visualization(d.pop("graph_visualization", UNSET))

        graph_rag_search_data = cls(
            sources=sources,
            graph_visualization=graph_visualization,
        )

        graph_rag_search_data.additional_properties = d
        return graph_rag_search_data

    @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
