package api

import (
	"log/slog"
	"net/http"
	"sort"
)

// listProxyRoutes handles GET /api/proxies. Returns proxy routes derived
// from the containers index — the legacy static-site / project split is
// gone; any workload whose container carries a proxy route ID is listed.
func (s *Server) listProxyRoutes(w http.ResponseWriter, r *http.Request) {
	settings, err := s.store.GetSettings()
	if err != nil {
		slog.Error("failed to get settings for proxy routes", "error", err)
		respondError(w, http.StatusInternalServerError, "internal server error")
		return
	}

	routes, err := s.store.ListProxyRoutes(settings.Domain)
	if err != nil {
		slog.Error("failed to list proxy routes", "error", err)
		respondError(w, http.StatusInternalServerError, "internal server error")
		return
	}

	sort.SliceStable(routes, func(i, j int) bool {
		if routes[i].Domain == routes[j].Domain {
			return routes[i].ProjectName < routes[j].ProjectName
		}
		return routes[i].Domain < routes[j].Domain
	})

	respondJSON(w, http.StatusOK, routes)
}
