using OmsorgCore.Domain.Enums;
using OmsorgCore.Engine.Events;
using OmsorgCore.Engine.Handlers;
using OmsorgCore.Tests.TestDoubles;

namespace OmsorgCore.Tests.Engine;

public class AuditEventHandlerTests
{
    [Fact]
    public async Task HandleAsync_PersistsAuditEventAsBehavioralEntry()
    {
        var repository = new FakeAuditLogRepository();
        var sut = new AuditEventHandler(repository);
        var userId = Guid.NewGuid();

        var domainEvent = new AuditEvent(userId, "sabina", "127.0.0.1", "Login", "{\"foo\":\"bar\"}");

        await sut.HandleAsync(domainEvent);

        var entry = Assert.Single(repository.Entries);
        Assert.Equal(AuditEventCategory.BehavioralEvent, entry.Category);
        Assert.Equal(userId, entry.ActorUserId);
        Assert.Equal("sabina", entry.ActorUsername);
        Assert.Equal("127.0.0.1", entry.IpAddress);
        Assert.Equal("Login", entry.Action);
        Assert.Equal("{\"foo\":\"bar\"}", entry.Details);
        Assert.Null(entry.EntityType);
        Assert.Null(entry.EntityId);
    }
}
