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"
)

// DetachLogicalPortsRequest struct to detach logical ports 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"`

	// Port bindings
	// Required: true
	PortBindings []DetachLogicalPortBinding `url:"port_bindings" json:"port_bindings" validate:"required,dive"`
}

type DetachLogicalPortBinding struct {
	// ID of a logical port
	// Required: true
	PortID string `url:"port_id" json:"port_id" validate:"required"`

	// Version of a logical port
	// Required: true
	PortVersion uint64 `url:"port_version" json:"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
}
