package data_pipeline

import (
	"fmt"

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

const lifecycleTaskTimeoutSeconds = 24 * 60 * 60

func buildLinearDag(name string, actions []run.Action) (*entity.Dag, []run.Action) {
	tasks := make([]entity.Task, 0, len(actions))
	for i, lifecycleAction := range actions {
		task := entity.Task{
			ID:         lifecycleAction.Name(),
			ActionName: lifecycleAction.Name(),
			Params: map[string]interface{}{
				workflow.KeyDataPipelineID: fmt.Sprintf("{{%v}}", workflow.KeyDataPipelineID),
			},
			TimeoutSecs: lifecycleTaskTimeoutSeconds,
		}
		if i > 0 {
			task.DependOn = []string{actions[i-1].Name()}
		}
		tasks = append(tasks, task)
	}

	return &entity.Dag{
		BaseInfo: entity.BaseInfo{ID: name},
		Name:     name,
		Vars: entity.DagVars{
			workflow.KeyDataPipelineID: {},
		},
		Status: entity.DagStatusNormal,
		Tasks:  tasks,
	}, actions
}
