package qihoo360testcase

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

// KVUnavailableKafkaSync
// create tidb cluster / kafka / workload
// create changefeed
// create database
// run workload
// make tikv component failed: by pod failure chaos, duration as 20s, for duration as 10m, it will be canceled when finish mark synced to downstream.
// wait for workload finish
// check consistency

func Qihoo360TikvUnavailableKafkaSync() steps.Step {
	var (
		periodTaskLifetime = time.Hour
		periodTaskCron     = 10 * time.Minute
		changeFeedID       = "kv-unavailable-kafka-sync"

		dbName360 = common.DBName
	)
	return steps.
		NewBuilder().
		LoadEnv().
		LoadResourceFromEnv("TI_PARAM_RES_upstream", common.ResourceHandleSource).
		LoadResourceFromEnv("TI_PARAM_RES_downstream2", common.ResourceHandleKafka).
		LoadResourceFromEnv("TI_PARAM_RES_workload", common.ResourceHandleWorkload).
		InitCDCComponents(common.ResourceHandleSource, common.ResourceHandleKafka).
		OperateChangeFeed(&ticdc.ChangeFeedCreateFlags{
			SinkType: ticdc.SinkTypeKafka,
			ID:       changeFeedID,
			Options: map[string]string{
				"topic":              "cdc-event-canal-json",
				"protocol":           "canal-json",
				"replication-factor": "3",
			},
		}).
		OperateChangeFeed(&ticdc.ChangeFeedAdminJobFlags{JobType: model.AdminStop, ID: changeFeedID}).
		CreateDatabase(common.ResourceHandleSource, common.DBName).
		InitWorkload(common.ResourceHandleWorkload).
		RunWorkload(&workload.QihooOption{
			Command:        workload.CommandTypeLoad,
			PropertyFile:   "./workloads/oncall2929", // property file is located inside 360 workload image
			PropertyValues: []string{"operationcount=50000", "autocommit=1", "databasename=" + common.DBName, "tablecount=200"},
			Threads:        200,
			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: changeFeedID}).
		Parallel(
			steps.NewPeriodicalTask(periodTaskLifetime, periodTaskCron,
				steps.NewKVFailure(common.ResourceHandleSource, 20*time.Second)),
			steps.NewRunWorkload(&workload.QihooOption{
				Command:        workload.CommandTypeRun,
				PropertyFile:   "./workloads/oncall2929", // property file is located inside 360 workload image
				PropertyValues: []string{"operationcount=5000000", "autocommit=1", "databasename=" + common.DBName, "tablecount=200", "exectime=3600"},
				Threads:        200,
				Duration:       2 * time.Hour, // we do not know how long the command will take, set it to 24 at the moment.
			}),
		).
		DropDatabasesIfExists(common.ResourceHandleSource, &dbName360).
		Sleep(1 * time.Minute).
		OperateChangeFeed(&ticdc.ChangeFeedAdminJobFlags{JobType: model.AdminRemove, ID: changeFeedID, ForceRemove: true}).
		Build()
}
