package cluster

import (
	"fmt"
	"net/http"
	"net/url"
	"testing"
	"time"

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

	"github.com/pingcap-inc/clinic/pkg/api/env"
	"github.com/pingcap-inc/clinic/pkg/config"
	"github.com/pingcap-inc/clinic/pkg/db"
	"github.com/pingcap-inc/clinic/pkg/proxy"
)

func Test_Time(t *testing.T) {
	tn := time.Unix(1648017649, 0)

	fmt.Println(tn.Format(time.RFC3339))
	fmt.Println(url.QueryEscape(tn.Format(time.RFC3339)))
}

func Test_KibanaURL(t *testing.T) {
	//now := time.Unix(1648017649, 0)
	//file := &db.UploadFile{
	//	BeginTime: &now,
	//	EndTime:   &now,
	//}

	// fmt.Println(getKibanaURL(file, 101, "53416a80-aa7e-11ec-9c32-05387131e6c2"))
}

func TestDashboardK8sClusterVariable(t *testing.T) {
	g := gomega.NewWithT(t)

	g.Expect(dashboardK8sClusterVariable("vulpix-prod/vulpix-prod-eks")).To(gomega.Equal("&var-k8s_cluster=vulpix-prod-eks"))
	g.Expect(dashboardK8sClusterVariable("single-k8s")).To(gomega.Equal("&var-k8s_cluster=single-k8s"))
	g.Expect(dashboardK8sClusterVariable("")).To(gomega.Equal(""))
}

func TestSharedResourceDashboardScopeVariable(t *testing.T) {
	g := gomega.NewWithT(t)

	g.Expect(sharedResourceDashboardScopeVariable(
		proxy.SharedResourceDashboardUID,
		"renamed dashboard",
	)).To(gomega.Equal("&var-shared_resource_scope=resource_pool"))
	g.Expect(sharedResourceDashboardScopeVariable(
		"future-version-uid",
		"[SharedScope]Shared Resource One Page-Clinic",
	)).To(gomega.Equal("&var-shared_resource_scope=resource_pool"))
	g.Expect(sharedResourceDashboardScopeVariable(
		"other-dashboard",
		"TiDB-Details-Clinic",
	)).To(gomega.Equal(""))
}

func TestUploadedMetricK8sClusterVariable(t *testing.T) {
	g := gomega.NewWithT(t)

	g.Expect(uploadedMetricK8sClusterVariable(
		&db.UploadFile{ClusterName: "from-file/from-file-eks"},
		&db.Cluster{Name: "from-cluster/from-cluster-eks"},
	)).To(gomega.Equal("&var-k8s_cluster=from-file-eks"))

	g.Expect(uploadedMetricK8sClusterVariable(
		&db.UploadFile{},
		&db.Cluster{Name: "pingraph-socialgraph-prod/pingraph-socialgraph-prod-eks"},
	)).To(gomega.Equal("&var-k8s_cluster=pingraph-socialgraph-prod-eks"))

	g.Expect(uploadedMetricK8sClusterVariable(nil, nil)).To(gomega.Equal(""))
}

func TestResolveDashboardContextKeepsTiUPClusterDashboard(t *testing.T) {
	g := gomega.NewWithT(t)

	ctx := service.resolveDashboardContext(
		&db.Organization{OrgType: ""},
		&db.Cluster{DeployType: "tiup-cluster", Name: "cdc-test"},
		"7670839071772911160",
		1786008000,
		1786012617,
	)

	g.Expect(ctx.deployTypeSuffix).To(gomega.Equal("cluster"))
	g.Expect(ctx.useCloudDashboard).To(gomega.BeFalse())
	g.Expect(ctx.orgType).To(gomega.Equal(""))
	g.Expect(ctx.k8sCluster).To(gomega.Equal(""))
	g.Expect(ctx.startTime).To(gomega.Equal("1786008000000"))
	g.Expect(ctx.endTime).To(gomega.Equal("1786012617000"))
}

func TestApplyTemporaryTiCIDashboardScope(t *testing.T) {
	g := gomega.NewWithT(t)

	baseMetas := []*config.MetricDashboardMeta{
		{Name: "TiDB-Cluster-Clinic", UID: "tidb"},
	}
	metas := applyTemporaryTiCIDashboardScope(baseMetas, &db.Cluster{
		ClusterID: temporaryTiCIDashboardClusterID,
	}, "operator", "v8.5.x")

	g.Expect(metas).To(gomega.HaveLen(4))
	g.Expect(metas[1:]).To(gomega.Equal([]*config.MetricDashboardMeta{
		{Name: "TiCI-Meta-Cluster-Clinic", UID: "2eebaf958b2af718operatorv8_5_x"},
		{Name: "TiCI-Reader-Cluster-Clinic", UID: "5a90bf7441e54762operatorv8_5_x"},
		{Name: "TiCI-Worker-Cluster-Clinic", UID: "e0213ee534a1b0ddoperatorv8_5_x"},
	}))

	metas = applyTemporaryTiCIDashboardScope(baseMetas, &db.Cluster{
		ClusterID: "another-cluster",
	}, "operator", "v8.5.x")
	g.Expect(metas).To(gomega.HaveLen(1))

	metas = applyTemporaryTiCIDashboardScope(baseMetas, &db.Cluster{
		ClusterID: temporaryTiCIDashboardClusterID,
	}, "cluster", "v8.5.x")
	g.Expect(metas).To(gomega.HaveLen(1))

	metasWithTiCI := []*config.MetricDashboardMeta{
		{Name: "TiCI-Meta-Cluster-Clinic", UID: "2eebaf958b2af718tidbcloudv8_5_x"},
		{Name: "TiDB-Cluster-Clinic", UID: "tidb"},
		{Name: "TiCI-Reader-Cluster-Clinic", UID: "5a90bf7441e54762tidbcloudv8_5_x"},
		{Name: "TiCI-Worker-Cluster-Clinic", UID: "e0213ee534a1bddtidbcloudv8_5_x"},
	}

	metas = applyTemporaryTiCIDashboardScope(metasWithTiCI, &db.Cluster{
		ClusterID: temporaryTiCIDashboardClusterID,
	}, "operator", "v8.5.x")
	g.Expect(metas).To(gomega.HaveLen(4))
	g.Expect(metas[0]).To(gomega.Equal(&config.MetricDashboardMeta{Name: "TiDB-Cluster-Clinic", UID: "tidb"}))
	g.Expect(metas[1:]).To(gomega.Equal([]*config.MetricDashboardMeta{
		{Name: "TiCI-Meta-Cluster-Clinic", UID: "2eebaf958b2af718operatorv8_5_x"},
		{Name: "TiCI-Reader-Cluster-Clinic", UID: "5a90bf7441e54762operatorv8_5_x"},
		{Name: "TiCI-Worker-Cluster-Clinic", UID: "e0213ee534a1b0ddoperatorv8_5_x"},
	}))

	metas = applyTemporaryTiCIDashboardScope(metasWithTiCI, &db.Cluster{
		ClusterID: "another-cluster",
	}, "operator", "v8.5.x")
	g.Expect(metas).To(gomega.Equal([]*config.MetricDashboardMeta{
		{Name: "TiDB-Cluster-Clinic", UID: "tidb"},
	}))
}

func TestListDashboards(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "cid",
			Value: "Test_01_1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
	}
	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 TestGetDataSummary(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "cid",
			Value: "Test_01_1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
	}
	status := env.EnvironmentInit("GET", service.GetDataSummary, params, nil, "?startTime=1491915697&endTime=1492002097", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

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

	status := env.EnvironmentInit("GET", service.Download, params, nil, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}
