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/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.KeyTargetStatus:  {},
			common.KeyTidbGroupId:   {},
			workflow.KeyRequestMeta: {},
			common.KeyNewCluster: {
				DefaultValue: "true",
			},
			common.KeyClusterConfiguration:      {},
			common.KeyOverwriteTenantInitConfig: {},
		},
		Tasks: []entity.Task{
			{
				ID:         action.GenerateConfiguration,
				ActionName: action.GenerateConfiguration,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
			},
			{
				ID:         action.EnsureVPC,
				ActionName: action.EnsureVPC,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.GenerateConfiguration},
			},
			{
				ID:         action.CreateCluster,
				ActionName: action.CreateCluster,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.EnsureVPC},
			},
			{
				ID:         action.PollClusterUntilSynced,
				ActionName: action.PollClusterUntilSynced,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.CreateCluster},
			},
			{
				ID:         action.CreateLoadBalancer,
				ActionName: action.CreateLoadBalancer,
				Params: map[string]any{
					common.KeyClusterId:   fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTidbGroupId: fmt.Sprintf("{{%s}}", common.KeyTidbGroupId),
				},
				DependOn: []string{action.PollClusterUntilSynced},
			},
			{
				ID:         action.CreatePrivateLinkService,
				ActionName: action.CreatePrivateLinkService,
				Params: map[string]any{
					common.KeyClusterId:   fmt.Sprintf("{{%s}}", common.KeyClusterId),
					common.KeyTidbGroupId: fmt.Sprintf("{{%s}}", common.KeyTidbGroupId),
				},
				DependOn: []string{action.CreateLoadBalancer},
			},
			{
				ID:         action.ExecExtraSetUpSql,
				ActionName: action.ExecExtraSetUpSql,
				Params: map[string]any{
					common.KeyClusterId: fmt.Sprintf("{{%s}}", common.KeyClusterId),
				},
				DependOn: []string{action.CreatePrivateLinkService},
			},
			{
				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.ExecExtraSetUpSql},
			},
		},
	}

	return dag
}
