package tech.easyflow.common.analyticaldb.support;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

/**
 * 分析数据库健康检查。
 */
@Component("analyticalDbHealthIndicator")
public class AnalyticalDBHealthIndicator implements HealthIndicator {

    private final AnalyticalDBHealthSupport healthSupport;

    /**
     * 创建分析数据库健康检查器。
     *
     * @param healthSupport 分析数据库健康检查支持
     */
    public AnalyticalDBHealthIndicator(AnalyticalDBHealthSupport healthSupport) {
        this.healthSupport = healthSupport;
    }

    /**
     * 检查分析数据库是否可用。
     *
     * @return 健康状态
     */
    @Override
    public Health health() {
        if (!healthSupport.enabled()) {
            return Health.up().withDetail("enabled", false).build();
        }
        try {
            healthSupport.selfCheck();
            return Health.up().withDetail("enabled", true).build();
        } catch (Exception e) {
            return Health.down(e).withDetail("enabled", true).build();
        }
    }
}
