package tests

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

	"github.com/pingcap/dbaas/billing2/middleware"

	"github.com/pingcap/dbaas/billing2/db/entity"

	"github.com/gin-gonic/gin"
	"github.com/pingcap/dbaas/billing2/api"
	"github.com/pingcap/dbaas/billing2/forms"
	"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"
)

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

func (suite *UncAPITestSuite) SetupTest() {
	suite.BillingTestSuite.SetupTest()
	suite.router = gin.New()

}
func TestUncAPITestSuite(t *testing.T) {
	suite.Run(t, new(UncAPITestSuite))
}

func (suite *UncAPITestSuite) TestRouter() {
	assert := suite.Assert()
	suite.router.Use(
		ginutils.ErrorMiddleWare,
		middleware.UserMiddleware,
		middleware.OpenAPIPathNormalize(suite.router),
		middleware.HeaderMiddleware,
	)

	billingGroup := suite.router.Group("/api/v1/billing")
	ws := ginutils.MyRoute{Route: billingGroup}
	api.InitializeCostExplorerRouter(ginutils.MyRoute{Route: ws})

	billingUncAPIGroup := suite.router.Group("/api/v1/unc")

	wsUncAPI := ginutils.MyRoute{Route: billingUncAPIGroup}
	api.InitializeUNCAPIRouter(ginutils.MyRoute{Route: wsUncAPI})

	datainit.CreateBillingConfig(1, entity.BillingConfigType_DirectCustomer)
	_, err := datainit.CreateInvoice(uint(1))
	assert.NoError(err)
	query := &forms.CostExplorerQuery{
		StartDate: datainit.BillingDate,
		EndDate:   datainit.BillingDate.Add(time.Hour),
	}
	w := tests.PerformRequest(suite.router, "POST", "/api/v1/billing/tenants/1/costexplorer", query)

	assert.Equal(http.StatusOK, w.Code)
	res := &forms.CostExplorerResult{}
	assert.NoError(json.Unmarshal(w.Body.Bytes(), res))

	query1 := &forms.Workflow{
		WorkFlowName: "test",
	}
	w = tests.PerformRequest(suite.router, "POST", "/api/v1/unc/workflow", query1)
	//assert.Equal(http.StatusOK, w.Code)
	//res1 := &forms.Workflow{}
	//assert.NoError(json.Unmarshal(w.Body.Bytes(), res1))

}
