package dag

import (
	"fmt"

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

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

func UpdateClusterDag() *entity.Dag {
	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: common.UpdateCluster,
		},
		Name:   common.UpdateCluster,
		Status: entity.DagStatusNormal,
		Vars: map[string]entity.DagVar{
			workflow.KeyTraceId:     {},
			common.KeyClusterId:     {},
			common.KeyTidbGroupIds:  {},
			common.KeyTargetStatus:  {},
			common.KeyEventName:     {},
			common.KeyOldMaxRcu:     {},
			common.KeyNewMaxRcu:     {},
			workflow.KeyRequestMeta: {},
		},
		Tasks: []entity.Task{
			{
				ID:         action.UpdateTidbCluster,
				ActionName: action.UpdateTidbCluster,
				Params: map[string]any{
					common.KeyClusterId:    fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTidbGroupIds: fmt.Sprintf("{{%s}}", common.KeyTidbGroupIds),
				},
			},
			{
				ID:         action.PollClusterUntilAvailable,
				ActionName: action.PollClusterUntilAvailable,
				Params: map[string]any{
					common.KeyClusterId:    fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTidbGroupIds: fmt.Sprintf("{{%s}}", common.KeyTidbGroupIds),
				},
				DependOn: []string{action.UpdateTidbCluster},
			},
			{
				ID:         action.PollTidbGroupUntilAvailable,
				ActionName: action.PollTidbGroupUntilAvailable,
				Params: map[string]any{
					common.KeyClusterId:    fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTidbGroupIds: fmt.Sprintf("{{%s}}", common.KeyTidbGroupIds),
				},
				DependOn: []string{action.PollClusterUntilAvailable},
			},
			{
				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:    fmt.Sprintf("{{%s}}", common.KeyEventName),
					common.KeyOldMaxRcu:    fmt.Sprintf("{{%s}}", common.KeyOldMaxRcu),
					common.KeyNewMaxRcu:    fmt.Sprintf("{{%s}}", common.KeyNewMaxRcu),
				},
				DependOn: []string{action.PollTidbGroupUntilAvailable},
			},
		},
	}

	return dag
}
