package inner

import (
	"errors"
	"fmt"

	"github.com/gin-gonic/gin"

	tidbmgmtservicev1 "github.com/tidbcloud/tidb-management-service/api/spec/tidb_mgmt_service/v1"
	"github.com/tidbcloud/tidb-management-service/tempo-wrapper/internal/api/types"
	"github.com/tidbcloud/tidb-management-service/tempo-wrapper/pkg/grpc"
	mgmtgrpc "github.com/tidbcloud/tidb-management-service/tempo-wrapper/pkg/grpc/mgmt"
)

func CountBillableClusters(ctx *gin.Context) {
	req := &tidbmgmtservicev1.CountBillableClustersRequest{}
	if err := types.ShouldBindJSON(ctx, req); err != nil {
		types.BadRequestResp(ctx, fmt.Errorf("bind request body failed: %w", err))
		return
	}

	if req.ProjectId == 0 || req.OrganizationId == 0 {
		types.BadRequestResp(ctx, errors.New("project id and organization id are required"))
		return
	}

	resp, err := mgmtgrpc.CountBillableClusters(grpc.NewContext(ctx), req)
	if err != nil {
		types.GrpcErrorResp(ctx, err)
		return
	}
	types.SuccessRespNotProtoNames(ctx, resp)
}
