package tests

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/pingcap/dbaas/billing2/api"
	"github.com/pingcap/dbaas/billing2/db/entity"
	"github.com/pingcap/dbaas/billing2/forms"
	"github.com/pingcap/dbaas/billing2/middleware"
	"github.com/pingcap/dbaas/billing2/utils/ginutils"
	"github.com/pingcap/dbaas/billing2/utils/tests"
	"github.com/pingcap/dbaas/billing2/utils/tests/datainit"
	"github.com/stretchr/testify/suite"
	"net/http"
	"testing"
)

type MspDiscountsOpenAPITestSuite struct {
	tests.BillingTestSuite
	router *gin.Engine
}

func (suite *MspDiscountsOpenAPITestSuite) SetupTest() {
	suite.BillingTestSuite.SetupTest()
	suite.router = gin.New()
	suite.router.Use(ginutils.ErrorMiddleWare,
		middleware.UserMiddleware,
		middleware.OpenAPIPathNormalize(suite.router),
		middleware.HeaderMiddleware)
	billingOpenAPIGroup := suite.router.Group("/v1beta1/msp")
	wsOpenAPI := ginutils.MyRoute{Route: billingOpenAPIGroup}
	api.RegisterMSPOpenAPIRouter(ginutils.MyRoute{Route: wsOpenAPI})
}

func TestMspDiscountsOpenAPITestSuite(t *testing.T) {
	suite.Run(t, new(MspDiscountsOpenAPITestSuite))
}

func (suite *MspDiscountsOpenAPITestSuite) TestMspCredits() {
	assert := suite.Assert()
	datainit.CreateBillingConfig(1, entity.BillingConfigType_MSP)

	mockCentralUseMspOpenAPI()
	w := tests.PerformRequestWithOpenAPIHeader(suite.router, "GET", "/v1beta1/msp/discounts", nil)
	assert.Equal(http.StatusOK, w.Code)
	credits := []forms.CreditsPackage{}
	err := json.NewDecoder(w.Body).Decode(&credits)
	assert.NoError(err)
	assert.Len(credits, 0)
}
