package handler

import (
	"context"

	"github.com/tidbcloud/observability/o11ymetering/pkg/common/constants"
	"github.com/tidbcloud/observability/o11ymetering/pkg/common/models"
	"github.com/tidbcloud/observability/o11ymetering/pkg/facade"
	"github.com/tidbcloud/observability/pkg/common/configs"
)

type DedicatedDataplaneS3Collection struct {
	Env constants.BizEnv
}

func (d DedicatedDataplaneS3Collection) Build(ctx context.Context, period string) (models.StatisticTask, error) {
	return models.StatisticTask{
		Region:         configs.GetServiceInfo().Region,
		StatisticClass: constants.LogsStorageS3,
		MeteringGroup:  constants.DedicatedDataplane,
		Env:            d.Env,
		Period:         period,
		State:          constants.StatePending,
	}, nil
}

//
//var dedicatedDataplaneS3QueryMap = map[constants.StatisticName]string{
//	constants.LogsS3EventsCount:          `sum(label_replace(rate(vector_component_received_events_count_sum{component_type="%s"}),"label_kubernetes_io_hostname", "$1", "host", "(.*)") * on (cluster,label_kubernetes_io_hostname,tenant) group_left(label_tidbcloud_cluster,label_component,label_project,label_tidbcloud_com_component) kube_node_labels{})` + queryByLabels,
//	constants.LogsS3DataTransmittedBytes: `sum(label_replace(rate(vector_component_received_event_bytes_total{component_type="%s"}),"label_kubernetes_io_hostname", "$1", "host", "(.*)") * on (cluster,label_kubernetes_io_hostname,tenant) group_left(label_tidbcloud_cluster,label_component,label_project,label_tidbcloud_com_component) kube_node_labels{})` + queryByLabels,
//	constants.LogsS3FilesCount:           `sum(label_replace(rate(vector_component_sent_events_total{component_type="%s"}),"label_kubernetes_io_hostname", "$1", "host", "(.*)") * on (cluster,label_kubernetes_io_hostname,tenant) group_left(label_tidbcloud_cluster,label_component,label_project,label_tidbcloud_com_component) kube_node_labels{})` + queryByLabels,
//}

var dedicatedDataplaneS3LessLabelQueryMap = map[constants.StatisticName]string{
	constants.LogsS3EventsCount:          `sum(rate(vector_component_received_events_count_sum{component_type="%s", tenant!=""}))` + queryByLabels,
	constants.LogsS3DataTransmittedBytes: `sum(rate(vector_component_received_event_bytes_total{component_type="%s", tenant!=""}))` + queryByLabels,
	constants.LogsS3FilesCount:           `sum(rate(vector_component_sent_events_total{component_type="%s", tenant!=""}))` + queryByLabels,
}

func (d DedicatedDataplaneS3Collection) Execute(ctx context.Context, task models.StatisticTask) ([]*models.StatisticRecord, error) {
	infraClusters, err := facade.GetO11yMetaQuery().ListDedicatedK8sInfraClusters(ctx, task.Env)
	if err != nil {
		return []*models.StatisticRecord{}, err
	}

	var records []*models.StatisticRecord
	for _, cluster := range infraClusters {
		for k, queryTemplate := range dedicatedDataplaneS3LessLabelQueryMap {
			//query := fmt.Sprintf(queryTemplate, convertVendorToProvider(configs.GetServiceInfo().Vendor), configs.GetServiceInfo().Region)
			query := renderS3ComponentType(queryTemplate)
			resp, err := facade.GetO11yMetricsQuery().QueryO11yMetricsInstant(ctx, configs.GetServiceInfo().Vendor, configs.GetServiceInfo().Region, cluster.ID, convertPeriodToTimestamp(task.Period), query, task.Env)

			if err != nil {
				return nil, err
			}
			records = append(records, convertMetricCollectionRespToRecords(k, constants.Logs, resp, task)...)
		}
	}

	return records, nil
}
