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

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

/// <summary>
/// Task 13 (arch-review 04) — verifies that an operator-initiated Retry on a parked
/// notification emits a <see cref="AuditChannel.Notification"/>/<see cref="AuditKind.NotifyDeliver"/>
/// audit row with status <see cref="AuditStatus.Submitted"/> and the operator as
/// <c>Actor</c>, closing the forensic gap (previously the lifecycle read
/// <c>Parked → Attempted → Delivered</c> with no record of who un-parked it). Also
/// asserts the audit write is best-effort — a throwing writer never fails the retry.
/// </summary>
public class NotificationOutboxActorRetryEmissionTests : TestKit
{
    private readonly INotificationOutboxRepository _outboxRepository =
        OutboxRepositorySubstitute.Healthy();

    private readonly RecordingCentralAuditWriter _auditWriter = new();

    private sealed class RecordingCentralAuditWriter : ICentralAuditWriter
    {
        public List<AuditRowProjection.AuditRowValues> Events { get; } = new();
        public Func<AuditEvent, Task>? OnWrite { get; set; }

        public Task WriteAsync(AuditEvent evt, CancellationToken ct = default)
        {
            lock (Events)
            {
                Events.Add(evt.AsRow());
            }

            return OnWrite?.Invoke(evt) ?? Task.CompletedTask;
        }
    }

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

    private IActorRef CreateActor()
    {
        return Sys.ActorOf(Props.Create(() => new NotificationOutboxActor(
            BuildServiceProvider(),
            new NotificationOutboxOptions { DispatchInterval = TimeSpan.FromHours(1) },
            (ICentralAuditWriter)_auditWriter,
            NullLogger<NotificationOutboxActor>.Instance)));
    }

    private static Notification MakeParked(Guid? notificationId = null)
    {
        return new Notification(
            (notificationId ?? Guid.NewGuid()).ToString("D"),
            NotificationType.Email,
            "ops-team",
            "Tank overflow",
            "Tank 3 level critical",
            "site-1")
        {
            Status = NotificationStatus.Parked,
            RetryCount = 5,
            CreatedAt = DateTimeOffset.UtcNow,
        };
    }

    private List<AuditRowProjection.AuditRowValues> EventsByStatus(AuditStatus status)
    {
        lock (_auditWriter.Events)
        {
            return _auditWriter.Events.Where(e => e.Status == status).ToList();
        }
    }

    [Fact]
    public void Retry_OnParked_Emits_Submitted_NotifyDeliver_AuditRow_With_Operator()
    {
        var notificationId = Guid.NewGuid();
        var notification = MakeParked(notificationId);
        _outboxRepository.GetByIdAsync(notification.NotificationId, Arg.Any<CancellationToken>())
            .Returns(notification);
        var actor = CreateActor();

        actor.Tell(new RetryNotificationRequest(
            CorrelationId: "test-corr", NotificationId: notification.NotificationId, RequestedBy: "jdoe"));

        ExpectMsg<RetryNotificationResponse>(r => r.Success);
        AwaitAssert(() =>
        {
            var submitted = EventsByStatus(AuditStatus.Submitted);
            Assert.Single(submitted);
            var evt = submitted[0];
            Assert.Equal(AuditChannel.Notification, evt.Channel);
            Assert.Equal(AuditKind.NotifyDeliver, evt.Kind);
            Assert.Equal("jdoe", evt.Actor);
            Assert.Equal(notificationId, evt.CorrelationId);
        });
    }

    [Fact]
    public void Discard_OnParked_Emits_Discarded_AuditRow_With_Operator()
    {
        var notification = MakeParked();
        _outboxRepository.GetByIdAsync(notification.NotificationId, Arg.Any<CancellationToken>())
            .Returns(notification);
        var actor = CreateActor();

        actor.Tell(new DiscardNotificationRequest(
            CorrelationId: "test-corr", NotificationId: notification.NotificationId, RequestedBy: "jdoe"));

        ExpectMsg<DiscardNotificationResponse>(r => r.Success);
        AwaitAssert(() =>
        {
            var discarded = EventsByStatus(AuditStatus.Discarded);
            Assert.Single(discarded);
            Assert.Equal("jdoe", discarded[0].Actor);
        });
    }

    [Fact]
    public void Retry_AuditFailure_DoesNotFail_TheRetry()
    {
        // Audit is best-effort: a throwing writer must NOT abort the retry — the
        // row is still flipped back to Pending and the response reports Success.
        var notification = MakeParked();
        _outboxRepository.GetByIdAsync(notification.NotificationId, Arg.Any<CancellationToken>())
            .Returns(notification);
        _auditWriter.OnWrite = _ => throw new InvalidOperationException("audit dead");
        var actor = CreateActor();

        actor.Tell(new RetryNotificationRequest(
            CorrelationId: "test-corr", NotificationId: notification.NotificationId, RequestedBy: "jdoe"));

        ExpectMsg<RetryNotificationResponse>(r => r.Success);
        AwaitAssert(() =>
        {
            _outboxRepository.Received(1).UpdateAsync(
                Arg.Is<Notification>(n => n.Status == NotificationStatus.Pending),
                Arg.Any<CancellationToken>());
        });
    }
}
