package netobjgroups

import (
	"context"
	"encoding/json"
	"net/http"

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

// AttachExtNetPortsRequest struct to attach external network ports to a network object group
type AttachExtNetPortsRequest struct {
	// ID of a network object group
	// Required: true
	ObjectGroupID string `url:"object_group_id" json:"object_group_id" validate:"required"`

	// ID of an access group
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// IDs of external network ports to attach to a network object group
	// Required: true
	PortIDs []string `url:"port_ids" json:"port_ids" validate:"required"`
}

// AttachExtNetPorts attaches external network ports to a network object group
func (nog NetworkObjectGroups) AttachExtNetPorts(ctx context.Context, req AttachExtNetPortsRequest) (*RecordVersion, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/network_object_group/attach_external_network_ports"

	res, err := nog.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
	if err != nil {
		return nil, err
	}

	info := RecordVersion{}

	err = json.Unmarshal(res, &info)
	if err != nil {
		return nil, err
	}

	return &info, nil
}
