"""
This module implements a trivial random valuation method.

It exists mainly for convenience when running experiments, e.g. when comparing methods
with [point removal][pydvl.reporting.point_removal], random values are a simple (albeit
weak) baseline.
"""

from __future__ import annotations

import numpy as np
from typing_extensions import Self

from pydvl.utils import Seed
from pydvl.valuation.base import Valuation
from pydvl.valuation.dataset import Dataset
from pydvl.valuation.result import ValuationResult


class RandomValuation(Valuation):
    """
    A trivial valuation method that assigns random values to each data point.

    Values are in the range [0, 1), as generated by
    [ValuationResult.from_random][pydvl.valuation.result.ValuationResult.from_random].

    Successive calls to [fit()][pydvl.valuation.base.Valuation.fit] will generate
    different values.

    Args:
        random_state: Random seed for reproducibility.
    """

    algorithm_name: str = "random"

    def __init__(self, random_state: Seed):
        super().__init__()
        self.random_state = np.random.default_rng(random_state)

    def fit(self, data: Dataset, continue_from: ValuationResult | None = None) -> Self:
        """Dummy fitting that generates a set of random values.

        Successive calls will generate different values.

        Args:
            data: used to determine the size of the valuation result
            continue_from: (For consistency with other valuation methods) If this
                argument provided, the result is initialized with this object. The
                random values are added to the existing values.
        """
        self._result = self._init_or_check_result(data, continue_from)
        self._result += ValuationResult.from_random(
            size=len(data), seed=self.random_state, algorithm=str(self)
        )
        return self
