using Akka.Hosting;
using Akka.TestKit.Xunit2;
using Shouldly;
using Xunit;
using ZB.MOM.WW.Audit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Audit;
using ZB.MOM.WW.OtOpcUa.ControlPlane;

namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Audit;

/// <summary>
///     Unit tests for <see cref="ActorAuditWriter" />, the AdminUI-side <see cref="IAuditWriter" />
///     adapter that forwards canonical audit events to the cluster-singleton
///     <see cref="AuditWriterActorKey" /> proxy resolved from the <see cref="ActorRegistry" />.
/// </summary>
public sealed class ActorAuditWriterTests : TestKit
{
    /// <summary>WriteAsync forwards the event verbatim to the registered AuditWriter singleton.</summary>
    [Fact]
    public async Task WriteAsync_tells_the_registered_singleton()
    {
        var probe = CreateTestProbe("audit-writer");
        var registry = new ActorRegistry();
        registry.Register<AuditWriterActorKey>(probe.Ref);

        var writer = new ActorAuditWriter(registry);
        var evt = new AuditEvent
        {
            EventId = Guid.NewGuid(),
            OccurredAtUtc = DateTimeOffset.UtcNow,
            Actor = "alice",
            Action = "Trust",
            Category = "Certificate",
            SourceNode = "AABBCC",
            Outcome = AuditOutcome.Success,
            DetailsJson = "{}",
        };

        await writer.WriteAsync(evt);

        var received = probe.ExpectMsg<AuditEvent>(TimeSpan.FromSeconds(3));
        received.ShouldBe(evt);
    }
}
