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

// CreateRequest struct to creating PCI device
type CreateRequest struct {
	// NodeID
	// Required: true
	NodeID uint64 `url:"node_id" json:"node_id" validate:"required"`

	// Name of device
	// Required: true
	Name string `url:"name" json:"name" validate:"required"`

	// PCI address of the device
	// Must be in format 0000:1f:2b.0
	// Required: true
	HWPath string `url:"hwPath" json:"hwPath" validate:"required,hwPath"`

	// Description, just for information
	// Required: false
	Description string `url:"description,omitempty" json:"description,omitempty"`

	// Account IDs
	// Required: false
	AccountIDs []uint64 `url:"account_ids,omitempty" json:"account_ids,omitempty"`
}

// Create creates PCI Device
func (p PCIDevice) Create(ctx context.Context, req CreateRequest) (uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return 0, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/pcidevice/create"

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

	result, err := strconv.ParseUint(string(res), 10, 64)
	if err != nil {
		return 0, err
	}

	return result, nil
}
