package node

import (
	"context"
	"encoding/json"
	"net/http"

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

// SetMemAllocationRatioRequest struct to set memory allocation ratio
type SetMemAllocationRatioRequest struct {
	// Node ID
	// Required: true
	NodeID uint64 `url:"node_id" json:"node_id" validate:"required"`

	// Allocation ratio (zero or positive value)
	// Required: true
	Ratio uint64 `url:"ratio" json:"ratio" validate:"required"`
}

// SetMemAllocationRatio set memory allocation ratio
func (i Node) SetMemAllocationRatio(ctx context.Context, req SetMemAllocationRatioRequest) (*ItemNode, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/node/set_mem_allocation_ratio"

	res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
	if err != nil {
		return nil, err
	}

	info := ItemNode{}

	err = json.Unmarshal(res, &info)
	if err != nil {
		return nil, err
	}

	return &info, nil
}
