use super::league_stats::StatDistribution;
use super::weights::StatWeight;

/// Convert a raw stat value to a standardized score (-3 to +3).
///
/// Uses standard deviation thresholds from the league mean.
/// A value of 0 always returns +3 (best possible).
pub fn standardize_value(value: f64, distribution: &StatDistribution, high_is_better: bool) -> i32 {
    if value == 0.0 {
        return 3;
    }

    let avg = distribution.avg;
    let stdev = distribution.stdev;

    let base_score = if value > avg + 2.0 * stdev {
        -3
    } else if value > avg + stdev {
        -2
    } else if value > avg + 0.33 * stdev {
        -1
    } else if value > avg - 0.33 * stdev {
        0
    } else if value > avg - stdev {
        1
    } else if value > avg - 2.0 * stdev {
        2
    } else {
        3
    };

    if high_is_better { -base_score } else { base_score }
}

/// Calculate weighted score for a single stat.
pub fn calculate_weighted_score(
    value: f64,
    distribution: &StatDistribution,
    stat_weight: &StatWeight,
) -> f64 {
    let std_score = standardize_value(value, distribution, stat_weight.high_is_better);
    std_score as f64 * stat_weight.weight as f64
}
