package biz_changefeed

import (
	"fmt"

	"github.com/shiningrush/fastflow/pkg/entity"
	"github.com/shiningrush/fastflow/pkg/entity/run"
	"github.com/tidbcloud/dataflow-service-ng/app/container"
	"github.com/tidbcloud/dataflow-service-ng/app/workflow"
	"github.com/tidbcloud/dataflow-service-ng/app/workflow/action/biz_changefeed/del"
)

//nolint:gomnd,funlen
func GetDeleteBizChangefeedDag() (*entity.Dag, []run.Action) {
	actions := []run.Action{
		del.NewCancelUnfinishedDagInsAction(container.Global().WorkflowModel),
		del.NewDeleteChangefeedAction(
			container.Global().BizChangefeedRepo,
			container.Global().RegionalTiCDCChangefeedModel),
		del.NewDeleteOrScaleTiCDCClusterAction(
			container.Global().BizChangefeedScheduler,
		),
		del.NewDeregisterClusterJobAction(container.Global().BizChangefeedRepo, container.Global().ClusterJobModel),
		del.NewFinalAction(
			container.Global().TransProvider,
			container.Global().BizChangefeedRepo,
			container.Global().BizChangefeedHooker,
		),
	}

	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: workflow.DagDeleteBizChangefeed,
		},
		Name: workflow.DagDeleteBizChangefeed,
		Vars: entity.DagVars{
			workflow.KeyBizChangefeedID: {},
		},
		Status: entity.DagStatusNormal,
		Tasks: []entity.Task{
			{
				ID:         workflow.ActionBizChangefeedCancelUnfinishedDagIns,
				ActionName: workflow.ActionBizChangefeedCancelUnfinishedDagIns,
				Params: map[string]interface{}{
					workflow.KeyBizChangefeedID: fmt.Sprintf("{{%v}}", workflow.KeyBizChangefeedID),
				},
				TimeoutSecs: 5 * 60,
			},
			{
				ID:         workflow.ActionBizChangefeedDeleteChangefeed,
				ActionName: workflow.ActionBizChangefeedDeleteChangefeed,
				Params: map[string]interface{}{
					workflow.KeyBizChangefeedID: fmt.Sprintf("{{%v}}", workflow.KeyBizChangefeedID),
				},
				TimeoutSecs: 5 * 60,
				DependOn:    []string{workflow.ActionBizChangefeedCancelUnfinishedDagIns},
			},
			{
				ID:         workflow.ActionBizChangefeedDeleteOrScaleTiCDCCluster,
				ActionName: workflow.ActionBizChangefeedDeleteOrScaleTiCDCCluster,
				Params: map[string]interface{}{
					workflow.KeyBizChangefeedID: fmt.Sprintf("{{%v}}", workflow.KeyBizChangefeedID),
				},
				TimeoutSecs: 30 * 60,
				DependOn:    []string{workflow.ActionBizChangefeedDeleteChangefeed},
			},
			{
				ID:         workflow.ActionBizChangefeedDeleteDeregisterClusterJob,
				ActionName: workflow.ActionBizChangefeedDeleteDeregisterClusterJob,
				Params: map[string]interface{}{
					workflow.KeyBizChangefeedID: fmt.Sprintf("{{%v}}", workflow.KeyBizChangefeedID),
				},
				TimeoutSecs: 3 * 60,
				DependOn:    []string{workflow.ActionBizChangefeedDeleteOrScaleTiCDCCluster},
			},
			{
				ID:         workflow.ActionBizChangefeedDeleteFinalStep,
				ActionName: workflow.ActionBizChangefeedDeleteFinalStep,
				Params: map[string]interface{}{
					workflow.KeyBizChangefeedID: fmt.Sprintf("{{%v}}", workflow.KeyBizChangefeedID),
				},
				TimeoutSecs: 3 * 60,
				DependOn:    []string{workflow.ActionBizChangefeedDeleteDeregisterClusterJob},
			},
		},
	}
	return dag, actions
}
