package image

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

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

// EditRequest struct to edit image
type EditRequest struct {
	// ID of the image to edit
	// Required: true
	ImageID uint64 `url:"imageId" json:"imageId" validate:"required"`

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

	// Name for the image
	// Required: false
	Name string `url:"name,omitempty" json:"name,omitempty"`

	// Username for the image
	// Required: false
	Username string `url:"username,omitempty" json:"username,omitempty"`

	// Password for the image
	// Required: false
	Password string `url:"password,omitempty" json:"password,omitempty"`

	// Account ID to make the image exclusive
	// Required: false
	AccountID uint64 `url:"accountId,omitempty" json:"accountId,omitempty"`

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

	// Does this image boot OS
	// Required: false
	Bootable interface{} `url:"bootable,omitempty" json:"bootable,omitempty" validate:"omitempty,isBool"`
}

// Edit edits an existing image
func (i Image) Edit(ctx context.Context, req EditRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/image/edit"

	res, err := i.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
}
