package changefeed

import (
	"fmt"

	"github.com/shiningrush/fastflow/pkg/entity"
	"github.com/shiningrush/fastflow/pkg/entity/run"
	appworkflow "github.com/tidbcloud/serverless-service/internal/workflow"
	workflowchangefeed "github.com/tidbcloud/serverless-service/internal/workflow/action/changefeed"
	"github.com/tidbcloud/serverless-service/internal/workflow/action/changefeed/scale"
)

func GetScaleClusterDag(hook appworkflow.CDCNewArchHook) (*entity.Dag, []run.Action) {
	scheduler := workflowchangefeed.NewSharedScheduler(hook)
	actions := []run.Action{
		scale.NewScaleClusterAction(scheduler),
		scale.NewFinalAction(hook),
	}

	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: appworkflow.DagScaleNewArchCluster,
		},
		Name: appworkflow.DagScaleNewArchCluster,
		Vars: entity.DagVars{
			"cluster_id":    {},
			"changefeed_id": {},
		},
		Status: entity.DagStatusNormal,
		Tasks: []entity.Task{
			{
				ID:         appworkflow.ActionScaleNewArchCluster,
				ActionName: appworkflow.ActionScaleNewArchCluster,
				Params: map[string]any{
					"cluster_id":    fmt.Sprintf("{{%s}}", "cluster_id"),
					"changefeed_id": fmt.Sprintf("{{%s}}", "changefeed_id"),
				},
				TimeoutSecs: 60 * 60,
			},
			{
				ID:         appworkflow.ActionScaleNewArchFinal,
				ActionName: appworkflow.ActionScaleNewArchFinal,
				Params: map[string]any{
					"cluster_id":    fmt.Sprintf("{{%s}}", "cluster_id"),
					"changefeed_id": fmt.Sprintf("{{%s}}", "changefeed_id"),
				},
				TimeoutSecs: 3 * 60,
				DependOn:    []string{appworkflow.ActionScaleNewArchCluster},
			},
		},
	}
	return dag, actions
}
