package metrics

import (
	"github.com/go-logr/logr"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/metrics/collectors"
	"sigs.k8s.io/controller-runtime/pkg/client"
	ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
)

var (
	ClusterCreationDuration = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "infra",
			Subsystem: "cluster",
			Name:      "create_stage_duration",
			Help:      "the time that used in each cluster create stage",
			Buckets:   prometheus.ExponentialBuckets(10, 2, 6), // 10s~640s
		}, []string{"stage"})

	NetworkCreationDuration = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "infra",
			Subsystem: "network",
			Name:      "create_stage_duration",
			Help:      "the time that used in each network create stage",
			Buckets:   prometheus.ExponentialBuckets(10, 2, 7), // 10s~1280s
		}, []string{"stage"})
)

// MustRegister provides a global helper function that registers collectors into a prometheus registry,
// which is backed by the controller-runtime's metrics registry.
func MustRegister(cs ...prometheus.Collector) {
	ctrlmetrics.Registry.MustRegister(cs...)
}

func SetupCollectors(provider string, cli client.Client, logger logr.Logger) {
	MustRegister(collectors.GetAllCollectors(provider, cli, logger)...)
	MustRegister(ClusterCreationDuration, NetworkCreationDuration)
}
