package node

import (
	"context"
	"net/http"

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

// EnableRequest struct to enable node from maintenance status to enabled
type EnableRequest struct {
	// Node ID
	// Required: true
	NID uint64 `url:"nid" json:"nid" validate:"required"`

	// Message
	// Required: false
	Message string `url:"message,omitempty" json:"message,omitempty"`

	// Do not check locks, iscsi-sessions or disk-present
	// Default: false
	// Required: false
	Force interface{} `url:"force" json:"force" validate:"isBool"`

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

type wrapperEnableRequest struct {
	EnableRequest

	AsyncMode bool `url:"asyncMode"`
}

// Enable enables node from maintenance status to enabled
func (n Node) Enable(ctx context.Context, req EnableRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	if req.Force == nil {
		req.Force = false
	}

	reqWrapped := wrapperEnableRequest{
		EnableRequest: req,
		AsyncMode:     false,
	}

	url := "/cloudbroker/node/enable"

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

	return string(res), nil
}

// EnableAsync enables node from maintenance status to enabled with AsyncMode
func (n Node) EnableAsync(ctx context.Context, req EnableRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	if req.Force == nil {
		req.Force = false
	}

	reqWrapped := wrapperEnableRequest{
		EnableRequest: req,
		AsyncMode:     true,
	}

	url := "/cloudbroker/node/enable"

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

	return string(res), nil
}
