package main

import (
	"context"
	"testing"
	"time"

	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/log"
	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/prometheus"
	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/service"
	"github.com/stretchr/testify/assert"
	"go.uber.org/zap"
)

// Custom test flags to allow passing cluster/time parameters via `go test -args`
// var (
// 	testClusterA = flag.String("clusterA", "", "Cluster A ID")
// 	testClusterB = flag.String("clusterB", "", "Cluster B ID")
// 	testAStart   = flag.String("aStart", "", "Cluster A start time (RFC3339)")
// 	testAEnd     = flag.String("aEnd", "", "Cluster A end time (RFC3339)")
// 	testBStart   = flag.String("bStart", "", "Cluster B start time (RFC3339)")
// 	testBEnd     = flag.String("bEnd", "", "Cluster B end time (RFC3339)")
// 	testAAlias   = flag.String("aAlias", "A", "Cluster A alias")
// 	testBAlias   = flag.String("bAlias", "B", "Cluster B alias")
// )

// TestTotalCPUTimeDay tests getting total CPU time of all SQLs within a day
func TestTotalCPUTimeDay(t *testing.T) {
	Init()
	clusterId := "1379661944646416027"
	// dayStr := "20250802"
	days := []string{
		"20250930",
		// "20250805",
	}
	for _, day := range days {
		queryTime := prometheus.GetDayQueryTime(day)
		interval := "1d"
		totalCpuTime, err := prometheus.GetAllSQLTotalCPUTime2(clusterId, interval, queryTime, prometheus.CpuTimeUnitHour)
		// totalCpuTime, err := prometheus.GetAllSQLTotalCPUTime(clusterId, interval, queryTime, prometheus.CpuTimeUnitHour)
		if err != nil {
			t.Fatalf("GetAllSQLTotalCPUTime failed: %v", err)
		}
		log.PlainInfo("topsql_cpu_time_ms(old)", zap.Float64("totalCpuTime", totalCpuTime))
		// log.PlainInfo("topsql_cpu_time_ms(new)", zap.Float64("totalCpuTime", totalCpuTime2))
	}
}

func TestUpdateCpu(t *testing.T) {
	Init()

	ctx := context.Background()
	log.PlainInfo("Starting Top SQL check...")
	svc := service.NewSlowQueryService()

	err := svc.UpdateSlowQuery(ctx)
	assert.NoError(t, err)
	log.PlainInfo("Top SQL check finished.")
}

func TestGetDigestsCount(t *testing.T) {
	Init()
	ctx := context.Background()
	clusterAId := "1379661944646416018" // saas05-poc
	aInterval := "24h"
	digests := []string{"9505CACB7C710ED17125FCC6CB3669E8DDCA6C8CD8AF6A31F6B3CD64604C3098"}
	// timeStr := "2025-12-18T11:30:00+08:00" // require
	timeStr := "2025-12-13T23:59:59Z" // require

	pcClusterA, err := prometheus.CreatePrometheusClient(ctx, prometheus.CreatePrometheusClientReq{ClusterID: clusterAId})
	if err != nil {
		log.Error(ctx, "Failed to create prod Prometheus client", zap.Error(err), zap.String("clusterId", clusterAId))

	}

	aQueryTime, err := time.Parse(time.RFC3339, timeStr)
	if err != nil {
		log.Error(ctx, "Failed to parse timeStr", zap.Error(err), zap.String("timeStr", timeStr))
		return
	}

	aDigestCount, err := pcClusterA.GetDigestsCount(clusterAId, aQueryTime, aInterval, digests)
	log.Info(ctx, "GetDigestsCount", zap.Any("aDigestCount", aDigestCount), zap.Error(err))
}
