from __future__ import annotations

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

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

from ..types import UNSET, Unset

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


@_attrs_define
class EvaluationConfig:
    """Configuration for an evaluation run.

    Attributes:
        k_values (list[int] | None | Unset): K values for Precision@K, Recall@K, NDCG@K, etc.
        metrics (list[str] | None | Unset): List of metrics to calculate. Available: precision, recall, f1, map, ndcg,
            mrr
    """

    k_values: list[int] | None | Unset = UNSET
    metrics: list[str] | None | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        k_values: list[int] | None | Unset
        if isinstance(self.k_values, Unset):
            k_values = UNSET
        elif isinstance(self.k_values, list):
            k_values = self.k_values

        else:
            k_values = self.k_values

        metrics: list[str] | None | Unset
        if isinstance(self.metrics, Unset):
            metrics = UNSET
        elif isinstance(self.metrics, list):
            metrics = self.metrics

        else:
            metrics = self.metrics

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

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        d = dict(src_dict)

        def _parse_k_values(data: object) -> list[int] | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, list):
                    raise TypeError()
                k_values_type_0 = cast(list[int], data)

                return k_values_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(list[int] | None | Unset, data)

        k_values = _parse_k_values(d.pop("k_values", UNSET))

        def _parse_metrics(data: object) -> list[str] | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, list):
                    raise TypeError()
                metrics_type_0 = cast(list[str], data)

                return metrics_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(list[str] | None | Unset, data)

        metrics = _parse_metrics(d.pop("metrics", UNSET))

        evaluation_config = cls(
            k_values=k_values,
            metrics=metrics,
        )

        evaluation_config.additional_properties = d
        return evaluation_config

    @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
