package infra_metrics

import (
	"context"
	"encoding/json"

	"github.com/prometheus/common/model"
	"github.com/tidbcloud/pkgv2/bizerr"
)

type Metric struct {
	Value  model.SamplePair  `json:"value"`
	Labels map[string]string `json:"metric"`
}

type ResponseBody struct {
	Status string     `json:"status"`
	Data   MetricData `json:"data"`
}

type MetricData struct {
	ResultType string   `json:"resultType"`
	Result     []Metric `json:"result"`
}

func DeserializeResponseToMetric(ctx context.Context, resp []byte) ([]Metric, bizerr.BizError) {
	var responseBody ResponseBody
	err := json.Unmarshal(resp, &responseBody)
	if err != nil {
		return nil, bizerr.New(bizerr.ErrCodeJsonMarshal,
			"Fail to deserialize prometheus response body, %s", err.Error())
	}
	return responseBody.Data.Result, nil
}
