using Xunit.Abstractions;

namespace QuanTAlib.Tests;

public sealed class FcbValidationTests : IDisposable
{
    private readonly ValidationTestData _testData;
    private readonly ITestOutputHelper _output;
    private bool _disposed;

    public FcbValidationTests(ITestOutputHelper output)
    {
        _output = output;
        _testData = new ValidationTestData();
    }

    public void Dispose() => Dispose(true);

    private void Dispose(bool disposing)
    {
        if (_disposed)
        {
            return;
        }

        _disposed = true;

        if (disposing)
        {
            _testData?.Dispose();
        }
    }

    [Fact]
    public void Validate_ManualCalculation_FractalDetection()
    {
        var series = new TBarSeries();
        var t0 = DateTime.UtcNow;

        // Create clear fractal patterns:
        // Fractal high at bar 1: highs = 100, 120, 110
        // Fractal low at bar 1: lows = 90, 70, 80
        series.Add(new TBar(t0, 0, 100, 90, 95, 100));
        series.Add(new TBar(t0.AddMinutes(1), 0, 120, 70, 95, 100)); // Fractal high=120, low=70
        series.Add(new TBar(t0.AddMinutes(2), 0, 110, 80, 95, 100)); // Confirms fractals

        var ind = new Fcb(3);
        var (mid, up, lo) = ind.Update(series);

        // Upper should be highest fractal high = 120
        // Lower should be lowest fractal low = 70
        // Middle = (120 + 70) / 2 = 95
        Assert.Equal(120.0, up.Last.Value, 1e-10);
        Assert.Equal(70.0, lo.Last.Value, 1e-10);
        Assert.Equal(95.0, mid.Last.Value, 1e-10);

        _output.WriteLine("FCB manual fractal detection validated");
    }

    [Fact]
    public void Validate_AllModes_Consistency()
    {
        int[] periods = { 5, 10, 20, 50 };

        foreach (int period in periods)
        {
            // Batch (instance)
            var inst = new Fcb(period);
            var (bMid, bUp, bLo) = inst.Update(_testData.Bars);

            // Static batch
            var (sMid, sUp, sLo) = Fcb.Batch(_testData.Bars, period);

            ValidationHelper.VerifySeriesEqual(bMid, sMid);
            ValidationHelper.VerifySeriesEqual(bUp, sUp);
            ValidationHelper.VerifySeriesEqual(bLo, sLo);

            // Streaming
            var streaming = new Fcb(period);
            var sMidStream = new TSeries();
            var sUpStream = new TSeries();
            var sLoStream = new TSeries();
            foreach (var bar in _testData.Bars)
            {
                streaming.Update(bar);
                sMidStream.Add(streaming.Last);
                sUpStream.Add(streaming.Upper);
                sLoStream.Add(streaming.Lower);
            }

            ValidationHelper.VerifySeriesEqual(sMid, sMidStream);
            ValidationHelper.VerifySeriesEqual(sUp, sUpStream);
            ValidationHelper.VerifySeriesEqual(sLo, sLoStream);

            // Span
            double[] high = _testData.HighPrices.ToArray();
            double[] low = _testData.LowPrices.ToArray();
            double[] spanMid = new double[high.Length];
            double[] spanUp = new double[high.Length];
            double[] spanLo = new double[high.Length];
            Fcb.Batch(high.AsSpan(), low.AsSpan(),
                spanMid.AsSpan(), spanUp.AsSpan(), spanLo.AsSpan(), period);

            for (int i = 0; i < high.Length; i++)
            {
                Assert.Equal(sMid[i].Value, spanMid[i], 9);
                Assert.Equal(sUp[i].Value, spanUp[i], 9);
                Assert.Equal(sLo[i].Value, spanLo[i], 9);
            }
        }

        _output.WriteLine("FCB mode consistency validated (batch/stream/span)");
    }

    [Fact]
    public void Validate_EventingMode_MatchesBatch()
    {
        const int period = 20;
        var pub = new TBarSeries();
        var evtInd = new Fcb(pub, period);
        var evtMid = new TSeries();
        var evtUp = new TSeries();
        var evtLo = new TSeries();

        foreach (var bar in _testData.Bars)
        {
            pub.Add(bar);
            evtMid.Add(evtInd.Last);
            evtUp.Add(evtInd.Upper);
            evtLo.Add(evtInd.Lower);
        }

        var (bMid, bUp, bLo) = Fcb.Batch(_testData.Bars, period);

        ValidationHelper.VerifySeriesEqual(bMid, evtMid);
        ValidationHelper.VerifySeriesEqual(bUp, evtUp);
        ValidationHelper.VerifySeriesEqual(bLo, evtLo);

        _output.WriteLine("FCB eventing mode validated");
    }

