package node

import (
	"context"
	"net/http"

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

// MaintenanceRequest struct to place node in maintenance state
type MaintenanceRequest struct {
	// Node ID
	// Required: true
	NID uint64 `url:"nid" json:"nid" validate:"required"`

	// VM Action
	// Default: stop
	// Required: false
	VMAction string `url:"vmaction,omitempty" json:"vmaction,omitempty" validate:"omitempty,vmaction"`

	// Offline
	// Default: false
	// Required: false
	Offline bool `url:"offline" json:"offline"`

	// Reason
	// Required: false
	Reason string `url:"reason,omitempty" json:"reason,omitempty"`

	// Allow node auto-enable
	// Default: false
	// Required: false
	AutoStart bool `url:"autostart" json:"autostart"`
}

// Maintenance places node in maintenance state
func (n Node) Maintenance(ctx context.Context, req MaintenanceRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/node/maintenance"

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

	return string(res), nil
}
