// SPDX-FileCopyrightText: © 2025 Nøkken.io <nokken.io@proton.me>
// SPDX-License-Identifier: AGPL-3.0
//
// basic_stats.cpp
// Implementation of basic statistical analysis functions
//
#include "health_analytics_engine.h"
#include "utils.h"
/**
 * @brief Calculate basic statistical properties of a data series
 * 
 * @param values Pointer to array of values
 * @param length Number of elements in the array
 * @return BasicStats Structure containing calculated statistics
 */
BasicStats calculate_basic_stats(const double* values, int length) {
    BasicStats stats;
    
    if (length == 0) {
        memset(&stats, 0, sizeof(BasicStats));
        return stats;
    }
    
    // Create a copy for calculations that require sorting
    std::vector<double> sorted(values, values + length);
    std::sort(sorted.begin(), sorted.end());
    
    // Calculate basic statistics
    stats.mean = calculateMean(values, length);
    stats.variance = calculateVariance(values, length, stats.mean);
    stats.stdDev = std::sqrt(stats.variance);
    stats.min = sorted.front();
    stats.max = sorted.back();
    stats.median = (length % 2 == 0) ? 
        (sorted[length/2 - 1] + sorted[length/2]) / 2.0 : sorted[length/2];
    
    // Calculate quartiles and IQR
    stats.q1 = calculateQuantile(sorted.data(), length, 0.25);
    stats.q3 = calculateQuantile(sorted.data(), length, 0.75);
    stats.iqr = stats.q3 - stats.q1;
    
    // Calculate higher-order statistics
    stats.skewness = calculateSkewness(values, length, stats.mean, stats.stdDev);
    stats.kurtosis = calculateKurtosis(values, length, stats.mean, stats.stdDev);
    
    return stats;
}