package metrics

import (
	"github.com/prometheus/client_golang/prometheus"

	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
)

type InfraProberMetrics struct {
	DescClusterExist  *prometheus.Desc
	DescClusterNormal *prometheus.Desc
}

func NewInfraProberMetrics(clusterID, name, ns string) *InfraProberMetrics {
	m := &InfraProberMetrics{}

	constLabels := map[string]string{
		"cluster_id":   clusterID,
		"cluster_name": name,
		"cluster_ns":   ns,
	}
	m.DescClusterExist = prometheus.NewDesc(
		m.fqName("cluster_exist"),
		"Whether the cluster cr is exist.",
		nil,
		constLabels,
	)
	m.DescClusterNormal = prometheus.NewDesc(
		m.fqName("cluster_normal"),
		"Whether the cluster phase is normal.",
		[]string{"cluster_phase"},
		constLabels,
	)

	return m
}

func (m *InfraProberMetrics) Desc() []*prometheus.Desc {
	return []*prometheus.Desc{
		m.DescClusterExist,
		m.DescClusterNormal,
	}
}

func (m *InfraProberMetrics) ClusterExist(exist bool) prometheus.Metric {
	return prometheus.MustNewConstMetric(
		m.DescClusterExist,
		prometheus.GaugeValue, GaugeBool(exist),
	)
}

func (m *InfraProberMetrics) ClusterNormal(status cloudv1.ClusterPhase) prometheus.Metric {
	return prometheus.MustNewConstMetric(
		m.DescClusterNormal,
		prometheus.GaugeValue, GaugeBool(status == cloudv1.ClusterPhaseRunning),
		string(status),
	)
}

func (m *InfraProberMetrics) fqName(name string) string {
	return prometheus.BuildFQName(Namespace, InfraProberSubsystem, name)
}
