package configcentercli

import (
	"context"
	"fmt"
	"testing"

	"github.com/tidbcloud/devops/pkg/infra/utils/pointer"
	"github.com/tidbcloud/devops/pkg/infra/utils/test"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	ccproto "github.com/tidbcloud/config-center/proto/config_center"
)

func TestLarkClient(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Approval Lark Client")
}

var _ = Describe("[Config Center Client]", func() {
	var cli *Client

	BeforeEach(func() {
		var err error
		addr := "localhost:8080"
		cli, err = NewClient(addr)
		Expect(err).To(BeNil())
	})

	Context("Have a try", func() {
		PIt("list configuration dict items", func() {
			ctx := context.Background()
			req := &ccproto.ListConfigurationDictItemsRequest{
				ConfigurationType: pointer.Pointer("tidb"),
				Page:              1,
				PageSize:          10,
			}

			resp, err := cli.ConfigAPI.ListConfigurationDictItems(ctx, req)
			Expect(err).To(BeNil())
			fmt.Println(test.MarshalIndentOrDie(resp))
		})

		PIt("get a cluster config", func() {
			ctx := context.Background()
			req := &ccproto.ListClusterConfigurationFilesRequest{
				ClusterType: ccproto.ClusterType_DEDICATED_TIDB,
				ClusterId:   "10390654326988456568",
			}
			resp, err := cli.ConfigAPI.ListClusterConfigurationFiles(ctx, req)
			Expect(err).To(BeNil())
			fmt.Println(test.MarshalIndentOrDie(resp))
		})

		PIt("list cluster configuration files", func() {
			ctx := context.Background()
			req := &ccproto.GetClusterMetaRequest{
				ClusterType:   ccproto.ClusterType_DEDICATED_TIDB,
				ClusterId:     "10390654326988456568",
				MainComponent: ccproto.DEDICATED_TIDB_COMPONENT_TIDB,
			}

			resp, err := cli.ConfigAPI.GetClusterMeta(ctx, req)
			Expect(err).To(BeNil())
			fmt.Println(test.MarshalIndentOrDie(resp))
		})

		PIt("batch search cluster configuration template", func() {
			ctx := context.Background()
			req := &ccproto.SearchDefaultConfigurationTemplateRequest{
				ClusterType:       ccproto.ClusterType_DEDICATED_TIDB,
				ConfigurationType: "tidb",
				Version:           "v6.5.0",
				CloudProvider:     "aws",
				NodeInstance:      "t3a.small",
			}
			resp, err := cli.ConfigAPI.SearchDefaultConfigurationTemplate(ctx, req)
			Expect(err).To(BeNil())
			fmt.Println(test.MarshalIndentOrDie(resp))
		})

		PIt("get tenant init configs", func() {
			ctx := context.Background()
			req := &ccproto.ListConfigurationDictItemsRequest{
				Version: pointer.Pointer("v6.5.0"),
			}
			usage, err := cli.ConfigAPI.ListConfigurationDictItems(ctx, req)
			Expect(err).To(BeNil())
			fmt.Println(test.MarshalIndentOrDie(usage))
		})
	})
})
