using Baya.Domain.Entities.Analytics;
using Baya.Infrastructure.Persistence.Services.Analytics;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;

namespace Baya.Test.Foundation.Marketplace;

public sealed class AnalyticsSinkTests
{
    [Fact]
    public async Task Emit_InsertsSystemEventRow()
    {
        using var host = new OpsTestHost();
        host.CurrentUser.UserId = await host.AddUserAsync("actor");
        var sink = new AnalyticsSink(host.Db, host.CurrentUser, host.Clock, NullLogger<AnalyticsSink>.Instance);

        await sink.EmitAsync("nurse_search_performed", new { query = "cardiac" });

        var evt = await host.Db.Set<SystemEvent>().SingleAsync();
        Assert.Equal("nurse_search_performed", evt.Name);
        Assert.Equal(host.CurrentUser.UserId, evt.UserId);
        Assert.Contains("cardiac", evt.PropsJson);
    }
}
