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"
)

// PCIDeviceDriverToVFIORequest struct to bind PCI device driver to VFIO
type PCIDeviceDriverToVFIORequest 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"`

	// Driver binding mode
	// Required: true
	// Possible values: safe, unsafe
	Mode string `url:"mode" json:"mode" validate:"required,oneof=safe unsafe"`
}

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

	url := "/cloudbroker/node/pci_device_driver_to_vfio"

	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
}
