using Akka.Actor;
using Akka.TestKit.Xunit2;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Health;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.Communication.Actors;
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;

namespace ZB.MOM.WW.ScadaBridge.Communication.Tests;

/// <summary>
/// Review 01 [Medium]: site→central health reports were fire-and-forget, so a lost
/// report was invisible to the sender. These tests pin the end-to-end ack: the site actor
/// always replies to the sender (even when the transport cannot forward — a fail-loud failure,
/// not silence), and the central actor processes + acks a report it receives.
/// </summary>
public class HealthReportAckTests : TestKit
{
    private readonly CommunicationOptions _options = new();

    private static SiteHealthReport SampleReport(long seq, string siteId = "site-a") =>
        new(siteId, seq, DateTimeOffset.UtcNow,
            new Dictionary<string, ConnectionHealth>(),
            new Dictionary<string, TagResolutionStatus>(),
            0, 0, new Dictionary<string, int>(), 0, 0, 0, 0);

    [Fact]
    public void SiteCommunicationActor_NoTransport_RepliesFailure_NotSilence()
    {
        // With no transport injected the actor falls back to the fail-loud
        // NoOpCentralTransport, which answers a Status.Failure so the health transport's
        // Ask sees a transient failure rather than hanging. (Production always injects the
        // gRPC GrpcCentralTransport; this pins the wiring-guard fallback.)
        var dmProbe = CreateTestProbe();
        var siteComm = Sys.ActorOf(Props.Create(() =>
            new SiteCommunicationActor("site-a", _options, dmProbe.Ref)));
        siteComm.Tell(SampleReport(seq: 7));
        ExpectMsg<Status.Failure>();
    }

    [Fact]
    public void CentralCommunicationActor_OnReport_ProcessesAndAcks()
    {
        var mockRepo = Substitute.For<ISiteRepository>();
        mockRepo.GetAllSitesAsync(Arg.Any<CancellationToken>()).Returns(new List<Site>());
        var aggregator = Substitute.For<ICentralHealthAggregator>();

        var services = new ServiceCollection();
        services.AddScoped(_ => mockRepo);
        services.AddSingleton(aggregator);
        var sp = services.BuildServiceProvider();

        var transport = Substitute.For<ISiteCommandTransport>();
        var actor = Sys.ActorOf(Props.Create(() => new CentralCommunicationActor(sp, transport)));

        actor.Tell(SampleReport(seq: 3));
        var ack = ExpectMsg<SiteHealthReportAck>();
        Assert.True(ack.Accepted);
        Assert.Equal(3, ack.SequenceNumber);
        AwaitAssert(() => aggregator.Received().ProcessReport(Arg.Is<SiteHealthReport>(r => r.SequenceNumber == 3)));
    }
}
