package pcidevice

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

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

// AccessGrant struct to grant access to a PCI device
type AccessGrantRequest struct {
	DeviceID   uint64   `url:"device_id"    json:"device_id" validate:"required"`
	AccountIDs []uint64 `url:"account_ids"  json:"account_ids" validate:"required"`
}

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

	url := "/cloudbroker/pcidevice/access_grant"

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