using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using RabbitMQ.Client;

public sealed class RabbitMqHealthCheck : IHealthCheck
{
    private readonly RabbitMqOptions _options;

    public RabbitMqHealthCheck(IOptions<RabbitMqOptions> options) => _options = options.Value;

    public async Task<HealthCheckResult> CheckHealthAsync(
        HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        var factory = RabbitMqConnectionFactory.Create(_options);

        using var connection = await Task.Run(() => factory.CreateConnection(), cancellationToken);
        var data = new Dictionary<string, object> { ["endpoint"] = connection.Endpoint.ToString() };
        return HealthCheckResult.Healthy(data: data);
    }
}
