package trunk

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

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

// CreateRequest struct to create a trunk
type CreateRequest struct {
	// Name of the trunk
	// Required: true
	Name string `url:"name" json:"name" validate:"required"`

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

	// OVS bridge name
	// Required: true
	OVSBridge string `url:"ovs_bridge" json:"ovs_bridge" validate:"required"`

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

	// List of account IDs with access to this trunk
	// Required: false
	AccountIDs []uint64 `url:"account_ids,omitempty" json:"account_ids,omitempty"`

	// 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"`
}

// Create creates a user.
func (t Trunk) Create(ctx context.Context, req CreateRequest) (uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return 0, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/trunk/create"

	res, err := t.client.DecortApiCall(ctx, http.MethodPost, url, req)
	if err != nil {
		return 0, err
	}

	result, err := strconv.ParseUint(string(res), 10, 64)
	if err != nil {
		return 0, err
	}

	return result, nil
}
