using System.Text.Json; using UsingCash.Database.Repositories; namespace UsingCash.Logic.Support; public class EventBus { private readonly SupportRepository _supportRepo; public EventBus(SupportRepository supportRepo) { _supportRepo = supportRepo; } public async Task PublishAsync(string eventType, T payload) where T : class { var json = JsonSerializer.Serialize(payload); return await _supportRepo.EnqueueEvent(eventType, json); } public async Task> ConsumeAsync(int limit = 100) where T : class { var events = await _supportRepo.GetPendingEvents(limit); var results = new List<(int, string, T?)>(); foreach (var (id, type, payloadJson) in events) { T? payload = null; try { payload = JsonSerializer.Deserialize(payloadJson); } catch { /* log and continue */ } results.Add((id, type, payload)); } return results; } public async Task CompleteAsync(int eventId) => await _supportRepo.SetEventStatus(eventId, "completed"); public async Task FailAsync(int eventId) => await _supportRepo.SetEventStatus(eventId, "failed"); public async Task PurgeOldAsync(int retentionDays = 30) => await _supportRepo.PurgeOldEvents(retentionDays); }