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.doc_grep_match import DocGrepMatch


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


@_attrs_define
class DocGrepResponse:
    """Response for documentation grep.

    Attributes:
        pattern (str): Search pattern used
        matches (list[DocGrepMatch] | Unset): Matching results
        total_matches (int | Unset): Total number of matches Default: 0.
    """

    pattern: str
    matches: list[DocGrepMatch] | Unset = UNSET
    total_matches: int | Unset = 0
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        matches: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.matches, Unset):
            matches = []
            for matches_item_data in self.matches:
                matches_item = matches_item_data.to_dict()
                matches.append(matches_item)

        total_matches = self.total_matches

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

        return field_dict

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

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

        _matches = d.pop("matches", UNSET)
        matches: list[DocGrepMatch] | Unset = UNSET
        if _matches is not UNSET:
            matches = []
            for matches_item_data in _matches:
                matches_item = DocGrepMatch.from_dict(matches_item_data)

                matches.append(matches_item)

        total_matches = d.pop("total_matches", UNSET)

        doc_grep_response = cls(
            pattern=pattern,
            matches=matches,
            total_matches=total_matches,
        )

        doc_grep_response.additional_properties = d
        return doc_grep_response

    @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
