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 ApplyClusterDag() *entity.Dag {
	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: common.ApplyCluster,
		},
		Name:   common.ApplyCluster,
		Status: entity.DagStatusNormal,
		Vars: map[string]entity.DagVar{
			workflow.KeyTraceId:        {},
			workflow.KeyRequestMeta:    {},
			common.KeyClusterId:        {},
			common.KeyTargetStatus:     {},
			common.KeyClusterApplyMask: {},
			common.KeyOperationContext: {},
		},
		Tasks: []entity.Task{
			{
				ID:         action.ApplyCluster,
				ActionName: action.ApplyCluster,
				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.ApplyCluster},
			},
			{
				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.EventTypeApplied),
				},
				DependOn: []string{action.PollClusterUntilSynced},
			},
		},
	}

	return dag
}
