package infra

import (
	"database/sql"
	"fmt"
	"net/http"
	"strings"
)

// CRUDRegisterRoutes registra en mux las 5 rutas REST del recurso usando la sintaxis
// "METHOD /path" de Go 1.22+. basePath es el prefijo de las rutas (ej: "/api/projects").
// Rutas generadas:
//   GET    {basePath}
//   GET    {basePath}/{id}
//   POST   {basePath}
//   PUT    {basePath}/{id}
//   DELETE {basePath}/{id}
func CRUDRegisterRoutes(mux *http.ServeMux, basePath string, res CRUDResource, db *sql.DB) {
	basePath = strings.TrimRight(basePath, "/")
	handlers := CRUDGenerateHandlers(res, db)
	mux.Handle(fmt.Sprintf("GET %s", basePath), handlers["list"])
	mux.Handle(fmt.Sprintf("GET %s/{id}", basePath), handlers["get"])
	mux.Handle(fmt.Sprintf("POST %s", basePath), handlers["create"])
	mux.Handle(fmt.Sprintf("PUT %s/{id}", basePath), handlers["update"])
	mux.Handle(fmt.Sprintf("DELETE %s/{id}", basePath), handlers["delete"])
}
