namespace Modules.User.Application.EventOutbox;

public interface IEventOutboxService
{
    // Task EnqueueAsync(object domainEvent, CancellationToken ct = default);
    Task EnqueueAsync(object payload, string type, CancellationToken ct = default);
}

public class EventOutboxService : IEventOutboxService
{
    private readonly UserDbContext _context;

    public EventOutboxService(UserDbContext context)
    {
        _context = context;
    }

    public async Task EnqueueAsync(object domainEvent, CancellationToken ct = default)
    {
        var message = new OutboxMessage
        {
            Id = Guid.NewGuid(),
            OccurredOn = DateTime.UtcNow,
            Type = domainEvent.GetType().AssemblyQualifiedName!,
            Payload = JsonSerializer.Serialize(domainEvent)
        };

        _context.OutboxMessages.Add(message);

        // Важно: SaveChanges НЕ вызываем — это делает UoW обработчика
        await Task.CompletedTask;
    }
}
