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"
)

// TiKVScaleSync
// create tidb cluster / mysql / workload
// create changefeed
// prepare incremental data
// resume changefeed
// run real time data
// scale out cluster tikv from 3 -> 7 -> 3
// wait for workload finish
// check consistency

var _ = common.AddTestCase("cc_tikv_scale_sync", func() steps.Step {
	dbName := "CTSS"
	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.ChangeFeedListFlags{}).
		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.
		}).
		ScaleTiDBCluster(resource.TiKV, 7, 0).
		ScaleTiDBCluster(resource.TiCDC, 4, 0).
		ScaleTiDBCluster(resource.TiKV, 4, 0).
		Sleep(5*time.Minute).
		Parallel(
			steps.NewRunWorkload(&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.
			}),
			steps.NewScaleTiDBCluster(resource.TiCDC, 7, 0),
		).
		SendFinishMark(dbName).
		WaitFinishMark(common.ResourceHandleMySQL, dbName, 3*time.Hour).
		Build()
},
)
