package grid

import (
	"context"
	"net/http"
	"strconv"

	"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
	"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)

// SetResourcesLimitRequest struct to set resources limit
type SetResourcesLimitRequest struct {
	// ID of the grid
	// Required: true
	GID uint64 `url:"grid_id" json:"grid_id" validate:"required"`

	// Number of CPU cores reserved
	// Default: 0
	// Required: true
	ReservedCPU uint64 `url:"reserved_cpu" json:"reserved_cpu"`

	// Amount of RAM in MB reserved
	// Default: 0
	// Required: true
	ReservedRAM uint64 `url:"reserved_ram" json:"reserved_ram"`
}

// SetResourcesLimit sets reserved CPU and RAM limits for the grid
func (g Grid) SetResourcesLimit(ctx context.Context, req SetResourcesLimitRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/grid/set_resources_limit"

	res, err := g.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
	if err != nil {
		return false, err
	}

	return strconv.ParseBool(string(res))
}
