package tests

import (
	"testing"

	"github.com/pingcap/dbaas/billing2/db/entity"
	"github.com/pingcap/dbaas/billing2/forms"
	"github.com/pingcap/dbaas/billing2/model/creditriskcontrol"
	"github.com/pingcap/dbaas/billing2/model/marketing"
	"github.com/pingcap/dbaas/billing2/utils/tests"
	"github.com/pingcap/dbaas/billing2/utils/tests/datainit"
	"github.com/pingcap/dbaas/billing2/utils/timeutils"
	"github.com/shopspring/decimal"
	"github.com/stretchr/testify/suite"
)

type TenantCloseActionSuite struct {
	tests.BillingTestSuite
}

func TestTenantCloseActionSuite(t *testing.T) {
	suite.Run(t, new(TenantCloseActionSuite))
}

func (suite *TenantCloseActionSuite) TestCleanupTenantSupportPlan() {
	assert := suite.Assert()
	var err error
	supportPlanModel := &marketing.SupportPlanModel{}
	err = supportPlanModel.CreateSupportPlanUnits()
	assert.NoError(err)
	tenantID := uint(1)
	datainit.CreateBillingConfig(tenantID, entity.BillingConfigType_DirectCustomer)
	err = creditriskcontrol.CleanupTenantSupportPlan(tenantID, &creditriskcontrol.ActionParams{})
	assert.NoError(err)
	info, err := supportPlanModel.CreateSupportPlan(&forms.SupportPlan{
		FixedChargePrice: decimal.NewFromInt(15),
		Type:             entity.SupportPlanType_FixedPrice,
		Public:           true,
		Level:            entity.SupportPlanLevel_Premium,
	})
	assert.NoError(err)
	err = datainit.CreatePaymentMock(tenantID)
	assert.NoError(err)
	timeutils.SetMockNow(datainit.BillingDate)
	plan, err := supportPlanModel.BindSupportPlanForPayableCustomer(tenantID, info.SupportPlanID, forms.DateInterval{StartDate: timeutils.GetTimeAddr(timeutils.GetDayDate(timeutils.GetNow()))})
	assert.NoError(err)
	assert.Equal(entity.BindedSupportPlanStatus_ACTIVE, plan.Status)
	err = creditriskcontrol.CleanupTenantSupportPlan(tenantID, &creditriskcontrol.ActionParams{})
	plan, err = supportPlanModel.GetBindedSupportPlan(plan.BindedSupportPlanID)
	assert.NoError(err)
	assert.Equal(entity.BindedSupportPlanStatus_CANCELED, plan.Status)
}
