
using MassTransit;

using Microsoft.AspNetCore.SignalR;

using Monitoring.Notifications.Events;

using Serilog;

namespace Monitoring.Notifications;

public sealed class NotificationSentConsumer(
	IHubContext<NotificationHub> hubContext
) : IConsumer<NotificationSent>
{
	public async Task Consume(ConsumeContext<NotificationSent> context)
	{
		var notificationEvent = context.Message;

		Log.Information(
			"NotificationSent - {NotificationType} - {UserIds}",
			notificationEvent.NotificationType,
			string.Join(", ", notificationEvent.UserIds));

		await hubContext
			.Clients
			.Users(notificationEvent.UserIds.Select(x => x.ToString()))
			.SendAsync(notificationEvent.NotificationType, notificationEvent.Model);

		await context.RespondAsync(new NotificationSentResponse());
	}
}