package api

import (
	"net/http"

	"github.com/pulse-lets-go/internal/models"
)

// handleGetNodes — GET /api/nodes — список всех нод с текущими score.
func (a *API) handleGetNodes(w http.ResponseWriter, r *http.Request) {
	nodes := a.engine.GetAllNodes()
	writeJSON(w, http.StatusOK, nodes)
}

// handleGetNodeMetrics — GET /api/nodes/{id}/metrics — история метрик ноды из ring buffer.
func (a *API) handleGetNodeMetrics(w http.ResponseWriter, r *http.Request) {
	nodeID := r.PathValue("id")
	if nodeID == "" {
		writeError(w, http.StatusBadRequest, "не указан id ноды")
		return
	}

	snapshots := a.engine.GetNodeHistory(nodeID)
	writeJSON(w, http.StatusOK, map[string]interface{}{
		"node_id":   nodeID,
		"snapshots": snapshots,
		"count":     len(snapshots),
	})
}

// handleToggleNode — PUT /api/nodes/{id}/toggle — включение/выключение ноды.
func (a *API) handleToggleNode(w http.ResponseWriter, r *http.Request) {
	nodeID := r.PathValue("id")
	if nodeID == "" {
		writeError(w, http.StatusBadRequest, "не указан id ноды")
		return
	}

	var req models.ToggleNodeRequest
	if err := decodeJSON(r, &req); err != nil {
		writeError(w, http.StatusBadRequest, "некорректный JSON")
		return
	}

	if err := a.engine.ToggleNode(nodeID, req.Disabled, req.Reason); err != nil {
		writeError(w, http.StatusNotFound, err.Error())
		return
	}

	a.PushRouteCache()

	info, _ := a.engine.GetNodeInfo(nodeID)
	writeJSON(w, http.StatusOK, info)
}
