package kvmx86

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

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

// CreateBlankRequest struct to create KVM x86 VM from scratch
type CreateBlankRequest struct {
	// ID of the resource group, which will own this VM
	// Required: true
	RGID uint64 `url:"rgId" json:"rgId" validate:"required"`

	// Name of this VM.
	// Must be unique among all VMs (including those in DELETED state) in target resource group
	// Required: true
	Name string `url:"name" json:"name" validate:"required"`

	// Number CPUs to allocate to this VM
	// Required: true
	CPU uint64 `url:"cpu" json:"cpu" validate:"required"`

	// Volume of RAM in MB to allocate to this VM
	// Required: true
	RAM uint64 `url:"ram" json:"ram" validate:"required"`

	// Storage policy id of compute. The rules of the specified storage policy will be used.
	// Required: true
	StoragePolicyID uint64 `url:"storage_policy_id" json:"storage_policy_id" validate:"required"`

	// If True, the imageId, bootDisk, sepId, pool parameters are ignored and the compute is created without a boot disk in the stopped state
	// Required: false
	WithoutBootDisk bool `url:"withoutBootDisk" json:"withoutBootDisk"`

	// Size of the boot disk in GB
	// Required: false
	BootDisk uint64 `url:"bootDisk,omitempty" json:"bootDisk,omitempty"`

	// ID of SEP to create boot disk on.
	// Uses images SEP ID if not set
	// Required: false
	SEPID uint64 `url:"sepId,omitempty" json:"sepId,omitempty"`

	// Pool to use if sepId is set, can be also empty if needed to be chosen by system
	// Required: false
	Pool string `url:"pool,omitempty" json:"pool,omitempty"`

	// Slice of structs with data disk description. Each disk has parameters: required - diskName, size; optional - sepId, pool, desc and imageId.
	// If not specified, compute will be created without disks.
	// To create compute without disks, pass initialized empty slice .
	// Required: false
	DataDisks []DataDisk `url:"-" json:"dataDisks,omitempty" validate:"omitempty,dive"`

	// Slice of structs with net interface description.
	// If not specified, compute will be created with default interface from RG.
	// To create compute without interfaces, pass initialized empty slice .
	// Required: false
	Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"`

	// Type of the emulated system, Q35 or i440fx
	// Required: false
	// Default: Q35
	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
	// Required: false
	// Default: false
	HPBacked bool `url:"hp_backed" json:"hp_backed"`

	// Run VM on dedicated CPUs. To use this feature, the system must be pre-configured by allocating CPUs on the physical node
	// Required: false
	// Default: false
	CPUPin bool `url:"cpu_pin" json:"cpu_pin"`

	// 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:"numa_affinity,omitempty" json:"numa_affinity,omitempty" validate:"omitempty,numaAffinity"`

	// Text description of this VM
	// Required: false
	Description string `url:"desc,omitempty" json:"desc,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"`

	// 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
	// Required: false
	HotResize bool `url:"hotResize,omitempty" json:"hotResize,omitempty"`

	// Zone ID
	// Required: false
	ZoneID uint64 `url:"zoneId,omitempty" json:"zoneId,omitempty"`

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

	// CPU alignment profile name
	// Required: false
	CPUAlignmentProfile string `url:"cpu_alignment_profile,omitempty" json:"cpu_alignment_profile,omitempty"`

	// Clock type for the VM
	// Required: false
	// Default: default
	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"`
}

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

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

	res["RAM"] = r.RAM

	return res
}

// CreateBlank creates KVM x86 VM from scratch
func (k KVMX86) CreateBlank(ctx context.Context, req CreateBlankRequest) (uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return 0, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/kvmx86/createBlank"

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

	return strconv.ParseUint(string(res), 10, 64)
}
