package register

import (
	"strings"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/tidbcloud/dataflow-service-ng/app/container"
	changefeed_metric "github.com/tidbcloud/dataflow-service-ng/app/metrics/changefeed/metric"
	datapipeline_metric "github.com/tidbcloud/dataflow-service-ng/app/metrics/data_pipeline"
	importinto_metric "github.com/tidbcloud/dataflow-service-ng/app/metrics/import_into"
	premiumdm_metric "github.com/tidbcloud/dataflow-service-ng/app/metrics/premium_dm"
	service_config "github.com/tidbcloud/dataflow-service-ng/internal/config"
)

func RegisterMetrics() {
	prometheus.MustRegister(changefeed_metric.ChangefeedStatus)
	if runsDataPipeline(service_config.Global().DataPipeline) {
		prometheus.MustRegister(datapipeline_metric.NewCollector(container.Global().DataPipelineRepo))
	}
	prometheus.MustRegister(premiumdm_metric.NewCollector(container.Global().PremiumDMDAO))
	prometheus.MustRegister(premiumdm_metric.NewStatusCollector(container.Global().PremiumDMDAO))
	prometheus.MustRegister(importinto_metric.NewCollector(&container.Global().ImportIntoModel))
	prometheus.MustRegister(importinto_metric.NewStatusCollector(&container.Global().ImportIntoModel))
}

// runsDataPipeline reports whether this deployment runs Data Pipeline at all,
// by the same signal the observation worker uses: a configured Lake base URL.
//
// The collector reads the data_pipeline table on every scrape, and a deployment
// that does not run the feature has never migrated that table - so registering
// it there collects nothing and logs a failed query once per scrape, from every
// worker and workflow pod, forever. Metrics for a feature that is switched off
// are noise either way; ones that can only fail are worse than noise, because
// they train everyone to ignore the line that would matter when it is real.
func runsDataPipeline(cfg service_config.DataPipelineConfig) bool {
	return strings.TrimSpace(cfg.LakeBaseURL) != ""
}
