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.source_chunk_metadata import SourceChunkMetadata


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


@_attrs_define
class SourceChunk:
    """
    Attributes:
        chunk_id (str):
        source (str):
        text_preview (str):
        score (float):
        document_id (None | str | Unset):
        s3_url (None | str | Unset):
        metadata (SourceChunkMetadata | Unset):
    """

    chunk_id: str
    source: str
    text_preview: str
    score: float
    document_id: None | str | Unset = UNSET
    s3_url: None | str | Unset = UNSET
    metadata: SourceChunkMetadata | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        source = self.source

        text_preview = self.text_preview

        score = self.score

        document_id: None | str | Unset
        if isinstance(self.document_id, Unset):
            document_id = UNSET
        else:
            document_id = self.document_id

        s3_url: None | str | Unset
        if isinstance(self.s3_url, Unset):
            s3_url = UNSET
        else:
            s3_url = self.s3_url

        metadata: dict[str, Any] | Unset = UNSET
        if not isinstance(self.metadata, Unset):
            metadata = self.metadata.to_dict()

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "chunk_id": chunk_id,
                "source": source,
                "text_preview": text_preview,
                "score": score,
            }
        )
        if document_id is not UNSET:
            field_dict["document_id"] = document_id
        if s3_url is not UNSET:
            field_dict["s3_url"] = s3_url
        if metadata is not UNSET:
            field_dict["metadata"] = metadata

        return field_dict

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

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

        source = d.pop("source")

        text_preview = d.pop("text_preview")

        score = d.pop("score")

        def _parse_document_id(data: object) -> None | str | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(None | str | Unset, data)

        document_id = _parse_document_id(d.pop("document_id", UNSET))

        def _parse_s3_url(data: object) -> None | str | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(None | str | Unset, data)

        s3_url = _parse_s3_url(d.pop("s3_url", UNSET))

        _metadata = d.pop("metadata", UNSET)
        metadata: SourceChunkMetadata | Unset
        if isinstance(_metadata, Unset):
            metadata = UNSET
        else:
            metadata = SourceChunkMetadata.from_dict(_metadata)

        source_chunk = cls(
            chunk_id=chunk_id,
            source=source,
            text_preview=text_preview,
            score=score,
            document_id=document_id,
            s3_url=s3_url,
            metadata=metadata,
        )

        source_chunk.additional_properties = d
        return source_chunk

    @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
