package cluster

import (
	"testing"

	"github.com/onsi/gomega"
	workflowentity "github.com/tidbcloud/fastflow/pkg/entity"
	"google.golang.org/protobuf/encoding/protojson"

	globaldedicatedv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/dedicated/v1"
	"github.com/tidbcloud/tidb-management-service/tidb-mgmt-service/domain/constant"
	"github.com/tidbcloud/tidb-management-service/tidb-mgmt-service/domain/entity"
)

func TestMergeInWorkflowCreateState(t *testing.T) {
	t.Parallel()

	g := gomega.NewWithT(t)

	ctx := t.Context()

	cluster := entity.Cluster{
		ClusterId: 111,
		GlobalCluster: &globaldedicatedv1.Cluster{
			ClusterId:      111,
			OrganizationId: 222,
			ProjectId:      333,
		},
	}

	r1Value := &globaldedicatedv1.CreateClusterRequest{
		Cluster: &globaldedicatedv1.Cluster{
			ClusterComponents: []*globaldedicatedv1.Cluster_ClusterComponent{
				{
					ComponentType: globaldedicatedv1.ComponentType_TIDB,
					SpuName:       "tidb",
				},
				{
					ComponentType: globaldedicatedv1.ComponentType_TIKV,
					SpuName:       "tikv",
				},
			},
		},
	}
	data, err := protojson.Marshal(r1Value)
	g.Expect(err).To(gomega.Succeed())

	createCase := mergeInWorkflowState(ctx, &cluster, []*workflowentity.DagInstance{
		{
			DagID: constant.DagDedicatedCreateCluster,
			Vars: map[string]workflowentity.DagInstanceVar{
				constant.KeyRequest: {
					Value: string(data),
				},
			},
		},
	})
	g.Expect(len(createCase.GlobalCluster.(*globaldedicatedv1.Cluster).ClusterComponents)).To(gomega.Equal(2))
	g.Expect(createCase.GlobalCluster.(*globaldedicatedv1.Cluster).State.String()).
		To(gomega.Equal(globaldedicatedv1.Cluster_CREATING.String()))
}
