package webScene

import (
	"fmt"
	l4g "github.com/alecthomas/log4go"
	"github.com/stretchr/testify/assert"
	"net/http"
)

func (w *WebE2ECase) CheckUpdateSpendLimit() {
	// init config
	orgId := w.WebRequestInfo.User.OrgId

	pathParam := map[string]string{"org_id": orgId, "cluster_id": w.WebRequestInfo.Cluster.ClusterId}
	urlInfo := w.URLInfoMap["querySpendLimits"]
	resp := w.WebRequestInfo.DoWebAPIRequest(urlInfo.UrlPath, urlInfo.UrlMethod, nil, pathParam, nil)
	l4g.Info("query spend limits, path param is %s, status code is %d", pathParam, resp.Raw().StatusCode)
	w.Reporter.StepReport(fmt.Sprintf("query spend limits, path param is %s, status code is %d", pathParam, resp.Raw().StatusCode),
		func() {
			resp.Status(http.StatusOK)
		})

	spendLimits := resp.JSON().Path("$.spend_limit").Number().Raw()
	newSpendLimits := float64(spendLimits + 123)
	body := map[string]float64{"spend_limit": newSpendLimits}
	urlInfo = w.URLInfoMap["updateSpendLimits"]

	upResp := w.WebRequestInfo.DoWebAPIRequest(urlInfo.UrlPath, urlInfo.UrlMethod, body, pathParam, nil)

	//二次查询
	urlInfo = w.URLInfoMap["querySpendLimits"]
	resp = w.WebRequestInfo.DoWebAPIRequest(urlInfo.UrlPath, urlInfo.UrlMethod, nil, pathParam, nil)
	l4g.Info("update spend limits, path param is %s, body is %s, status code is %d", pathParam, body, upResp.Raw().StatusCode)
	w.Reporter.StepReport(fmt.Sprintf("update spend limits, path param is %s,body is %v,  status code is %d", pathParam, body, upResp.Raw().StatusCode),
		func() {
			upResp.Status(http.StatusOK)
			spendLimits = resp.JSON().Path("$.spend_limit").Number().Raw()
			assert.Equal(w.Reporter.T, float64(spendLimits), newSpendLimits)
		})

}

func (w *WebE2ECase) CheckServerlessPrivateLinkConnect() {
	if !w.WebRequestInfo.GetVPCSupportRegion() {
		return
	}
	orgId := w.WebRequestInfo.User.OrgId
	projectId := w.WebRequestInfo.User.ProjectId
	clusterId := w.WebRequestInfo.Cluster.ClusterId
	pathParam := map[string]string{"org_id": orgId, "project_id": projectId, "cluster_id": clusterId}

	urlInfo := w.URLInfoMap["queryServerlessPrivateLink"]
	resp := w.WebRequestInfo.DoWebAPIRequest(urlInfo.UrlPath, urlInfo.UrlMethod, nil, pathParam, nil)

	l4g.Info("query serverless private link, path param is %s, status code is %d", pathParam, resp.Raw().StatusCode)
	w.Reporter.StepReport(fmt.Sprintf("query serverless private link, path param is %s, status code is %d", pathParam, resp.Raw().StatusCode),
		func() {
			resp.Status(http.StatusOK)
			resp.JSON().Path("$.privatelink_service.status").String().Equal("ACTIVE")
		})

	serverlessName := resp.JSON().Path("$.privatelink_service.name").String().Raw()
	dnsName := resp.JSON().Path("$.privatelink_service.dns_name").String().Raw()
	zoneId := resp.JSON().Path("$.privatelink_service.az_ids").Array().Element(0).String().Raw()

	endPoint, err := w.WebRequestInfo.CreateServerlessVPCEndpoint(serverlessName, zoneId)
	w.Reporter.StepReport(fmt.Sprintf("createEndPoint, result is:%v, reason is:%s", endPoint, err), func() {
		assert.Nil(w.Reporter.T, err)
	})
	if err != nil {
		w.WebRequestInfo.Cluster.RemainFlag = true
		return
	}

	//wait status to active
	result := w.WebRequestInfo.CheckAWSVPCEndpointAvailable(endPoint)
	w.Reporter.StepReport(fmt.Sprintf("vpc endpoint status is %v", result),
		func() {
			assert.True(w.Reporter.T, result)
		})
	if !result {
		w.WebRequestInfo.Cluster.RemainFlag = true
		return
	}

	w.WebRequestInfo.EnableAWSVPCEndpointDNS(endPoint)

	//获取cluster信息
	cluster, _ := w.WebRequestInfo.GetClusterInfo()
	token := *cluster.Cluster.Endpoint.Token
	user := token + ".root"
	result = w.WebRequestInfo.CheckConnectTiDBInVM(dnsName, user)
	w.Reporter.StepReport(fmt.Sprintf("connect serverless use private link result is %v", result),
		func() {
			assert.True(w.Reporter.T, result)
		})
	if !result {
		w.WebRequestInfo.Cluster.RemainFlag = true
		return
	}

	//delete private endpoint
	w.WebRequestInfo.DeleteVPCEndpoint(endPoint)
}
