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

// CdcScaleSync
// create tidb cluster / mysql / workload
// create changefeed
// stop changefeed
// prepare incremental data
// resume changefeed
// run real time data
// scale out cluster ticdc from 3 -> 5, then 5 -> 1 -> 3 -> 2 once
// wait for workload finish
// check consistency

var _ = common.AddTestCase("cc_scale_sync", func() steps.Step {
	dbName := "CSS"
	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).
		CreateDatabase(common.ResourceHandleSource, dbName).
		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,
		}).
		Parallel(
			steps.NewRunWorkload(&workload.QihooOption{
				Command:        workload.CommandTypeRun,
				PropertyFile:   "./workloads/oncall2929", // property file is located inside cc workload image
				PropertyValues: []string{"operationcount=5000000", "autocommit=1", "databasename=" + common.DBNamecc, "tablecount=200", "exectime=3600"},
				Threads:        20,
				Duration:       2 * time.Hour, // we do not know how long the command will take, set it to 24 at the moment.
			}),

			steps.NewLoopTask(1*time.Hour, 10*time.Minute, true,
				steps.NewScaleTiDBCluster(resource.TiCDC, 10, 0),
				steps.NewSwitchOwner(common.ResourceHandleSource, 20*time.Second),
				steps.NewScaleTiDBCluster(resource.TiCDC, 9, 0),
				steps.NewScaleTiDBCluster(resource.TiCDC, 5, 0),
				steps.NewSwitchOwner(common.ResourceHandleSource, 20*time.Second),
				steps.NewScaleTiDBCluster(resource.TiCDC, 7, 0),
			),
		).
		SendFinishMark(dbName).
		WaitFinishMark(common.ResourceHandleMySQL, dbName, 5*time.Hour).
		// CheckConsistency(common.ResourceHandleSource, common.ResourceHandleMySQL, common.DBNamecc).
		Build()
},
)
