package node

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

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

// PCIDeviceDriverToKernelRequest struct to bind PCI device driver to kernel
type PCIDeviceDriverToKernelRequest struct {
	// Node ID
	// Required: true
	NodeID uint64 `url:"nid" json:"nid" validate:"required"`

	// Hardware path of the PCI device, e.g. 0000:81:00.0
	// Required: true
	HWPath string `url:"hw_path" json:"hw_path" validate:"required,pciDeviceHWPath"`

	// Mode of binding the PCI device driver to kernel
	// Default: safe
	// Required: false
	Mode string `url:"mode,omitempty" json:"mode,omitempty" validate:"omitempty,oneof=safe unsafe"`
}

// PCIDeviceDriverToKernel binds PCI device driver to kernel
func (n Node) PCIDeviceDriverToKernel(ctx context.Context, req PCIDeviceDriverToKernelRequest) (*RecordPCIDeviceDriver, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/node/pci_device_driver_to_kernel"

	res, err := n.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
	if err != nil {
		return nil, err
	}

	result := RecordPCIDeviceDriver{}

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

	return &result, nil
}
