using Akka.Actor;
using Akka.TestKit.Xunit2;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Notification;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.NotificationOutbox.Delivery;
using ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests.TestSupport;

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

/// <summary>
/// Task 13: Tests for the <see cref="NotificationOutboxActor"/> ingest path — building a
/// <see cref="Notification"/> from a <see cref="NotificationSubmit"/>, persisting it via
/// <see cref="INotificationOutboxRepository.InsertIfNotExistsAsync"/>, and acking the sender.
/// </summary>
public class NotificationOutboxActorIngestTests : TestKit
{
    private readonly INotificationOutboxRepository _repository =
        OutboxRepositorySubstitute.Healthy();

    private IServiceProvider BuildServiceProvider()
    {
        var services = new ServiceCollection();
        services.AddScoped(_ => _repository);
        return services.BuildServiceProvider();
    }

    private IActorRef CreateActor()
    {
        return Sys.ActorOf(Props.Create(() => new NotificationOutboxActor(
            BuildServiceProvider(),
            new NotificationOutboxOptions(),
            new NoOpCentralAuditWriter(),
            NullLogger<NotificationOutboxActor>.Instance)));
    }

    private static NotificationSubmit MakeSubmit(
        string? notificationId = null,
        Guid? originExecutionId = null,
        Guid? originParentExecutionId = null,
        string? sourceNode = null)
    {
        return new NotificationSubmit(
            NotificationId: notificationId ?? Guid.NewGuid().ToString(),
            ListName: "ops-team",
            Subject: "Tank overflow",
            Body: "Tank 3 level critical",
            SourceSiteId: "site-1",
            SourceInstanceId: "instance-42",
            SourceScript: "AlarmScript",
            SiteEnqueuedAt: new DateTimeOffset(2026, 5, 19, 8, 30, 0, TimeSpan.Zero),
            OriginExecutionId: originExecutionId,
            OriginParentExecutionId: originParentExecutionId,
            SourceNode: sourceNode);
    }

    [Fact]
    public void NotificationSubmit_PersistsMappedNotification_AndAcksAccepted()
    {
        _repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .Returns(true);
        var submit = MakeSubmit();
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        var ack = ExpectMsg<NotificationSubmitAck>();
        Assert.Equal(submit.NotificationId, ack.NotificationId);
        Assert.True(ack.Accepted);
        Assert.Null(ack.Error);

        _repository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n =>
                n.NotificationId == submit.NotificationId &&
                n.Type == NotificationType.Email &&
                n.ListName == submit.ListName &&
                n.Subject == submit.Subject &&
                n.Body == submit.Body &&
                n.SourceSiteId == submit.SourceSiteId &&
                n.SourceInstanceId == submit.SourceInstanceId &&
                n.SourceScript == submit.SourceScript &&
                n.SiteEnqueuedAt == submit.SiteEnqueuedAt &&
                n.Status == NotificationStatus.Pending &&
                n.CreatedAt != default),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public void NotificationSubmit_CopiesOriginExecutionId_OntoPersistedNotification()
    {
        // Audit Log #23: the originating script execution's id rides on the
        // NotificationSubmit and must be persisted on the Notification row so
        // the dispatcher can later echo it onto NotifyDeliver audit rows.
        _repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .Returns(true);
        var executionId = Guid.NewGuid();
        var submit = MakeSubmit(originExecutionId: executionId);
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        ExpectMsg<NotificationSubmitAck>();
        _repository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n => n.OriginExecutionId == executionId),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public void NotificationSubmit_NullOriginExecutionId_PersistsNull()
    {
        _repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .Returns(true);
        var submit = MakeSubmit(originExecutionId: null);
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        ExpectMsg<NotificationSubmitAck>();
        _repository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n => n.OriginExecutionId == null),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public void NotificationSubmit_CopiesOriginParentExecutionId_OntoPersistedNotification()
    {
        // Audit Log ParentExecutionId: the routed run's parent ExecutionId rides
        // on the NotificationSubmit and must be persisted on the Notification row
        // so the dispatcher can later echo it onto NotifyDeliver audit rows.
        _repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .Returns(true);
        var parentExecutionId = Guid.NewGuid();
        var submit = MakeSubmit(originParentExecutionId: parentExecutionId);
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        ExpectMsg<NotificationSubmitAck>();
        _repository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n => n.OriginParentExecutionId == parentExecutionId),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public void NotificationSubmit_NullOriginParentExecutionId_PersistsNull()
    {
        _repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .Returns(true);
        var submit = MakeSubmit(originParentExecutionId: null);
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        ExpectMsg<NotificationSubmitAck>();
        _repository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n => n.OriginParentExecutionId == null),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public void DuplicateSubmit_RepositoryReturnsFalse_StillAcksAccepted()
    {
        _repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .Returns(false);
        var submit = MakeSubmit();
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        var ack = ExpectMsg<NotificationSubmitAck>();
        Assert.Equal(submit.NotificationId, ack.NotificationId);
        Assert.True(ack.Accepted);
        Assert.Null(ack.Error);
    }

    [Fact]
    public void RepositoryThrows_AcksNotAcceptedWithError()
    {
        _repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .ThrowsAsync(new InvalidOperationException("database unavailable"));
        var submit = MakeSubmit();
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        var ack = ExpectMsg<NotificationSubmitAck>();
        Assert.Equal(submit.NotificationId, ack.NotificationId);
        Assert.False(ack.Accepted);
        Assert.NotNull(ack.Error);
        Assert.Contains("database unavailable", ack.Error);
    }

    [Fact]
    public void NotificationSubmit_CopiesSourceNode_OntoPersistedNotification()
    {
        // SourceNode-stamping (Task 13): the originating site's node name (node-a/node-b)
        // rides on the NotificationSubmit and must be persisted on the Notification row so
        // central observers (KPIs, audit drill-ins, ops dashboards) can see which node
        // emitted the notification.
        _repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .Returns(true);
        var submit = MakeSubmit(sourceNode: "node-a");
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        ExpectMsg<NotificationSubmitAck>();
        _repository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n => n.SourceNode == "node-a"),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public void NotificationSubmit_NullSourceNode_PersistsNull()
    {
        // Submissions from a host that didn't wire INodeIdentityProvider, or from
        // pre-SourceNode-stamping clients, carry null SourceNode — the central row must
        // persist NULL rather than fall back to a placeholder.
        _repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .Returns(true);
        var submit = MakeSubmit(sourceNode: null);
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        ExpectMsg<NotificationSubmitAck>();
        _repository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n => n.SourceNode == null),
            Arg.Any<CancellationToken>());
    }
}
