from typing import Iterable
from pathlib import Path

from attrs import field, define
from attrs.validators import instance_of, deep_iterable, optional
from attr import attrib

from src.data_classes.parameters_general import GeneralParameters
from src.utils.helper_conversion import create_dictionary_of_aggregation_methods


@define
class RegressionParameters(GeneralParameters):
    dependent_variable: str = field(validator=instance_of(str))
    independent_variables: list[str] = field(
        validator=deep_iterable(
            member_validator=instance_of(str), iterable_validator=instance_of(Iterable)
        ),
    )


@define
class BayesianRegressionParameters(RegressionParameters):
    pass


@define
class LinearRegressionParameters(RegressionParameters):
    standardize: bool = field(default=False, validator=instance_of(bool))
    report_effect_size: bool = field(
        default=False, validator=optional(instance_of(bool))
    )


@define
class GroupedLinearRegressionParameters(RegressionParameters):
    aggregation_functions: list[str] = field(
        default=[],
        validator=deep_iterable(
            member_validator=instance_of(str), iterable_validator=instance_of(list)
        ),
    )
    group_by: str = field(default="sum", validator=instance_of(str))
    standardize: bool = field(default=False, validator=instance_of(bool))
    report_effect_size: bool = field(
        default=False, validator=optional(instance_of(bool))
    )
    print_detailed_coefficients: bool = field(
        default=False, validator=optional(instance_of(bool))
    )
    dictionary_aggregation_methods = attrib(init=False)

    def __attrs_post_init__(self):
        self.name_save_file = Path(f"{self.name}")
        self.dictionary_aggregation_methods: dict = (
            create_dictionary_of_aggregation_methods(
                self.independent_variables,
                self.dependent_variable,
                self.aggregation_functions,
            )
        )
