package compute

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

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

// UpdateRequest struct to update compute
type UpdateRequest struct {
	// ID of the compute
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// New name
	// Required: false
	Name string `url:"name,omitempty" json:"name,omitempty"`

	// New description
	// Required: false
	Description string `url:"desc,omitempty" json:"desc,omitempty"`

	// Rule for VM placement with NUMA affinity.
	// Possible values - none (placement without NUMA affinity),
	// strict (strictly with NUMA affinity, if not possible - do not start VM),
	// loose (use NUMA affinity if possible)
	// Required: false
	// Default: none
	NumaAffinity string `url:"numaAffinity,omitempty" json:"numaAffinity,omitempty" validate:"omitempty,numaAffinity"`

	// Run VM on dedicated CPUs. To use this feature, the system must be pre-configured by allocating CPUs on the physical node, true or false
	// Required: false
	CPUPin interface{} `url:"cpupin,omitempty" json:"cpupin,omitempty" validate:"omitempty,isBool"`

	// Type of the emulated system, Q35 or i440fx
	// Required: false
	Chipset string `url:"chipset,omitempty" json:"chipset,omitempty" validate:"omitempty,chipset"`

	// Use Huge Pages to allocate RAM of the virtual machine. The system must be pre-configured by allocating Huge Pages on the physical node, true or false
	// Required: false
	HPBacked interface{} `url:"hpBacked,omitempty" json:"hpBacked,omitempty" validate:"omitempty,isBool"`

	// Auto start when node restarted, true or false
	// Required: false
	AutoStart interface{} `url:"autoStart,omitempty" json:"autoStart,omitempty" validate:"omitempty,isBool"`

	// 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"`

	// VM type linux, windows or unknown
	// Required: false
	LoaderType string `url:"loaderType,omitempty" json:"loaderType,omitempty" validate:"omitempty,loaderType"`

	// Boot type of image bios or uefi
	// Required: false
	BootType string `url:"bootType,omitempty" json:"bootType,omitempty" validate:"omitempty,imageBootType"`

	// Select a network interface naming pattern for your Linux machine. eth - onboard, ens - pci slot naming.
	// Required: false
	NetworkInterfaceNaming string `url:"networkInterfaceNaming,omitempty" json:"networkInterfaceNaming,omitempty" validate:"omitempty,networkInterfaceNaming"`

	// Does this machine supports hot resize, true or false
	// Required: false
	HotResize interface{} `url:"hotResize,omitempty" json:"hotResize,omitempty" validate:"omitempty,isBool"`

	// The OS version that will be installed on the virtual machine
	// Required: false
	OSVersion string `url:"os_version,omitempty" json:"os_version,omitempty"`

	// Priority weight of the compute: higher value means higher priority and later migration
	// Required: false
	Weight uint64 `url:"weight,omitempty" json:"weight,omitempty"`

	// Clock type for the VM
	// Required: false
	// Default: null
	Clock string `url:"clock,omitempty" json:"clock,omitempty"`

	// Watchdog action
	// Required: false
	// Default: reset
	WatchdogAction string `url:"watchdog_action,omitempty" json:"watchdog_action,omitempty" validate:"omitempty,watchdogAction"`

	// Memory balloon mode. One of ["auto","legacy","none"]
	// Required: false
	// Default: "auto"
	MemoryBalloonMode string `url:"memory_balloon_mode,omitempty" json:"memory_balloon_mode,omitempty" validate:"omitempty,memoryBalloonMode"`
}

// Update updates some properties of the compute
func (c Compute) Update(ctx context.Context, req UpdateRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/compute/update"

	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
}
