package cc

import (
	"time"

	"github.com/pingcap/test-infra/caselib/pkg/host"
	"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/pkg/utils"
	"github.com/pingcap/test-infra/caselib/ticdc/common"
	"github.com/pingcap/test-infra/common/model/resource"
)

// CDC works with BR restore upstream and downstream
// Create upstream TiDB cluster/downstream TiDB/workload/br/mc
// BR restore from s3 to upstream, then restore the same data (sysbench) to downstream
// Create changefeed
// Start to run sysbench in tables restored by BR
// Waiting for workload finish
// Check consistency
func CCCDCBRSerialized() steps.Step {
	var (
		brHost       = &resource.ResourceHandle{}
		mcHost       = &resource.ResourceHandle{}
		backupOpts   = &host.BackupFullOpts{}
		dbName		 = "CCBSNEW"
		sqlStatement1 = "set global tidb_stats_cache_mem_quota = 1073741824;"
		sqlStatement2 =	"set global tidb_analyze_version = 1;"
	)
	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).
		LoadResourceFromEnv(utils.ResourcePrefix+"br", brHost).
		LoadResourceFromEnv(utils.ResourcePrefix+"mc", mcHost).
		LoadResourceFromEnv(utils.ParamPrefix+"br_opts", backupOpts).
		DropUserDatabases(common.ResourceHandleSource).
		DropDatabasesIfExists(common.ResourceHandleSource, &dbName).
		DropDatabasesIfExists(common.ResourceHandleMySQL, &dbName).
		ExecuteSqlStatements(common.ResourceHandleSource, &sqlStatement1).
		ExecuteSqlStatements(common.ResourceHandleSource, &sqlStatement2).
		Parallel(
			steps.NewBrRestoreFull(brHost, common.ResourceHandleSource, backupOpts),
			steps.NewBrRestoreFull(brHost, common.ResourceHandleMySQL, backupOpts),
			).
		InitCDCComponents(common.ResourceHandleSource, common.ResourceHandleMySQL).
		Sleep(10*time.Second).
		OperateChangeFeed(&ticdc.ChangeFeedCreateFlags{SinkType: ticdc.SinkTypeMySQL, ID: common.ChangefeedMySQLcc}).
		CreateDatabase(common.ResourceHandleSource, dbName).
		Sleep(1*time.Minute).
		InitWorkload(common.ResourceHandleWorkload).
		RunWorkload(&workload.QihooOption{
			Command:        workload.CommandTypeLoad,
			PropertyFile:   "./workloads/betting", // property file is located inside cc workload image
			PropertyValues: []string{"recordcount=1200000", "dbnameprefix=" + common.DBNameccPrefix, "tablecount=200", "unitnameprefix=unit2_", "unitscount=10", "databaseproportions=1.0", "loadbatchsize=50", "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.
		}).
		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.
		}).
		SendFinishMark(dbName).
		Sleep(1*time.Minute).
		WaitFinishMark(common.ResourceHandleMySQL, dbName, 3*time.Hour).
		//CheckConsistency(common.ResourceHandleSource, common.ResourceHandleMySQL, dbNameVar).
		Build()
}
