package cc

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"
	"github.com/pingcap/tiflow/cdc/model"
)

// BasicIncrementalSync
// 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("cc_basic_incremental_sync", func() steps.Step {
	dbName := "CBIS"
	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).
		CreateDatabase(common.ResourceHandleSource, dbName).
		InitCDCComponents(common.ResourceHandleSource, common.ResourceHandleMySQL).
		CreateDatabase(common.ResourceHandleSource, dbName).
		//OperateChangeFeed(&ticdc.ChangeFeedCreateFlags{SinkType: ticdc.SinkTypeMySQL, ID: common.ChangefeedMySQLcc}).
		Sleep(1 * time.Minute).
		OperateChangeFeed(&ticdc.ChangeFeedAdminJobFlags{JobType: model.AdminStop, ID: common.ChangefeedMySQLcc}).
		InitWorkload(common.ResourceHandleWorkload).
		RunWorkload(&workload.QihooOption{
			Command:        workload.CommandTypeRun,
			PropertyFile:   "./workloads/betting", // property file is located inside cc workload image
			PropertyValues: []string{"operationcount=500000", "dbnameprefix=" + common.DBNameccPrefix, "tablecount=200", "unitnameprefix=unit2_", "unitscount=10", "databaseproportions=1.0", "exectime=1800"},
			Threads:        20,
			Duration:       3 * time.Hour, // we do not know how long the command will take, set it to 8 at the moment.
		}).
		OperateChangeFeed(&ticdc.ChangeFeedAdminJobFlags{JobType: model.AdminResume, ID: common.ChangefeedMySQLcc}).
		SendFinishMark(dbName).
		WaitFinishMark(common.ResourceHandleMySQL, dbName, 5*time.Hour).
		//CheckConsistency(common.ResourceHandleSource, common.ResourceHandleMySQL, common.DBNamecc).
		Build()
},
)
