using Microsoft.Extensions.Options;
using RabbitMQ.Client;
using TakeoutSaaS.Module.Messaging.Options;

namespace TakeoutSaaS.Module.Messaging.Services;

/// <summary>
/// RabbitMQ 连接工厂封装。
/// </summary>
public sealed class RabbitMqConnectionFactory(IOptionsMonitor<RabbitMqOptions> optionsMonitor)
{
    /// <summary>
    /// 创建连接。
    /// </summary>
    public Task<IConnection> CreateConnectionAsync(CancellationToken cancellationToken = default)
    {
        var options = optionsMonitor.CurrentValue;
        var factory = new ConnectionFactory
        {
            HostName = options.Host,
            Port = options.Port,
            UserName = options.Username,
            Password = options.Password,
            VirtualHost = options.VirtualHost
        };

        return factory.CreateConnectionAsync(cancellationToken);
    }
}
