package compute

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

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

// AddACLAccessRequest adds ACL access to a vm
type AddACLAccessRequest 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"`
}

// AddACLAccess adds ACL access to a vm.
func (c Compute) AddACLAccess(ctx context.Context, req AddACLAccessRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/compute/add_acl_access"

	res, err := c.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
}
