import { Router } from "express";

import { reportsController } from "../controllers/reportsController.js";

const router = Router();
const asyncHandler = (handler) => (req, res, next) =>
  Promise.resolve(handler(req, res, next)).catch(next);

const bindReportKey = (reportKey) => asyncHandler((req, res, next) => {
  req.params.reportKey = reportKey;
  return reportsController.get(req, res, next);
});

router.get("/", asyncHandler(reportsController.list));
router.get("/brouillard", bindReportKey("brouillard"));
router.get("/journal", bindReportKey("journal"));
router.get("/grand-livre", bindReportKey("grand-livre"));
router.get("/balance", bindReportKey("balance"));
router.get("/balance-auxiliaire", bindReportKey("balance-auxiliaire"));
router.get("/aged-clients", bindReportKey("aged-clients"));
router.get("/aged-suppliers", bindReportKey("aged-suppliers"));
router.get("/cash-state", bindReportKey("cash-state"));
router.get("/bank-reconciliation-state", bindReportKey("bank-reconciliation-state"));
router.get("/assets-book", bindReportKey("assets-book"));
router.get("/:reportKey", asyncHandler(reportsController.get));

export default router;
