package api

import (
	"github.com/gin-gonic/gin"
	"github.com/pingcap/dbaas/billing2/billing_errors"
	"github.com/pingcap/dbaas/billing2/forms"
	"github.com/pingcap/dbaas/billing2/model/creditriskcontrol"
	"github.com/pingcap/dbaas/billing2/utils/ginutils"
)

type CostQuotaAPI struct{}

// @Summary Get Tenant Cost Quota
// @Schemes
// @Accept json
// @Produce json
// @Param tenant_id path string true "tenant_id"
// @Success 200 {object} forms.CostQuota
// @Router /tenants/{tenant_id}/cost_quota [get]
func (*CostQuotaAPI) GetTenantCostQuota(c *gin.Context) (ginutils.HandlerFuncResult, error) {
	billingCfgStruct, err := getBillingCfgFromURI(c)
	if err != nil {
		return nil, err
	}
	return (&creditriskcontrol.CostQuotaModel{}).GetTenantCostQuota(billingCfgStruct.TenantID, billingCfgStruct.BillingConfigID)
}

// @Summary Set Tenant Cost Quota
// @Schemes
// @Accept json
// @Produce json
// @Param tenant_id path string true "tenant_id"
// @Param CostQuota body forms.CostQuota true "CostQuota"
// @Success 200 {object} forms.CostQuota
// @Router /tenants/{tenant_id}/cost_quota [put]
func (*CostQuotaAPI) SetTenantCostQuota(c *gin.Context) (ginutils.HandlerFuncResult, error) {
	tenantID, err := getTenantIDFromURI(c)
	if err != nil {
		return nil, err
	}
	input := &forms.CostQuota{}
	err = c.ShouldBindJSON(input)
	if err != nil {
		return nil, billing_errors.NewInvalidInputError(err)
	}
	return (&creditriskcontrol.CostQuotaModel{}).SetTenantCostQuota(tenantID, input)
}

// @Summary Get Tenant Cost Quota
// @Schemes
// @Accept json
// @Produce json
// @Param credit_risk_level_id path string true "credit_risk_level_id"
// @Success 200 {object} forms.CostQuota
// @Router /credit_risk_levels/{credit_risk_level_id}/cost_quota [get]
func (*CostQuotaAPI) GetCreditRiskLevelCostQuota(c *gin.Context) (ginutils.HandlerFuncResult, error) {
	creditRiskLevelID, err := getCreditRiskLevelIDFromURI(c)
	if err != nil {
		return nil, err
	}
	return (&creditriskcontrol.CostQuotaModel{}).GetCreditRiskLevelQuota(creditRiskLevelID)
}

// @Summary Set Tenant Cost Quota
// @Schemes
// @Accept json
// @Produce json
// @Param credit_risk_level_id path string true "credit_risk_level_id"
// @Param CostQuota body forms.CostQuota true "CostQuota"
// @Success 200 {object} forms.CostQuota
// @Router /credit_risk_levels/{credit_risk_level_id}/cost_quota [put]
func (*CostQuotaAPI) SetCreditRiskLevelCostQuota(c *gin.Context) (ginutils.HandlerFuncResult, error) {
	creditRiskLevelID, err := getCreditRiskLevelIDFromURI(c)
	if err != nil {
		return nil, err
	}
	input := &forms.CostQuota{}
	err = c.ShouldBindJSON(input)
	if err != nil {
		return nil, billing_errors.NewInvalidInputError(err)
	}
	return (&creditriskcontrol.CostQuotaModel{}).SetCreditRiskLevelQuota(creditRiskLevelID, input)
}

func InitializeCostQuotaRouter(router ginutils.MyRoute) {
	s := &CostQuotaAPI{}
	router.HandleWithError("GET", "/tenants/:tenant_id/cost_quota", s.GetTenantCostQuota)
	router.HandleWithError("PUT", "/tenants/:tenant_id/cost_quota", s.SetTenantCostQuota)
	router.HandleWithError("GET", "/credit_risk_levels/:credit_risk_level_id/cost_quota", s.GetCreditRiskLevelCostQuota)
	router.HandleWithError("PUT", "/credit_risk_levels/:credit_risk_level_id/cost_quota", s.SetCreditRiskLevelCostQuota)

}
