using System.Text.Json;
using CommunityToolkit.HighPerformance;
using RabbitMQ.Client.Events;

namespace e_suite.MessageProcessor.Handlers;

public abstract class QueueHandlerBase
{
    protected static async Task<T> TranslateMessage<T>(BasicDeliverEventArgs basicDeliverEventArgs)
    {
        var stream = basicDeliverEventArgs.Body.AsStream();
        var message = await JsonSerializer.DeserializeAsync<T>(stream) ?? throw new NullReferenceException("Unable to deserialise message");
        return message;
    }

    public abstract Task OnReceived(string queueName, BasicDeliverEventArgs basicDeliverEventArgs);
}