package rg

import (
	"context"
	"net/http"
	"strconv"

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

// UpdateACLAccessRequest updates ACL access in a resource group
type UpdateACLAccessRequest struct {
	ID          uint64   `url:"id"          json:"id" validate:"required"`
	AccessType  string   `url:"accesstype"  json:"accesstype" validate:"required"`
	Usernames   []string `url:"usernames,omitempty"   json:"usernames,omitempty"`
	APIAccesses []uint64 `url:"apiaccesses,omitempty" json:"apiaccesses,omitempty"`
}

// UpdateACLAccess updates ACL access in a resource group.
func (r RG) UpdateACLAccess(ctx context.Context, req UpdateACLAccessRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/rg/update_acl_access"

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

	result, err := strconv.ParseBool(string(res))
	if err != nil {
		return false, err
	}

	return result, nil
}
