package dag

import (
	"fmt"

	"github.com/tidbcloud/fastflow/pkg/entity"

	"github.com/tidbcloud/tidb-management-service/dedicated-global-service/domain/service/workflow/action"
	"github.com/tidbcloud/tidb-management-service/dedicated-global-service/domain/service/workflow/common"
	"github.com/tidbcloud/tidb-management-service/utilities/clusterevent"
	"github.com/tidbcloud/tidb-management-service/utilities/workflow"
)

func PauseClusterDag() *entity.Dag {
	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: common.PauseCluster,
		},
		Name:   common.PauseCluster,
		Status: entity.DagStatusNormal,
		Vars: map[string]entity.DagVar{
			workflow.KeyTraceId:     {},
			workflow.KeyRequestMeta: {},
			common.KeyClusterId:     {},
			common.KeyTargetStatus:  {},
		},
		Tasks: []entity.Task{
			{
				ID:         action.PauseCluster,
				ActionName: action.PauseCluster,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
			},
			{
				ID:         action.PollClusterUntilSynced,
				ActionName: action.PollClusterUntilSynced,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.PauseCluster},
			},
			{
				ID:         action.SetGlobalClusterStatus,
				ActionName: action.SetGlobalClusterStatus,
				Params: map[string]any{
					common.KeyClusterId:    fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTargetStatus: fmt.Sprintf("{{%s}}", common.KeyTargetStatus),
					common.KeyEventName:    string(clusterevent.EventTypePaused),
				},
				DependOn: []string{action.PollClusterUntilSynced},
			},
		},
	}

	return dag
}
