package metering

import (
	"context"

	globaldedicatedv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/dedicated/v1"
	meteringsvc "github.com/tidbcloud/tidb-management-service/dedicated-global-service/domain/service/metering"
)

func NewServer(calcClusterFeeSvc *meteringsvc.CalcClusterFeeSvc) globaldedicatedv1.MeteringServiceServer {
	return &server{
		calcClusterFeeSvc: calcClusterFeeSvc,
	}
}

type server struct {
	calcClusterFeeSvc *meteringsvc.CalcClusterFeeSvc
}

func (s server) CalcClusterFee(
	ctx context.Context,
	request *globaldedicatedv1.CalcClusterFeeRequest,
) (*globaldedicatedv1.CalcClusterFeeResponse, error) {
	resp := &globaldedicatedv1.CalcClusterFeeResponse{}

	costs, err := s.calcClusterFeeSvc.Execute(ctx, request)
	if err != nil {
		return resp, err
	}

	resp.Costs = costs
	return resp, nil
}
