package compute

import (
	"context"
	"net/http"
	"strconv"

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

// PinToNodeRequest struct to pin compute to node
type PinToNodeRequest struct {
	// ID of the compute instance
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// Node ID to pin to
	// Required: false
	TargetNodeID uint64 `url:"targetNodeId" json:"targetNodeId"`

	// Try to migrate or not if compute in running states
	// Required: false
	Force bool `url:"force" json:"force"`

	// Auto start when node restarted
	// Required: false
	// Default: false
	AutoStart bool `url:"autoStart" json:"autoStart"`
}

// PinToNode pins compute to current node
func (c Compute) PinToNode(ctx context.Context, req PinToNodeRequest) (uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return 0, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/compute/pin_to_node"

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

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

	return result, nil
}
