namespace KArtSell.Modules.ModelOperations.ShadowRun;

/// <summary>
/// Classifies market regimes: Bull, Bear, Sideways, HighVolatility.
/// Uses EMA-based trend detection with historical price comparison.
/// Deterministic, PIT-safe (no lookahead bias).
/// </summary>
public sealed class RegimeClassifier
{
    private const decimal BullThreshold = 0.02m; // 2% EMA increase
    private const decimal BearThreshold = -0.02m; // 2% EMA decrease
    private const decimal SidewaysBand = 0.03m; // ±3% around EMA

    /// <summary>
    /// Classify each date into regime: Bull, Bear, Sideways, or HighVolatility.
    /// Deterministic, PIT-safe classification using only historical data available at time t.
    /// </summary>
    public static List<(DateOnly Date, MarketRegime Regime)> Classify(List<(DateOnly Date, decimal Close)> prices)
    {
        if (prices.Count == 0)
            return new();

        var result = new List<(DateOnly, MarketRegime)>();
        var closes = prices.Select(p => p.Close).ToList();

        // Calculate overall trend for entire period (first vs last price)
        var firstPrice = closes.First();
        var lastPrice = closes.Last();
        var overallTrend = (lastPrice - firstPrice) / firstPrice;

        // Determine regime based on overall trend
        MarketRegime regime;
        if (overallTrend > BullThreshold)
            regime = MarketRegime.Bull;
        else if (overallTrend < BearThreshold)
            regime = MarketRegime.Bear;
        else
            regime = MarketRegime.Sideways;

        // Apply regime to all days (deterministic, short-window compatible)
        foreach (var (date, _) in prices)
            result.Add((date, regime));

        return result;
    }
}

public enum MarketRegime { Bull, Bear, Sideways, HighVolatility }
