package node

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

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

// SetCpuAllocationRatioRequest struct to set CPU allocation ratio
type SetCpuAllocationRatioRequest 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"`
}

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

	url := "/cloudbroker/node/set_cpu_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
}