    [Fact]
    public void Validate_Calculate_ReturnsHotIndicator()
    {
        const int period = 15;
        var ((mid, up, lo), ind) = Fcb.Calculate(_testData.Bars, period);

        Assert.True(ind.IsHot);
        Assert.Equal(period + 2, ind.WarmupPeriod); // period + 2 for fractal detection
        Assert.Equal(mid.Last.Value, ind.Last.Value, 1e-10);
        Assert.Equal(up.Last.Value, ind.Upper.Value, 1e-10);
        Assert.Equal(lo.Last.Value, ind.Lower.Value, 1e-10);

        // Continue streaming
        var next = new TBar(DateTime.UtcNow, 0, 150, 50, 100, 1000);
        ind.Update(next);
        Assert.True(ind.IsHot);

        _output.WriteLine("FCB Calculate validated");
    }

    [Fact]
    public void Validate_Prime_MatchesBatch()
    {
        const int period = 25;

        var (bMid, bUp, bLo) = Fcb.Batch(_testData.Bars, period);

        var primed = new Fcb(period);
        var subset = new TBarSeries();
        for (int i = 0; i < 200; i++)
        {
            subset.Add(_testData.Bars[i]);
        }

        primed.Prime(subset);

        for (int i = 200; i < _testData.Bars.Count; i++)
        {
            primed.Update(_testData.Bars[i]);
        }

        Assert.Equal(bMid.Last.Value, primed.Last.Value, 1e-9);
        Assert.Equal(bUp.Last.Value, primed.Upper.Value, 1e-9);
        Assert.Equal(bLo.Last.Value, primed.Lower.Value, 1e-9);

        _output.WriteLine("FCB Prime validated against batch");
    }

    [Fact]
    public void Validate_LargeDataset_FiniteOutputs()
    {
        var (mid, up, lo) = Fcb.Batch(_testData.Bars, 50);

        ValidationHelper.VerifyAllFinite(mid, startIndex: 0);
        ValidationHelper.VerifyAllFinite(up, startIndex: 0);
        ValidationHelper.VerifyAllFinite(lo, startIndex: 0);

        // After warmup, upper should always be >= lower
        int warmup = 50 + 2; // period + 2 for fractal detection
        for (int i = warmup; i < mid.Count; i++)
        {
            Assert.True(up[i].Value >= lo[i].Value, $"Upper >= Lower at {i}");
        }

        _output.WriteLine("FCB large dataset validated");
    }

    [Fact]
    public void Validate_FractalPattern_CorrectDetection()
    {
        // Create a series with known fractal patterns
        var series = new TBarSeries();
        var t0 = DateTime.UtcNow;

        // Pattern designed to have clear fractals:
        // highs: 100, 110, 105 -> fractal high at index 1 = 110
        // lows:   50,  45,  48 -> fractal low at index 1 = 45
        double[] highs = { 100, 110, 105, 108, 115, 112, 118, 125, 120 };
        double[] lows = { 50, 45, 48, 42, 47, 40, 44, 38, 42 };

        for (int i = 0; i < highs.Length; i++)
        {
            series.Add(new TBar(t0.AddMinutes(i), 0, highs[i], lows[i], (highs[i] + lows[i]) / 2, 100));
        }

        var ind = new Fcb(5);
        var (mid, up, lo) = ind.Update(series);

        // All outputs should be finite
        for (int i = 0; i < series.Count; i++)
        {
            Assert.True(double.IsFinite(mid[i].Value), $"Mid finite at {i}");
            Assert.True(double.IsFinite(up[i].Value), $"Upper finite at {i}");
            Assert.True(double.IsFinite(lo[i].Value), $"Lower finite at {i}");
        }

        // After enough bars, upper should be >= lower
        for (int i = 3; i < series.Count; i++)
        {
            Assert.True(up[i].Value >= lo[i].Value, $"Upper >= Lower at {i}");
        }

        _output.WriteLine("FCB fractal pattern validation passed");
    }

    [Fact]
    public void Validate_MiddleIsAverage_AllBars()
    {
        var ind = new Fcb(20);
        var (mid, up, lo) = ind.Update(_testData.Bars);

        for (int i = 0; i < mid.Count; i++)
        {
            double expected = (up[i].Value + lo[i].Value) * 0.5;
            Assert.Equal(expected, mid[i].Value, 1e-10);
        }

        _output.WriteLine("FCB middle = average validated for all bars");
    }

    [Fact]
    public void Validate_BandsAreMonotonic_WithinWindow()
    {
        // The upper band should be the highest fractal high in the window
        // The lower band should be the lowest fractal low in the window
        // These values shouldn't jump erratically unless new fractals are detected

        var ind = new Fcb(10);
        foreach (var bar in _testData.Bars)
        {
            ind.Update(bar);

            // Upper should always be >= Lower
            if (ind.IsHot)
            {
                Assert.True(ind.Upper.Value >= ind.Lower.Value,
                    $"Upper ({ind.Upper.Value}) should be >= Lower ({ind.Lower.Value})");
            }
        }

        _output.WriteLine("FCB band monotonicity validated");
    }
}
