package acsgroups

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

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

// UpdateRequest struct to update access group
type UpdateRequest struct {
	// Access group ID
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// Comment of the acces group
	// Required: false
	Comment string `url:"comment,omitempty" json:"comment,omitempty"`

	// Name of acces group
	// Required: false
	DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`
}

// Update updates a access groups
func (i AccessGroups) Update(ctx context.Context, req UpdateRequest) (*AccessGroup, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/access_group/update"

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

	info := AccessGroup{}

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

	return &info, nil
}
