from __future__ import annotations

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

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

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.document import Document
    from ..models.search_hit_highlight import SearchHitHighlight


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


@_attrs_define
class SearchHit:
    """
    Attributes:
        field_id (str | Unset):
        field_score (float | Unset):
        field_source (Document | Unset):
        highlight (SearchHitHighlight | Unset):
    """

    field_id: str | Unset = UNSET
    field_score: float | Unset = UNSET
    field_source: Document | Unset = UNSET
    highlight: SearchHitHighlight | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        field_score = self.field_score

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

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

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if field_id is not UNSET:
            field_dict["_id"] = field_id
        if field_score is not UNSET:
            field_dict["_score"] = field_score
        if field_source is not UNSET:
            field_dict["_source"] = field_source
        if highlight is not UNSET:
            field_dict["highlight"] = highlight

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.document import Document
        from ..models.search_hit_highlight import SearchHitHighlight

        d = dict(src_dict)
        field_id = d.pop("_id", UNSET)

        field_score = d.pop("_score", UNSET)

        _field_source = d.pop("_source", UNSET)
        field_source: Document | Unset
        if isinstance(_field_source, Unset):
            field_source = UNSET
        else:
            field_source = Document.from_dict(_field_source)

        _highlight = d.pop("highlight", UNSET)
        highlight: SearchHitHighlight | Unset
        if isinstance(_highlight, Unset):
            highlight = UNSET
        else:
            highlight = SearchHitHighlight.from_dict(_highlight)

        search_hit = cls(
            field_id=field_id,
            field_score=field_score,
            field_source=field_source,
            highlight=highlight,
        )

        search_hit.additional_properties = d
        return search_hit

    @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
