package netobjgroups

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

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

// UpdateRequest struct to update a network object group
type UpdateRequest struct {
	// ID of the object group
	// Required: true
	ObjectGroupID string `url:"object_group_id" json:"object_group_id" validate:"required"`

	// ID of the version
	// Required: true
	VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`

	// Access Group ID
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// Description of the network object group
	// Required: true
	Description string `url:"description" json:"description" validate:"required"`

	// Name of the network object group
	// Required: true
	Name string `url:"name" json:"name" validate:"required"`

	// Addresses
	// Required: false
	Addresses []NetAddressRequest `url:"addresses,omitempty" json:"addresses,omitempty" validate:"omitempty,dive"`
}

// Update updates a network object group
func (nog NetworkObjectGroups) Update(ctx context.Context, req UpdateRequest) (*RecordNetObjGroup, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/network_object_group/update"

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

	info := RecordNetObjGroup{}

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

	return &info, nil
}
