package metric

import (
	"fmt"
	"time"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/tidbcloud/dataflow-service/app/repo/entity"
)

const (
	NS                = "dataflow"
	SUBSYSTEM_RESTORE = "restore"

	ClusterIDLabel = "cluster_id"
	RestoreIDLabel = "restore_id"

	WaitMetricScrapeInterval = time.Minute * 2
)

var (
	//nolint:gomnd
	RestoreStatusIntMap = map[entity.RestoreStatus]int{
		entity.RestoreStatusUnspecified: 0,
		entity.RestoreStatusPENDING:     1,
		entity.RestoreStatusRUNNING:     2,
		entity.RestoreStatusFAILED:      3,
		entity.RestoreStatusSUCCESS:     4,
		entity.RestoreStatusFinished:    5,
		entity.RestoreStatusDeleting:    6,
		entity.RestoreStatusDeleted:     7,
	}

	RestoreStatusDesc = prometheus.NewDesc(
		prometheus.BuildFQName(NS, SUBSYSTEM_RESTORE, "status_desc"),
		"the status of restore jobs",
		[]string{ClusterIDLabel, RestoreIDLabel}, nil)

	RestoreStatus = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: NS,
			Subsystem: SUBSYSTEM_RESTORE,
			Name:      "status",
			Help: "restore status: 1 - pending, 2 - running, 3 - failed, 4 - success, 5 - finished, " +
				"6 - deleting, 7 - deleted",
			ConstLabels: nil,
		}, []string{ClusterIDLabel, RestoreIDLabel})
)

func UpdateRestoreStatusMetric(r *entity.Restore) {
	RestoreStatus.WithLabelValues(
		fmt.Sprintf("%d", r.ClusterID),
		fmt.Sprintf("%d", r.ID)).Set(float64(RestoreStatusIntMap[r.Status]))
}
