//go:build ops
// +build ops

package ops

import (
	"fmt"
	"github.com/tidbcloud/observability/pkg/common/message"
	"os"
	"testing"
	"time"

	"github.com/tidbcloud/observability/test/ft/utils"
	"k8s.io/apimachinery/pkg/util/rand"
)

func TestMain(m *testing.M) {
	os.Environ()
	utils.PrepareConfig("./ops_config.yaml")
	utils.PrepareToken()
	utils.PrepareKubeClient()
	utils.SetRandomID(rand.String(6))
	clearResource()
	prepareResource()
	code := m.Run()
	clearResource()
	os.Exit(code)
}

func clearResource() {
	utils.ClearFtNamespaces()
}

var specTestID string
var storageTestID string
var resourceTestID string
var alertCpuTestID string
var alertMemTestID string
var alertStorageTestID string
var alertMaxSpecTestID string

func prepareResource() {
	fmt.Println("prepareResource start")
	defer fmt.Println("prepareResource end")

	specTest := utils.CreateCluster(utils.HandleWithDefault(func(req message.CreateClusterReq) message.CreateClusterReq {
		req.TenantID = "spectest"
		req.ProjectID = "spectest"
		req.ApplicationID = "spectest"
		return req
	}))
	specTestID = specTest.ID

	storageTest := utils.CreateCluster(utils.HandleWithDefault(func(req message.CreateClusterReq) message.CreateClusterReq {
		req.TenantID = "storagetest"
		req.ProjectID = "storagetest"
		req.ApplicationID = "storagetest"
		return req
	}))
	storageTestID = storageTest.ID

	resourceTest := utils.CreateCluster(utils.HandleWithDefault(func(req message.CreateClusterReq) message.CreateClusterReq {
		req.TenantID = "resourcetest"
		req.ProjectID = "resourcetest"
		req.ApplicationID = "resourcetest"
		return req
	}))
	resourceTestID = resourceTest.ID

	alertCpuTest := utils.CreateCluster(utils.HandleWithDefault(func(req message.CreateClusterReq) message.CreateClusterReq {
		req.TenantID = "alertcputest"
		req.ProjectID = "alertcputest"
		req.ApplicationID = "alertcputest"
		return req
	}))
	alertCpuTestID = alertCpuTest.ID

	alertMemTest := utils.CreateCluster(utils.HandleWithDefault(func(req message.CreateClusterReq) message.CreateClusterReq {
		req.TenantID = "alertmemtest"
		req.ProjectID = "alertmemtest"
		req.ApplicationID = "alertmemtest"
		return req
	}))
	alertMemTestID = alertMemTest.ID

	alertStorageTest := utils.CreateCluster(utils.HandleWithDefault(func(req message.CreateClusterReq) message.CreateClusterReq {
		req.TenantID = "alertstoragetest"
		req.ProjectID = "alertstoragetest"
		req.ApplicationID = "alertstoragetest"
		return req
	}))
	alertStorageTestID = alertStorageTest.ID

	alertMaxSpecTest := utils.CreateCluster(utils.HandleWithDefault(func(req message.CreateClusterReq) message.CreateClusterReq {
		req.TenantID = "alertmaxspectest"
		req.ProjectID = "alertmaxspectest"
		req.ApplicationID = "alertmaxspectest"
		req.MonitorSpecID = "t1g.small"
		return req
	}))
	alertMaxSpecTestID = alertMaxSpecTest.ID

	time.Sleep(40 * time.Second)
}
