using Microsoft.Extensions.Options;
using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;

public static class RabbitMqTestHelper
{
    public static void PurgeNotificationQueues(IOptions<RabbitMqOptions> opts)
    {
        var o = opts.Value;
        var factory = new ConnectionFactory
        {
            HostName = o.Host,
            Port = o.Port,
            UserName = o.Username,
            Password = o.Password,
        };

        using var connection = factory.CreateConnection("test-queue-purge");
        using var channel = connection.CreateModel();

        foreach (var queue in new[]
        {
            "alerts.paging.queue",
            "alerts.paging.dlq",
            "alerts.escalation.queue",
        })
        {
            try
            {
                channel.QueuePurge(queue);
            }
            catch (OperationInterruptedException)
            {
                // Queue may not exist yet on a cold broker.
            }
        }
    }
}
