package system

import (
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/pingcap-inc/nexuscrm-api-host/dialect"
)

// SystemHandler handles system-level version discovery endpoints.
type SystemHandler struct{}

func NewSystemHandler() *SystemHandler {
	return &SystemHandler{}
}

func (h *SystemHandler) VersionsFor(mounts []dialect.Mount) gin.HandlerFunc {
	return func(c *gin.Context) {
		c.JSON(http.StatusOK, versionRows(mounts))
	}
}

func versionRows(mounts []dialect.Mount) []gin.H {
	labels := map[string]string{
		"v59.0": "Winter '24",
		"v60.0": "Spring '24",
		"v61.0": "Summer '24",
		"v62.0": "Winter '25",
		"v63.0": "Spring '25",
	}
	rows := make([]gin.H, 0, len(mounts))
	for _, m := range mounts {
		label := labels[m.Version]
		if label == "" {
			label = m.Version
		}
		// url is the authoritative mounted path from the dialect binder, not a
		// reconstruction that assumes a concrete dialect root.
		rows = append(rows, gin.H{
			"label":   label,
			"url":     m.Path,
			"version": strings.TrimPrefix(m.Version, "v"),
		})
	}
	return rows
}
