package node

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

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

// GetPCIDevicesRequest struct to get list of PCI devices on a node
type GetPCIDevicesRequest struct {
	// Node ID
	// Required: true
	NodeID uint64 `url:"nid" json:"nid" validate:"required"`

	// Search string
	// Required: false
	Search string `url:"search,omitempty" json:"search,omitempty"`

	// Sort by one of supported fields, format +|-(field)
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty" validate:"omitempty,sortBy"`

	// Page number
	// Required: false
	Page uint64 `url:"page,omitempty" json:"page,omitempty"`

	// Page size
	// Required: false
	Size uint64 `url:"size,omitempty" json:"size,omitempty"`
}

// GetPCIDevices gets list of PCI devices on a node as a ListPCIDevices struct
func (n Node) GetPCIDevices(ctx context.Context, req GetPCIDevicesRequest) (*ListPCIDevices, error) {
	res, err := n.GetPCIDevicesRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	list := ListPCIDevices{}

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

	return &list, nil
}

// GetPCIDevicesRaw gets list of PCI devices on a node as an array of bytes
func (n Node) GetPCIDevicesRaw(ctx context.Context, req GetPCIDevicesRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/node/get_pci_devices"

	res, err := n.client.DecortApiCall(ctx, http.MethodGet, url, req)
	return res, err
}
