package tech.easyflow.common.analyticaldb.support;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Component;
import tech.easyflow.common.analyticaldb.config.AnalyticalDBFlywayProperties;
import tech.easyflow.common.analyticaldb.core.AnalyticalDBOperations;

@Component
public class AnalyticalDBHealthSupport {

    private final ObjectProvider<AnalyticalDBOperations> analyticalDBOperationsProvider;
    private final AnalyticalDBFlywayProperties flywayProperties;

    public AnalyticalDBHealthSupport(ObjectProvider<AnalyticalDBOperations> analyticalDBOperationsProvider,
                                     AnalyticalDBFlywayProperties flywayProperties) {
        this.analyticalDBOperationsProvider = analyticalDBOperationsProvider;
        this.flywayProperties = flywayProperties;
    }

    public boolean enabled() {
        return analyticalDBOperationsProvider.getIfAvailable() != null;
    }

    public void selfCheck() {
        AnalyticalDBOperations operations = analyticalDBOperationsProvider.getIfAvailable();
        if (operations == null) {
            return;
        }
        operations.queryOne("SELECT 1", Integer.class);
        operations.queryOne("SELECT COUNT(1) FROM " + flywayProperties.getTable(), Long.class);
    }
}
