package compute

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

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

// SetNetConfigRequest struct to Configure libvirt virtio interface parameters
type SetNetConfigRequest struct {
	// ID of compute instance
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// Interface MAC-address
	// Required: true
	MAC string `url:"mac" json:"mac" validate:"required"`

	// TXMode, must be 'iothread', 'timer' or 'selected by hypervisor'
	// Required: false
	TXMode string `url:"txmode,omitempty" json:"txmode,omitempty" validate:"omitempty,interfaceTXModel"`

	// IOEventFD, must be 'on', 'off' or 'selected by hypervisor'
	// Required: false
	IOEventFD string `url:"ioeventfd,omitempty" json:"ioeventfd,omitempty" validate:"omitempty,interfaceIOEventFD"`

	// EventIDx, must be 'on', 'off' or 'selected by hypervisor'
	// Required: false
	EventIDx string `url:"event_idx,omitempty" json:"event_idx,omitempty" validate:"omitempty,interfaceEventIDx"`

	// Number of queues
	// Required: false
	Queues uint64 `url:"queues,omitempty" json:"queues,omitempty"`

	// RX queue size
	// Required: false
	RXQueueSize uint64 `url:"rx_queue_size,omitempty" json:"rx_queue_size,omitempty"`

	// TX queue size
	// Required: false
	TXQueueSize uint64 `url:"tx_queue_size,omitempty" json:"tx_queue_size,omitempty"`
}

// Configure libvirt virtio interface parameters
func (c Compute) SetNetConfig(ctx context.Context, req SetNetConfigRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/compute/setNetConfig"

	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
}
