package ddl

import (
	"context"
	"fmt"
	"io"
	"time"

	_ "github.com/go-sql-driver/mysql"
	. "github.com/onsi/gomega"
	"golang.org/x/sync/errgroup"

	. "github.com/pingcap/endless/pkg/util"
	"github.com/pingcap/endless/pkg/workloadnode/cluster"
	"github.com/pingcap/endless/testcase/oltp/oltppkg"
	"github.com/pingcap/log"
	_ "github.com/pingcap/test-infra/sdk/resource/impl/k8s"
)

func OLTP_TRUNCATE_TABLE_FUN_001(
	ctx context.Context,
	tc cluster.BaseCluster,
	pr *io.PipeReader,
	caseCmdParms oltppkg.CaseCmdParms,
) {
	storeSize, sumSize, err := oltppkg.GetStoreSizeByType(tc, "used_size")
	Expect(err).Should(Succeed())
	log.Info(fmt.Sprintf("Store Size and sumSize%v %v before truncate table.", storeSize, sumSize))

	var useDb string
	useDb = caseCmdParms.GetRunDbName()
	g, gctx := errgroup.WithContext(ctx)
	var runTime time.Duration = 3600 * time.Second

	oltppkg.FunCommonCheck(gctx, tc, g, runTime, pr)

	g.Go(WithGinkgoRecover(func() {
		oltppkg.WaitFor(gctx, "Warm Up before truncate table", 10*time.Minute)
		TruncateAllTables(gctx, tc, runTime, useDb)
	}))
	Expect(g.Wait()).Should(BeNil())
}
