<?php

namespace FleekDash\Controllers\Stats;

/**
 * Stats Helpers
 *
 * Utility methods for statistics operations
 *
 * @package FleekDash\Controllers\Stats
 */
class StatsHelpers
{
    /**
     * Resample data points to reduce dataset size for charts
     *
     * @param array $labels Original labels
     * @param array $values Original values
     * @param int $max_points Maximum points to return
     * @return array Resampled data with labels and values
     */
    public static function resampleDataPoints($labels, $values, $max_points)
    {
        $total_points = count($labels);

        if ($total_points <= $max_points) {
            return ['labels' => $labels, 'values' => $values];
        }

        $factor = ceil($total_points / $max_points);

        $resampled_labels = [];
        $resampled_values = [];

        for ($i = 0; $i < $total_points; $i += $factor) {
            $bucket_sum = 0;
            $bucket_count = 0;
            $bucket_label = $labels[$i];

            for ($j = 0; $j < $factor && ($i + $j) < $total_points; $j++) {
                $bucket_sum += $values[$i + $j];
                $bucket_count++;
            }

            $avg_value = ($bucket_count > 0) ? round($bucket_sum / $bucket_count) : 0;

            $resampled_labels[] = $bucket_label;
            $resampled_values[] = $avg_value;
        }

        return [
            'labels' => $resampled_labels,
            'values' => $resampled_values
        ];
    }

    /**
     * Check permissions - always return true
     * Let AuthMiddleware handle authentication
     */
    public static function checkPermissions()
    {
        return true;
    }
}
