package trunk

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

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

// UpdateRequest struct to update a trunk
type UpdateRequest struct {
	// ID of the trunk to update
	// Required: true
	TrunkID uint64 `url:"id" json:"id" validate:"required"`

	// New name of the trunk
	// Required: false
	Name string `url:"name,omitempty" json:"name,omitempty"`

	// List of trunk tags (values between 1-4095)
	// Required: false
	TrunkTags string `url:"trunk_tags,omitempty" json:"trunk_tags,omitempty" validate:"omitempty,trunkTags"`

	// New description of the trunk
	// Required: false
	Description string `url:"description,omitempty" json:"description,omitempty"`

	// New native VLAN ID
	// Required: false
	NativeVLANID uint64 `url:"native_vlan_id,omitempty" json:"native_vlan_id,omitempty"`

	// MTU
	// Default value: 1500
	// Required: false
	MTU uint64 `url:"mtu,omitempty" json:"mtu,omitempty"`
}

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

	url := "/cloudbroker/trunk/update"

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