using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using ScadaLink.Commons.Messages.Health;
using ScadaLink.Commons.Types.Enums;

namespace ScadaLink.HealthMonitoring.Tests;

public class HealthReportSenderTests
{
    private class FakeTransport : IHealthReportTransport
    {
        public List<SiteHealthReport> SentReports { get; } = [];
        public void Send(SiteHealthReport report) => SentReports.Add(report);
    }

    private class FakeSiteIdentityProvider : ISiteIdentityProvider
    {
        public string SiteId { get; set; } = "test-site";
    }

    [Fact]
    public async Task SendsReportsWithMonotonicSequenceNumbers()
    {
        var transport = new FakeTransport();
        var collector = new SiteHealthCollector();
        collector.SetActiveNode(true);
        var options = Options.Create(new HealthMonitoringOptions
        {
            ReportInterval = TimeSpan.FromMilliseconds(50)
        });

        var sender = new HealthReportSender(
            collector,
            transport,
            options,
            NullLogger<HealthReportSender>.Instance,
            new FakeSiteIdentityProvider { SiteId = "site-A" });

        using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
        try
        {
            await sender.StartAsync(cts.Token);
            await Task.Delay(280, CancellationToken.None);
            await sender.StopAsync(CancellationToken.None);
        }
        catch (OperationCanceledException) { }

        // Should have sent several reports
        Assert.True(transport.SentReports.Count >= 2,
            $"Expected at least 2 reports, got {transport.SentReports.Count}");

        // Verify monotonic sequence numbers starting at 1
        for (int i = 0; i < transport.SentReports.Count; i++)
        {
            Assert.Equal(i + 1, transport.SentReports[i].SequenceNumber);
            Assert.Equal("site-A", transport.SentReports[i].SiteId);
        }
    }

    [Fact]
    public async Task SequenceNumberStartsAtOne()
    {
        var transport = new FakeTransport();
        var collector = new SiteHealthCollector();
        collector.SetActiveNode(true);
        var options = Options.Create(new HealthMonitoringOptions
        {
            ReportInterval = TimeSpan.FromMilliseconds(50)
        });

        var sender = new HealthReportSender(
            collector,
            transport,
            options,
            NullLogger<HealthReportSender>.Instance,
            new FakeSiteIdentityProvider());

        using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(150));
        try
        {
            await sender.StartAsync(cts.Token);
            await Task.Delay(120, CancellationToken.None);
            await sender.StopAsync(CancellationToken.None);
        }
        catch (OperationCanceledException) { }

        Assert.True(transport.SentReports.Count >= 1);
        Assert.Equal(1, transport.SentReports[0].SequenceNumber);
    }

    [Fact]
    public async Task ReportsIncludeUtcTimestamp()
    {
        var transport = new FakeTransport();
        var collector = new SiteHealthCollector();
        collector.SetActiveNode(true);
        var options = Options.Create(new HealthMonitoringOptions
        {
            ReportInterval = TimeSpan.FromMilliseconds(50)
        });

        var sender = new HealthReportSender(
            collector,
            transport,
            options,
            NullLogger<HealthReportSender>.Instance,
            new FakeSiteIdentityProvider());

        var before = DateTimeOffset.UtcNow;
        using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(150));
        try
        {
            await sender.StartAsync(cts.Token);
            await Task.Delay(120, CancellationToken.None);
            await sender.StopAsync(CancellationToken.None);
        }
        catch (OperationCanceledException) { }
        var after = DateTimeOffset.UtcNow;

        Assert.True(transport.SentReports.Count >= 1);
        foreach (var report in transport.SentReports)
        {
            Assert.InRange(report.ReportTimestamp, before, after);
            Assert.Equal(TimeSpan.Zero, report.ReportTimestamp.Offset);
        }
    }

    [Fact]
    public void InitialSequenceNumberIsZero()
    {
        var transport = new FakeTransport();
        var collector = new SiteHealthCollector();
        var options = Options.Create(new HealthMonitoringOptions());

        var sender = new HealthReportSender(
            collector,
            transport,
            options,
            NullLogger<HealthReportSender>.Instance,
            new FakeSiteIdentityProvider());

        Assert.Equal(0, sender.CurrentSequenceNumber);
    }
}
