using Elastic.Clients.Elasticsearch;
using Microsoft.Extensions.Diagnostics.HealthChecks;

public sealed class ElasticsearchHealthCheck : IHealthCheck
{
    private readonly ElasticsearchClient _client;

    public ElasticsearchHealthCheck(ElasticsearchClient client) => _client = client;

    public async Task<HealthCheckResult> CheckHealthAsync(
        HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        var response = await _client.PingAsync(cancellationToken);

        if (response.IsValidResponse)
            return HealthCheckResult.Healthy();

        return HealthCheckResult.Unhealthy("Elasticsearch ping failed.");
    }
}
