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

// All cdc,pd, tikv restart by pod failure
// create tidb cluster / mysql / workload
// create changefeed
// create database
// prepare workload, make all pd failed/restart: by pod failure chaos
// run workload, make all captures failed/restart: by pod failure chaos
// make all tikv component failed/restart: by pod failure chaos, duration as 1m, it will be canceled when finish mark synced to downstream.
// wait for workload finish
// check consistency
var _ = common.AddTestCase("cc_all_node_restart_sync", func() steps.Step {
	dbName := "CANRS"
	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.ChangeFeedAdminJobFlags{JobType: model.AdminResume, ID: common.ChangefeedMySQLcc}).
		CreateDatabase(common.ResourceHandleSource, dbName).
		InitWorkload(common.ResourceHandleWorkload).
		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=20", "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.NewAllPDRestart(common.ResourceHandleSource, 20*time.Second),
		).
		Parallel(
			steps.NewAllCaptureRestart(common.ResourceHandleSource, 20*time.Second),
			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=20", "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.
			}),
		).
		MakeTiKVFailure(common.ResourceHandleSource, time.Minute).
		SendFinishMark(dbName).
		WaitFinishMark(common.ResourceHandleMySQL, dbName, 3*time.Hour).
		// CheckConsistency(common.ResourceHandleSource, common.ResourceHandleMySQL, common.DBNamecc).
		Build()
},
)
