package extnet

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

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

// UpdateRequest struct to update external network
type UpdateRequest struct {
	// ID of external network
	// Required: true
	NetID uint64 `url:"net_id" json:"net_id" validate:"required"`

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

	// New external network description
	// Required: false
	Description string `url:"desc,omitempty" json:"desc,omitempty"`

	// Maximum transmission unit
	// Default: 1500
	// Required: false
	MTU uint64 `url:"mtu,omitempty" json:"mtu,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 external network parameters
func (e ExtNet) Update(ctx context.Context, req UpdateRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/extnet/update"

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