package trunk

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

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

// AccessGrant struct to grant access to a trunk to some accounts
type AccessGrantRequest struct {
	// ID of the trunk to disable
	// Required: true
	TrunkID uint64 `url:"id" json:"id" validate:"required"`

	// IDs of the accounts to grant access to
	// Required: true
	AccountIDs []uint64 `url:"account_ids" json:"account_ids" validate:"required"`
}

// AccessGrant grants access to a trunk
func (t Trunk) AccessGrant(ctx context.Context, req AccessGrantRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/trunk/access_grant"

	res, err := t.client.DecortApiCall(ctx, http.MethodPost, url, req)
	if err != nil {
		return false, err
	}

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

	return result, nil
}
