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"
	"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("incremental_table_num_sync", func() steps.Step {
	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: "basic-incremental-sync-1"}).
		Sleep(20 * time.Second).
		OperateChangeFeed(&ticdc.ChangeFeedAdminJobFlags{JobType: model.AdminStop, ID: "basic-incremental-sync-1"}).
		CreateDatabase(common.ResourceHandleSource, common.DBName).
		// TODO
		// Use Qihoo workload for big number table this moment
		// Will use sysbench in the future
		InitWorkload(common.ResourceHandleWorkload).
		RunWorkload(&workload.QihooOption{
			Command:        workload.CommandTypeLoad,
			PropertyFile:   "./workloads/betting", // property file is located inside 360 workload image
			PropertyValues: []string{"operationcount=5000", "unitnameprefix=unit1_", "autocommit=1", "dbnameprefix=" + common.DBName, "tablecount=1000", "unitscount=125"},
			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:      "basic-incremental-sync-1",
		}).
		Parallel(
			steps.NewRunWorkload(&workload.QihooOption{
				Command:        workload.CommandTypeRun,
				PropertyFile:   "./workloads/oncall2929", // property file is located inside 360 workload image
				PropertyValues: []string{"operationcount=5000", "unitnameprefix=unit1_", "autocommit=1", "dbnameprefix=" + common.DBName, "tablecount=100", "unitscount=125"},
				Threads:        20,
				Duration:       3 * time.Hour, // we do not know how long the command will take, set it to 24 at the moment.
			}),
		).
		SendFinishMark(common.DBName).
		WaitFinishMark(common.ResourceHandleMySQL, common.DBName, 2*time.Hour).
		CheckConsistency(common.ResourceHandleSource, common.ResourceHandleMySQL, common.DBName).
		Build()
})
