from src.analysis_functions.descriptive import DescriptiveAnalysis
from src.data_classes.parameters_descriptive_aggregated import (
    DescriptiveAggregatedParameters,
)
from src.data_classes.parameters_descriptive_overview import (
    DescriptiveOverviewParameters,
)
from src.data_classes.parameters_descriptive_percentage_of_dataset_under_condition import (
    DescriptivePercentageOfDatasetUnderConditionParameters,
)

from src.data_classes.parameters_general import GeneralParameters

from src.utils.helper_logging import log_descriptive_analysis_details


def run_descriptive_analysis(
    descriptive: DescriptiveAnalysis, analyses_list: list[GeneralParameters]
):
    """
    Run descriptive analyses based on a list of parameter objects.

    This function iterates over a list of GeneralParameters objects,
    logs the details of each analysis, and then performs the appropriate
    descriptive analysis based on the type of the Parameters object.

    Parameters
    ----------
    descriptive: DescriptiveAnalysis
        An instance of the DescriptiveAnalysis class.
    analyses_list: list[PlotParameters]
        A list of PlotParameters objects.
        Each object contains the parameters for a single visualization.

    Raises
    ------
    ValueError: If the type of the GeneralParameters object is not recognized or not a descriptive analysis.

    Returns
    -------
    None
    """
    for analysis_config in analyses_list:
        log_descriptive_analysis_details(analysis_config)
        if isinstance(analysis_config, DescriptiveAggregatedParameters):
            descriptive.create_descriptive_aggregated_for_metrics(
                data=analysis_config.data,
                variables=analysis_config.variables,
                aggregation_function=analysis_config.aggregation_function,
                group_by=analysis_config.group_by,
                name_save_file=analysis_config.name_save_file,
            )
        elif isinstance(analysis_config, DescriptiveOverviewParameters):
            descriptive.create_descriptives_for_metrics(
                data=analysis_config.data,
                metrics=analysis_config.metrics,
                group_by=analysis_config.group_by,
                name_save_file=analysis_config.name_save_file,
            )
        elif isinstance(
            analysis_config, DescriptivePercentageOfDatasetUnderConditionParameters
        ):
            descriptive.give_percentage_of_dataset_under_condition(
                data=analysis_config.data,
                variable=analysis_config.variable,
                comparison=analysis_config.comparison,
                condition=analysis_config.condition,
                name_save_file=analysis_config.name_save_file,
            )
        else:
            raise ValueError(
                f"Invalid type of descriptive analysis requested {type(analysis_config)}"
            )
