using Microsoft.Extensions.Diagnostics.HealthChecks;
using StackExchange.Redis;

public sealed class RedisHealthCheck : IHealthCheck
{
    private readonly IConnectionMultiplexer _redis;

    public RedisHealthCheck(IConnectionMultiplexer redis) => _redis = redis;

    public async Task<HealthCheckResult> CheckHealthAsync(
        HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        var db = _redis.GetDatabase();
        var latency = await db.PingAsync();
        var data = new Dictionary<string, object> { ["ping_ms"] = latency.TotalMilliseconds };
        return HealthCheckResult.Healthy(data: data);
    }
}
