package apiaccess

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

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

// APIFindRequest struct for finding apiaccess groups.
type APIFindRequest struct {
	//API group to find (cloudbroker, cloudapi, etc)
	//Available values : cloudapi, cloudbroker, system
	//Required: true
	APIGroup string `url:"api_group" json:"api_group" validate:"required,apiGroup"`

	//API object to find (compute, vins, etc )
	//Required: true
	APIObject string `url:"api_object" json:"api_object" validate:"required"`

	//API endpoint to find (delete, create, etc)
	//Required: true
	APIMethod string `url:"api_method" json:"api_method" validate:"required"`
}

// APIFind outputs a list of apiaccess groups that mention the specified API function.
func (a APIAccess) APIFind(ctx context.Context, req APIFindRequest) ([]uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/apiaccess/apiFind"

	list := make([]uint64, 0)

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

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

	return list, nil
}
