package data_pipeline

import (
	"github.com/shiningrush/fastflow/pkg/entity"
	"github.com/shiningrush/fastflow/pkg/entity/run"
	data_pipeline_model "github.com/tidbcloud/dataflow-service-ng/app/models/data_pipeline"
	"github.com/tidbcloud/dataflow-service-ng/app/workflow"
	data_pipeline_action "github.com/tidbcloud/dataflow-service-ng/app/workflow/action/data_pipeline"
)

func GetDeleteDataPipelineDag(
	coordinator data_pipeline_model.LifecycleCoordinator,
) (*entity.Dag, []run.Action) {
	// Producers first, destination last.
	//
	// The reverse order holds hostage the two children that could have been
	// cleaned up: the steps run in sequence, so a destination that cannot be
	// reached retries forever and the changefeed is never deleted - it keeps
	// running, keeps writing to a staging area nobody reads, and keeps
	// charging, on the one path where the user has already said to stop.
	// Observed on staging with an unreachable Lake.
	//
	// Stopping the writers first also leaves a more sensible partial state if
	// the sequence does break: a destination that is still present but has
	// nothing arriving, rather than a stream with no reader.
	actions := []run.Action{
		data_pipeline_action.NewDeleteChangefeedAction(coordinator),
		data_pipeline_action.NewDeleteExportAction(coordinator),
		data_pipeline_action.NewDeleteDestinationAction(coordinator),
		data_pipeline_action.NewFinishDeleteAction(coordinator),
	}
	return buildLinearDag(workflow.DagDeleteDataPipeline, actions)
}
