package dpdknet

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

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

// UpdateRequest struct to update DPDK network
type UpdateRequest struct {
	// ID of DPDK network to update
	// Required: true
	DPDKID uint64 `url:"dpdkId" json:"dpdkId" validate:"required"`

	// New name of DPDK network
	// Required: false
	Name string `url:"name,omitempty" json:"name,omitempty"`

	// Description of DPDK network. Prefer to contain additional information about DPDK network
	// Required: false
	Description string `url:"description,omitempty" json:"description,omitempty"`

	// List of account IDs to which DPDK network will be available. Empty field means all accounts in the system.
	// Required: false
	AccountAccess []uint64 `url:"accountAccess,omitempty" json:"accountAccess,omitempty"`

	// List of resource groups IDs to which DPDK network will be available. Empty field means all resource groups in the system.
	// Required: true
	RGAccess []uint64 `url:"rgAccess,omitempty" json:"rgAccess,omitempty"`

	// ID of vlan
	// Required: true
	VlanID uint64 `url:"vlanId,omitempty" json:"vlanId,omitempty"`

	// Name of OVS Bridge to use for DPDK network
	// Required: true
	OVSBridge string `url:"ovsBridge,omitempty" json:"ovsBridge,omitempty"`

	// Flag indicating whether security groups are enabled for this network
	// Required: false
	EnableSecGroups interface{} `url:"enable_secgroups,omitempty" json:"enable_secgroups,omitempty" validate:"omitempty,isBool"`
}

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

	url := "/cloudbroker/dpdknet/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
}
