<?php

namespace App\Services;

/**
 * Exponentially-weighted moving average. Pure function — used by
 * BrentPricePredictor for the EWMA fallback prediction and by
 * AnthropicPredictionProvider to enrich the basic-flow prompt.
 */
final class Ewma
{
    public const float DEFAULT_ALPHA = 0.3;

    /** @param  float[]  $prices  Chronological order (oldest first). */
    public static function compute(array $prices, float $alpha = self::DEFAULT_ALPHA): float
    {
        $ema = $prices[0];

        foreach (array_slice($prices, 1) as $price) {
            $ema = $alpha * $price + (1 - $alpha) * $ema;
        }

        return round($ema, 4);
    }
}
