package node

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

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

// AutoStartRequest struct to set node autostart
type AutoStartRequest struct {
	// Node ID
	// Required: true
	NodeID uint64 `url:"node_id" json:"node_id" validate:"required"`

	// Auto start
	// Required: true
	AutoStart bool `url:"autostart" json:"autostart" validate:"required"`
}

// AutoStart sets node autostart
func (n Node) AutoStart(ctx context.Context, req AutoStartRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/node/autostart"

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

	result, err := strconv.ParseBool(string(res))
	if err != nil {
		return false, err
	}

	return result, nil
}
