package tidbautoscaling

import (
	pkgmetrics "github.com/tidbcloud/aws-shared-provider/pkg/metrics"
	ep "github.com/tidbcloud/aws-shared-provider/pkg/utils/endpoint"
)

type tokenMetric struct {
	podMetrics
	replicas int
}

func (ta *TidbAutoscaling) convertToTokenMetricMap(metrics podMetrics) map[string]tokenMetric {
	tokenMetricMap := make(map[string]tokenMetric, 2000) //nolint:mnd // no need
	ta.tManager.WalkActiveTokenFromAssignCache(func(token string, endpoints []string, replicas int) bool {
		tMetric := tokenMetric{podMetrics: podMetrics{}, replicas: replicas}
		for _, endpoint := range endpoints {
			podName, _ := ep.ParsePodFromEndpoint(endpoint)
			tMetric.podMetrics[podName] = metrics[podName]
		}
		tokenMetricMap[token] = tMetric
		return true
	})
	pkgmetrics.ActiveTokenNumGauge.WithLabelValues(ta.tidbPoolNamespace).Set(float64(len(tokenMetricMap)))
	return tokenMetricMap
}
