package cluster

import (
	"encoding/json"
	"fmt"
	"net/http"
	"testing"

	"github.com/gin-gonic/gin"
	"github.com/onsi/gomega"

	"github.com/pingcap-inc/clinic/pkg/api/common"
	"github.com/pingcap-inc/clinic/pkg/api/env"
)

var service = ClusterService{}

func Test(t *testing.T) {
	g := gomega.NewWithT(t)
	var val common.Rebuild
	val.ItemID = "d"

	data, err := json.Marshal(val)
	g.Expect(err).To(gomega.Succeed())
	g.Expect(len(data) > 0).To(gomega.BeTrue())

	var test common.Rebuild
	err = json.Unmarshal(data, &test)
	g.Expect(err).To(gomega.Succeed())
	g.Expect(test.ItemID).To(gomega.Equal("d"))
}

func TestListCluster(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
	}
	status := env.EnvironmentInit("GET", service.ListCluster, params, nil, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

/*
func TestListDashboards(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},

		{
			Key:   "cid",
			Value: "Test_01_1",
		},
	}
	status := env.EnvironmentInit("GET", service.ListDashboards, params, nil, "?startTime=1491915697&endTime=1492002097", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}*/

func TestGetCluster(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
		{
			Key:   "cid",
			Value: "Test_01_1",
		},
	}
	status := env.EnvironmentInit("GET", service.GetCluster, params, nil, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

func Test_UpdateCluster(t *testing.T) {
	g := gomega.NewWithT(t)
	reqMap := map[string]any{
		"description": "cluster_update_test",
		"name":        "cluster_update_01",
	}
	body, err := json.Marshal(reqMap)
	if err != nil {
		fmt.Println("TestPostReq json.Marshal err:", err)
		return
	}
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
		{
			Key:   "cid",
			Value: "Test_01_1",
		},
	}
	status := env.EnvironmentInit("PUT", service.UpdateCluster, params, body, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

func TestDelete(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
		{
			Key:   "cid",
			Value: "Test_01_1",
		},
	}
	status := env.EnvironmentInit("DELETE", service.Delete, params, nil, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}
