using Akka.Actor;
using Akka.TestKit.Xunit2;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
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.Tests.TestSupport;

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

/// <summary>
/// SMS Notifications (S4): at ingest the <see cref="NotificationOutboxActor"/> stamps the
/// new <see cref="Notification"/>'s <see cref="Notification.Type"/> from the target
/// <see cref="NotificationList.Type"/> (the authoritative delivery channel), defaulting to
/// <see cref="NotificationType.Email"/> when the list cannot be resolved. The idempotent
/// insert-if-not-exists / ack-after-persist semantics are unchanged.
/// </summary>
public class NotificationIngestTypeStampingTests : TestKit
{
    private readonly INotificationOutboxRepository _outboxRepository =
        OutboxRepositorySubstitute.Healthy();

    private readonly INotificationRepository _listRepository =
        Substitute.For<INotificationRepository>();

    public NotificationIngestTypeStampingTests()
    {
        // Default: a fresh insert succeeds. Individual tests configure the list lookup.
        _outboxRepository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
            .Returns(true);
    }

    private IServiceProvider BuildServiceProvider()
    {
        var services = new ServiceCollection();
        services.AddScoped(_ => _outboxRepository);
        services.AddScoped(_ => _listRepository);
        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 listName = "ops-team")
    {
        return new NotificationSubmit(
            NotificationId: Guid.NewGuid().ToString(),
            ListName: listName,
            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: null,
            OriginParentExecutionId: null,
            SourceNode: "node-a");
    }

    private void ArrangeList(string name, NotificationType type)
    {
        _listRepository.GetListByNameAsync(name, Arg.Any<CancellationToken>())
            .Returns(new NotificationList(name) { Type = type });
    }

    [Fact]
    public void Ingest_EmailTypedList_StampsEmail()
    {
        ArrangeList("ops-team", NotificationType.Email);
        var submit = MakeSubmit();
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        var ack = ExpectMsg<NotificationSubmitAck>();
        Assert.True(ack.Accepted);
        _outboxRepository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n =>
                n.NotificationId == submit.NotificationId &&
                n.Type == NotificationType.Email),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public void Ingest_SmsTypedList_StampsSms()
    {
        ArrangeList("sms-oncall", NotificationType.Sms);
        var submit = MakeSubmit("sms-oncall");
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        var ack = ExpectMsg<NotificationSubmitAck>();
        Assert.True(ack.Accepted);
        _outboxRepository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n =>
                n.NotificationId == submit.NotificationId &&
                n.Type == NotificationType.Sms),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public void Ingest_MissingList_StampsEmailDefault()
    {
        // GetListByNameAsync returns null (substitute default) — the row is stamped Email
        // and will later park at delivery with "list not found" (unchanged behaviour).
        var submit = MakeSubmit("does-not-exist");
        var actor = CreateActor();

        actor.Tell(submit, TestActor);

        var ack = ExpectMsg<NotificationSubmitAck>();
        Assert.True(ack.Accepted);
        _outboxRepository.Received(1).InsertIfNotExistsAsync(
            Arg.Is<Notification>(n =>
                n.NotificationId == submit.NotificationId &&
                n.Type == NotificationType.Email),
            Arg.Any<CancellationToken>());
    }
}
