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

if TYPE_CHECKING:
    from ..models.fdc_query_foods import FdcQueryFoods


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


@_attrs_define
class FdcQuery:
    """
    Attributes:
        total_hits (int):
        current_page (int):
        total_pages (int):
        foods (list[FdcQueryFoods]):
    """

    total_hits: int
    current_page: int
    total_pages: int
    foods: list[FdcQueryFoods]
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        current_page = self.current_page

        total_pages = self.total_pages

        foods = []
        for foods_item_data in self.foods:
            foods_item = foods_item_data.to_dict()
            foods.append(foods_item)

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "totalHits": total_hits,
                "currentPage": current_page,
                "totalPages": total_pages,
                "foods": foods,
            }
        )

        return field_dict

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

        d = dict(src_dict)
        total_hits = d.pop("totalHits")

        current_page = d.pop("currentPage")

        total_pages = d.pop("totalPages")

        foods = []
        _foods = d.pop("foods")
        for foods_item_data in _foods:
            foods_item = FdcQueryFoods.from_dict(foods_item_data)

            foods.append(foods_item)

        fdc_query = cls(
            total_hits=total_hits,
            current_page=current_page,
            total_pages=total_pages,
            foods=foods,
        )

        fdc_query.additional_properties = d
        return fdc_query

    @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
