package main

import (
	"context"
	"fmt"
	"testing"
	"time"

	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/common"
	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/config"
	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/dao"
	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/log"
	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/model"
	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/service"
	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/thirdapi/feishu"
	"github.com/stretchr/testify/assert"
	"go.uber.org/zap"
)

func TestCheckTikvIn30Min(t *testing.T) {
	ctx := log.StartTrace("TestCheckTikvIn30Min", "")
	defer log.EndTrace(ctx)
	runCheckTikvIn30Min(ctx)
	runTikvHighCpuIn30Min(ctx)

}
func TestCreateTable(t *testing.T) {
	// 1. Initialize database connection
	Init()
	// dao.InitMySqlDB()
	db := dao.InitMySqlDB()
	assert.NotNil(t, db)

	// Drop table if it exists to ensure a clean state
	err := db.Migrator().DropTable(
		&model.TikvHighCPUWindow{},
	)
	assert.NoError(t, err)

	// 2. Auto migrate the CntDigest model to create table
	err = db.AutoMigrate(
		&model.TikvHighCPUWindow{},
	)
	assert.NoError(t, err)
}

func TestGetTopCpuTikvDigests(t *testing.T) {
	Init()
	org := config.OrgsConfig.Orgs[0]
	dbPath := config.GetDbPath(org.OrgID)
	log.PlainInfo("dbPath:" + dbPath)
	// Initialize database manager, todo remove

	db, err := dao.InitSQLiteDB(dbPath)

	dao := common.NewCommonDAOWithDB(db)
	slowQueryLogMax := model.SlowQueryLog{}
	// digest := "1a96197a635ab3d1c0fc027ce178d8aa751e4bd033c39db6163d3ba75b49fe3f"
	param := common.ComFilter{
		ModelFilters: model.SlowQueryLog{

			FormattedTimestamp: "1760182386.785547",
		},
	}
	ctx := context.Background()
	err = dao.GetEntity(ctx, param, &slowQueryLogMax)
	if err != nil {
		log.Error(ctx, "enrichTop10Result failed", zap.Error(err))

	}
	log.PlainInfo("GetTopCpuTikvDigests", zap.Any("slowQueryLogMax", slowQueryLogMax))
}

func runCheckTikvIn30Min(ctx context.Context) {
	// ctx := context.Background()
	Init()
	// begin := time.Now()

	sartTime := time.Now()
	sartTime = sartTime.Add(-30 * time.Minute)
	log.Info(ctx, "TestCheckTikvIn30Min sart", zap.String("sartTime", sartTime.Format(time.RFC3339)))
	svc := service.NewTikvLatencyService()
	err := svc.QueryTikvDuration30Min(ctx, sartTime)

	if err != nil {
		log.Error(ctx, "TestCheckTikvIn30Min failed", zap.Error(err))
		if config.IsProd() {
			feishu.SendMessageWithStr(fmt.Sprintf("TestCheckTikvIn30Min failed, err: %v", err))
		}
		return
	}

	log.Info(ctx, "TestCheckTikvIn30Min end", zap.String("sartTime", sartTime.Format(time.RFC3339)),
		zap.Any("err", err), zap.Duration("cost", time.Duration(time.Since(sartTime).Minutes())))
}

// func TestLocalCheckTikvIn30Min(t *testing.T) {
// 	ctx := context.Background()
// 	Init()

// 	clusterId := "1379661944646414653"
// 	// UTC 时间字符串
// 	// tStr := "2026-01-01T19:00:00+08:00"
// 	// sartTime, _ := time.Parse(time.RFC3339, tStr)

// 	sartTime := time.Now()
// 	sartTime = sartTime.Add(-40 * time.Minute)

// 	log.Info(ctx, "TestCheckTikvIn30Min sart", zap.String("sartTime", sartTime.Format(time.RFC3339)))
// 	svc := service.NewTikvLatencyService()
// 	err := svc.QueryTikvDuration30Min(ctx, sartTime, clusterId)

// 	assert.NoError(t, err)
// 	// if err != nil {
// 	// 	log.Error(ctx, "TestCheckTikvIn30Min failed", zap.Error(err))
// 	// 	feishu.SendMessageWithStr(fmt.Sprintf("TestCheckTikvIn30Min failed, err: %v", err))
// 	// 	return
// 	// }
// 	log.Info(ctx, "TestCheckTikvIn30Min end", zap.String("sartTime", sartTime.Format(time.RFC3339)))
// }

func TestTikvHighCpuIn30Min(t *testing.T) {
	ctx := log.StartTrace("TestCheckTikvIn30Min", "")
	defer log.EndTrace(ctx)
	runTikvHighCpuIn30Min(ctx)
}
func TestLocalTikvHighCpuIn30Min(t *testing.T) {
	ctx := log.StartTrace("TestCheckTikvIn30Min", "")
	defer log.EndTrace(ctx)
	clusterId := "10989049060142230334"
	runTikvHighCpuIn30Min(ctx, clusterId)
}

// func TestStartTraceHasTraceId(t *testing.T) {
// 	ctx := log.StartTrace("TestStartTraceHasTraceId", "")
// 	defer log.EndTrace(ctx)
// 	assert.NotEmpty(t, log.GeTraceId(ctx))
// }

func runTikvHighCpuIn30Min(ctx context.Context, clusterId ...string) {

	// ctx := context.Background()
	Init()
	sartTime := time.Now()
	sartTime = sartTime.Add(-30 * time.Minute)
	log.Info(ctx, "TestTikvHighCpuIn30Min sart", zap.String("sartTime", sartTime.Format(time.RFC3339)))
	svc := service.NewTikvHighCpuService()
	err := svc.QueryTikvHighCpu30Min(ctx, sartTime, clusterId...)

	if err != nil {
		log.Error(ctx, "TestCheckTikvIn30Min failed", zap.Error(err))
		if config.IsProd() {
			feishu.SendMessageWithStr(fmt.Sprintf("TestCheckTikvIn30Min failed, err: %v", err))
		}
		return
	}
	log.Info(ctx, "runTikvHighCpuIn30Min end", zap.String("sartTime", sartTime.Format(time.RFC3339)),
		zap.Any("err", err), zap.Duration("cost", time.Duration(time.Since(sartTime).Minutes())))

}
