package compute

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

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

// DiskAddRequest struct to create and attach disk to compute
type DiskAddRequest struct {
	// ID of compute instance
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// Name for disk
	// Required: true
	DiskName string `url:"diskName" json:"diskName" validate:"required"`

	// Disk size in GB
	// Required: true
	Size uint64 `url:"size" json:"size" validate:"required"`

	// Storage endpoint provider ID
	// By default the same with boot disk
	// Required: false
	SepID uint64 `url:"sepId,omitempty" json:"sepId,omitempty"`

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

	// Pool name
	// By default will be chosen automatically
	// Required: false
	Pool string `url:"pool,omitempty" json:"pool,omitempty"`

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

	// Specify image id for create disk from template
	// Required: false
	ImageID uint64 `url:"imageId,omitempty" json:"imageId,omitempty"`

	// Desired PCI slot (hex string, e.g. "0x1A")
	// Required: false
	PCISlot string `url:"pci_slot,omitempty" json:"pci_slot,omitempty"`

	// Desired bus number (hex string, e.g. "0x03")
	// Required: false
	BusNumber string `url:"bus_number,omitempty" json:"bus_number,omitempty"`

	// Disk cache mode
	// Required: false
	Cache string `url:"cache,omitempty" json:"cache,omitempty"`

	// Discard
	// Required: false
	Discard string `url:"discard,omitempty" json:"discard,omitempty"`

	// Mount disk in read-only mode
	// Required: false
	ReadOnly bool `url:"read_only,omitempty" json:"read_only,omitempty"`
}

type wrapperDiskAddRequest struct {
	DiskAddRequest

	AsyncMode bool `url:"asyncMode"`
}

// DiskAdd creates new disk and attach to compute
func (c Compute) DiskAdd(ctx context.Context, req DiskAddRequest) (uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return 0, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperDiskAddRequest{
		DiskAddRequest: req,
		AsyncMode:      false,
	}

	url := "/cloudbroker/compute/diskAdd"

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

	result, err := strconv.ParseUint(string(res), 10, 64)
	if err != nil {
		return 0, err
	}

	return result, nil
}

// DiskAddAsync creates new disk and attach to compute with AsyncMode
func (c Compute) DiskAddAsync(ctx context.Context, req DiskAddRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperDiskAddRequest{
		DiskAddRequest: req,
		AsyncMode:      true,
	}

	url := "/cloudbroker/compute/diskAdd"

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

	return string(res), nil
}
