package testcase

import (
	"time"

	"github.com/pingcap/test-infra/caselib/pkg/model/ticdc"
	"github.com/pingcap/test-infra/caselib/pkg/model/workload"
	"github.com/pingcap/test-infra/caselib/pkg/steps"
	"github.com/pingcap/test-infra/caselib/ticdc/common"
)

// CdcMaxIncrementalSync
// Init CDC Components: tidb cluster / sink (mysql)
// Create a changefeed
// Init Workload: tpcc
// Stop the changefeed, and then sleep
// Resume the changefeed
// Wait workload finish: even though workload finished, but it does not mean changefeed has synced all data
// Check consistency.
var _ = common.AddTestCase("cdc_3d_sync", func() steps.Step {
	var (
		loopLifeTime                     = 60 * time.Hour
		loopCron                         = 12 * time.Hour
	)
	return steps.
		NewBuilder().
		LoadEnv().
		LoadResourceFromEnv("TI_PARAM_RES_upstream", common.ResourceHandleSource).
		LoadResourceFromEnv("TI_PARAM_RES_downstream", common.ResourceHandleMySQL).
		LoadResourceFromEnv("TI_PARAM_RES_workload", common.ResourceHandleWorkload).
		InitCDCComponents(common.ResourceHandleSource, common.ResourceHandleMySQL).
		OperateChangeFeed(&ticdc.ChangeFeedCreateFlags{SinkType: ticdc.SinkTypeMySQL, ID: "cdc-3d-sync"}).
		CreateDatabase(common.ResourceHandleSource, common.DBName).
		InitWorkload(common.ResourceHandleWorkload).
		RunWorkload(common.Spec.GetWorkloadOption(workload.CommandTypePrepare)).
		RunLoopTask(loopLifeTime, loopCron, false,steps.NewRunWorkload(common.Spec.GetWorkloadOption(workload.CommandTypeRun))).
		//RunWorkload(common.Spec.GetWorkloadOption(workload.CommandTypeRun)).
		SendFinishMark(common.DBName).
		WaitFinishMark(common.ResourceHandleMySQL, common.DBName, 5*time.Hour).
		CheckConsistency(common.ResourceHandleSource, common.ResourceHandleMySQL, common.DBName).
		Build()
},
)
