package compute

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

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

// ResizeRequest struct to resize compute
type ResizeRequest struct {
	// ID of compute instance
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// New CPU count.
	// Pass 0 if no change to CPU count is required
	// Required: false
	CPU uint64 `url:"cpu,omitempty" json:"cpu,omitempty"`

	// New RAM volume in MB.
	// Pass 0 if no change to RAM volume is required
	// Required: false
	RAM uint64 `url:"ram,omitempty" json:"ram,omitempty"`

	// Force compute resize
	// Required: false
	Force bool `url:"force,omitempty" json:"force,omitempty"`

	// Recommended isolated CPUs. Field is ignored if compute.cpupin=False or compute.pinned=False
	// Required: false
	PreferredCPU []int64 `url:"preferredCpu,omitempty" json:"preferredCpu,omitempty" validate:"omitempty,preferredCPU"`
}

// GetRAM returns RAM field values
func (r ResizeRequest) GetRAM() map[string]uint64 {

	res := make(map[string]uint64, 1)

	res["RAM"] = r.RAM

	return res
}

// Resize resizes compute instance
func (c Compute) Resize(ctx context.Context, req ResizeRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/compute/resize"

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

	result, err := strconv.ParseBool(string(res))
	if err != nil {
		return false, err
	}

	return result, nil
}
