package netobjgroups

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

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

// DetachLogicalPortsRequest struct to detach an logical port from a network object group
type DetachLogicalPortsRequest 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"`

	// Version ID
	// Required: true
	VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`

	// ID of a logical port
	// Required: true
	LogicalPortID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`

	// Version of a logical port
	// Required: true
	LogicalPortVersion uint64 `url:"logical_port_version" json:"logical_port_version" validate:"required"`
}

// DetachLogicalPorts detaches logical ports from a network object group
func (nog NetworkObjectGroups) DetachLogicalPorts(ctx context.Context, req DetachLogicalPortsRequest) (*RecordVersion, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/network_object_group/detach_logical_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
}
