import express from "express";
import {
  listRecords,
  getRecordByPk,
  createRecord,
  updateRecordByPk,
  deleteRecordByPk
} from "../controllers/genericCrudController.js";

/**
 * Builds a REST CRUD router for `tableName`. Supports single or composite
 * primary keys: the path segment is `/:pk1/:pk2/...` in declared order.
 */
export function createCrudRouter(tableName, primaryKeys) {
  const router = express.Router();

  const pkPath = primaryKeys.map((k) => `:${k}`).join("/");

  router.get("/", listRecords(tableName));
  router.get(`/${pkPath}`, getRecordByPk(tableName, primaryKeys));
  router.post("/", createRecord(tableName));
  router.put(`/${pkPath}`, updateRecordByPk(tableName, primaryKeys));
  router.delete(`/${pkPath}`, deleteRecordByPk(tableName, primaryKeys));

  return router;
}
