package disks

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

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

// UpdateRequest struct to update disk
type UpdateRequest struct {
	// ID of the disk to update
	// Required: true
	DiskID uint64 `url:"disk_id" json:"disk_id" validate:"required"`

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

	// BLK Discard
	// Required: false
	BLKDiscard interface{} `url:"blkdiscard,omitempty" json:"blkdiscard,omitempty" validate:"omitempty,isBool"`

	// Block size of disk
	// Required: false
	BlockSize string `url:"block_size,omitempty" json:"block_size,omitempty"`
}

// Update updates disk
func (d Disks) Update(ctx context.Context, req UpdateRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/disks/update"

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