package kafka

import (
	"fmt"
	"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"
)

// CdcLargeNumberChangefeed
// Init CDC Components: tidb cluster / sink (kafka)
// Create amount of changefeeds with config filter, each changefeed sync one sysbench table
// Init and run workload: sysbench
// Wait workload finish: even though workload finished, but it does not mean changefeed has synced all data
// Check consistency.
var _ = common.AddTestCase("cdc_large_number_changefeed_kafka", func() steps.Step {
	var (
		loopLifeTime                = time.Hour
		loopCron                    = 1 * time.Second
		changeFeedPrefix            = "cdc-large-number-changefeed-kafka-"
		changefeedCategory          = `[filter]`
		changefeedCount             = 300
		dbNameVar                   = common.DBName
		createChangefeedsWithConfig []steps.Step
		removeChangefeed            []steps.Step
	)
	for i := 0; i <= changefeedCount; i++ {
		tableName := fmt.Sprintf("sbtest%d", i)
		changeFeedName := fmt.Sprintf("%s%d", changeFeedPrefix, i)
		changefeedContent := fmt.Sprintf(`rules = ['%s.%s']`, dbNameVar, tableName)

		createChangefeedsWithConfig = append(createChangefeedsWithConfig, steps.NewChangeFeedConfig(&changefeedCategory, &changefeedContent))
		createChangefeedsWithConfig = append(createChangefeedsWithConfig, steps.NewChangeFeedTask(&ticdc.ChangeFeedCreateFlags{
			SinkType: ticdc.SinkTypeKafka,
			ID:       changeFeedName,
			Config:   true,
			Options: map[string]string{
				"topic":              "cdc-event-canal-json",
				"protocol":           "canal-json",
				"replication-factor": "3",
			},
		}))
		removeChangefeed = append(removeChangefeed, steps.NewChangeFeedTask(&ticdc.ChangeFeedAdminJobFlags{JobType: model.AdminRemove, ID: changeFeedName, ForceRemove: true}))
	}

	return steps.
		NewBuilder().
		LoadEnv().
		LoadResourceFromEnv("TI_PARAM_RES_upstream", common.ResourceHandleSource).
		LoadResourceFromEnv("TI_PARAM_RES_downstream", common.ResourceHandleKafka).
		LoadResourceFromEnv("TI_PARAM_RES_workload", common.ResourceHandleWorkload).
		InitCDCComponents(common.ResourceHandleSource, common.ResourceHandleKafka).
		RunLoopTask(loopLifeTime, loopCron, false, createChangefeedsWithConfig...).
		CreateDatabase(common.ResourceHandleSource, dbNameVar).
		InitWorkload(common.ResourceHandleWorkload).
		RunWorkload(common.Spec.GetWorkloadOption(workload.CommandTypePrepare)).
		RunWorkload(common.Spec.GetWorkloadOption(workload.CommandTypeRun)).
		SendFinishMark(dbNameVar).
		// WaitFinishMark(common.ResourceHandleMySQL, dbNameVar, 40*time.Minute).
		// CheckConsistency(common.ResourceHandleSource, common.ResourceHandleKafka, dbNameVar).
		DropDatabasesIfExists(common.ResourceHandleSource, &dbNameVar).
		Sleep(1*time.Minute).
		RunLoopTask(loopLifeTime, loopCron, false, removeChangefeed...).
		Build()
},
)
