package scenario

import (
	"context"
	"time"

	"github.com/pingcap/log"
	"github.com/pingcap/test-infra/sdk/resource"
	"golang.org/x/sync/errgroup"

	. "github.com/pingcap/endless/pkg/matcher"
	"github.com/pingcap/endless/testcase/oltp/oltppkg"
)

func PreCheck(rootCtx context.Context, tc resource.TiDBCluster) {
	log.Info("Check the cluster health status before running the case")
	oltppkg.CheckClusterHealth(tc)

	log.Info("Waiting for cluster balance before running the case.")
	// TODO: well-planned balanced detection.
	balanceWaitTime := 3 * time.Hour
	threshhold := 0.5
	pollingInterval := 5 * time.Minute
	oltppkg.WaitForBalance(rootCtx, tc, balanceWaitTime, pollingInterval, threshhold)
}

func PostCheck(rootCtx context.Context, tc resource.TiDBCluster) {
	log.Info("Check the cluster balance state after the case ends.")
	balanceCheckTime := 2 * time.Minute
	threshhold := 0.3
	g, gctx := errgroup.WithContext(rootCtx)
	oltppkg.BalanceCheckAftWl(gctx, tc, g, balanceCheckTime, threshhold)
	Try(g.Wait())
}
