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 ShutdownUpgradeClusterDag() *entity.Dag {
	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: common.ShutdownUpgradeCluster,
		},
		Name:   common.ShutdownUpgradeCluster,
		Status: entity.DagStatusNormal,
		Vars: map[string]entity.DagVar{
			workflow.KeyTraceId:       {},
			common.KeyClusterId:       {},
			common.KeyTargetVersion:   {},
			common.KeyTargetStatus:    {},
			common.KeyTidbImageTag:    {},
			common.KeyTikvImageTag:    {},
			common.KeyPdImageTag:      {},
			common.KeyTiflashImageTag: {},
		},
		Tasks: []entity.Task{
			{
				ID:         action.PauseClusterTc,
				ActionName: action.PauseClusterTc,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
			},
			{
				ID:         action.PollUntilClusterTcPaused,
				ActionName: action.PollUntilClusterTcPaused,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.PauseClusterTc},
			},
			{
				ID:         action.GenerateConfigAndApplyToConfigCenter,
				ActionName: action.GenerateConfigAndApplyToConfigCenter,
				Params: map[string]any{
					common.KeyClusterId:       fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTargetVersion:   fmt.Sprintf("{{%s}}", common.KeyTargetVersion),
					common.KeyTidbImageTag:    fmt.Sprintf("{{%s}}", common.KeyTidbImageTag),
					common.KeyTikvImageTag:    fmt.Sprintf("{{%s}}", common.KeyTikvImageTag),
					common.KeyPdImageTag:      fmt.Sprintf("{{%s}}", common.KeyPdImageTag),
					common.KeyTiflashImageTag: fmt.Sprintf("{{%s}}", common.KeyTiflashImageTag),
				},
				DependOn: []string{action.PollUntilClusterTcPaused},
			},
			{
				ID:         action.ApplyCluster,
				ActionName: action.ApplyCluster,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.GenerateConfigAndApplyToConfigCenter},
			},
			{
				ID:         action.PollClusterUntilSynced,
				ActionName: action.PollClusterUntilSynced,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.ApplyCluster},
			},
			{
				ID:         action.DeleteAstsAndResumeClusterTc,
				ActionName: action.DeleteAstsAndResumeClusterTc,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.PollClusterUntilSynced},
			},
			{
				ID:         action.PollUntilClusterTcResumed,
				ActionName: action.PollUntilClusterTcResumed,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.DeleteAstsAndResumeClusterTc},
			},
			{
				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.EventTypeShutdownUpgraded),
				},
				DependOn: []string{action.PollUntilClusterTcResumed},
			},
		},
	}
	return dag
}
