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 CreateClusterDag() *entity.Dag {
	dag := &entity.Dag{
		BaseInfo: entity.BaseInfo{
			ID: common.CreateCluster,
		},
		Name:   common.CreateCluster,
		Status: entity.DagStatusNormal,
		Vars: map[string]entity.DagVar{
			workflow.KeyTraceId:          {},
			common.KeyClusterId:          {},
			common.KeyClusterDisplayName: {},
			common.KeyTargetStatus:       {},
			common.KeyTidbGroupId:        {},
			common.KeyTidbGroupIds:       {},
			workflow.KeyRequestMeta:      {},
			common.KeyNewCluster: {
				DefaultValue: "true",
			},
		},
		Tasks: []entity.Task{
			{
				ID:         action.PollUntilEncryptionKeyActive,
				ActionName: action.PollUntilEncryptionKeyActive,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
			},
			{
				ID:         action.CreateTidbCluster,
				ActionName: action.CreateTidbCluster,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.PollUntilEncryptionKeyActive},
			},
			{
				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.CreateTidbCluster},
			},
			{
				ID:         action.CreateTidbGroup,
				ActionName: action.CreateTidbGroup,
				Params: map[string]any{
					common.KeyClusterId:   fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTidbGroupId: fmt.Sprintf("{{%s}}", common.KeyTidbGroupId),
				},
				DependOn: []string{action.PollClusterUntilAvailable},
			},
			{
				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.CreateTidbGroup},
			},
			{
				ID:         action.ExecExtraSetUpSql,
				ActionName: action.ExecExtraSetUpSql,
				Params: map[string]any{
					common.KeyClusterId:   fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTidbGroupId: fmt.Sprintf("{{%s}}", common.KeyTidbGroupId),
				},
				DependOn: []string{action.PollTidbGroupUntilAvailable},
			},
			{
				ID:         action.FullClusterInfo,
				ActionName: action.FullClusterInfo,
				Params: map[string]any{
					common.KeyClusterId:    fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTidbGroupIds: fmt.Sprintf("{{%s}}", common.KeyTidbGroupIds),
				},
				DependOn: []string{action.ExecExtraSetUpSql},
			},
			{
				ID:         action.CreateBackupSetting,
				ActionName: action.CreateBackupSetting,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.FullClusterInfo},
			},
			{
				ID:         action.SetGlobalClusterStatus,
				ActionName: action.SetGlobalClusterStatus,
				Params: map[string]any{
					common.KeyClusterId:    fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTargetStatus: fmt.Sprintf("{{%s}}", common.KeyTargetStatus),
				},
				DependOn: []string{action.CreateBackupSetting},
			},
		},
	}

	return dag
}
