// const express = require("express");
// const router = express.Router();
// const requisitionController = require("../../controllers/procurement/requisitionController");
// const authenticate = require("../../middlewares/authenticate");

// // ========== SPECIFIC ROUTES (MUST COME FIRST) ==========

// // CREATE
// router.post(
//   "/create",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Create a new requisition'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.createRequisition(req, res);
//   }
// );

// // GET ALL (with pagination)
// router.get(
//   "/",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Get all requisitions with pagination'
//       #swagger.security = [{ "bearerAuth": [] }]
//       #swagger.parameters['search'] = { in: 'query', type: 'string' }
//       #swagger.parameters['page'] = { in: 'query', type: 'integer' }
//       #swagger.parameters['limit'] = { in: 'query', type: 'integer' }
//     */
//     requisitionController.getAllRequisitions(req, res);
//   }
// );

// // GET ALL STOCK SUMMARY FOR DASHBOARD
// router.get(
//   "/stock-summary",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Get stock summary for dashboard'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.getStockSummary(req, res);
//   }
// );

// // GET ALL INVENTORY ISSUES (for Issues tab)
// router.get(
//   "/inventory-issues",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Get all inventory issues'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.getAllInventoryIssues(req, res);
//   }
// );

// // GET ALL (without pagination)
// router.get(
//   "/all",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Get all requisitions without pagination'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.getAllRequisitionsWithoutPagination(req, res);
//   }
// );

// // GET STOCK LEDGER FOR MATERIAL
// router.get(
//   "/stock-ledger/:materialName",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Get stock ledger for material'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.getStockLedger(req, res);
//   }
// );

// // GET STOCK AVAILABILITY FOR REQUISITION
// router.get(
//   "/:id/stock-availability",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Get stock availability for requisition'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.getStockAvailabilityForPR(req, res);
//   }
// );

// router.get(
//   '/:id/issued-inventory',
//   authenticate,
//   requisitionController.getIssuedInventoryDetails
// );

// // GET ISSUE HISTORY
// router.get(
//   "/:id/issue-history",
//   authenticate,
//   requisitionController.getIssueHistory
// );

// // GET ISSUE HISTORY
// router.get(
//   "/:id/issue-history",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Get issue history for requisition'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.getIssueHistory(req, res);
//   }
// );

// // GET BY ID (MUST COME AFTER SPECIFIC ROUTES)
// router.get(
//   "/:id",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Get requisition by ID'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.getRequisitionById(req, res);
//   }
// );

// // UPDATE STATUS
// router.put(
//   "/:id/status",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Update requisition status'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.updateRequisitionStatus(req, res);
//   }
// );

// // UPDATE COMMENT
// router.put(
//   "/:id/comment",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Update requisition comment'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.updateRequisitionComment(req, res);
//   }
// );

// // UPDATE ITEMS
// router.put(
//   "/:id/items",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Update requisition items'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.updateRequisitionItems(req, res);
//   }
// );

// // // ISSUE FROM INVENTORY
// // router.post(
// //   "/:id/issue-from-inventory",
// //   authenticate,
// //   (req, res) => {
// //     /*
// //       #swagger.tags = ['Requisition Postgre']
// //       #swagger.summary = 'Issue items from inventory'
// //       #swagger.security = [{ "bearerAuth": [] }]
// //     */
// //     requisitionController.issueFromInventory(req, res);
// //   }
// // );

// // SEND QUOTATION
// router.post(
//   "/:id/send-quotation",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Send quotation request'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.sendQuotationRequest(req, res);
//   }
// );


// router.put('/requisitions/:requisitionId/pr-inventory/consumption-status', 
//   authenticate, 
//   requisitionController.updatePRInventoryConsumptionStatus
// );
// // SOFT DELETE
// router.delete(
//   "/:id",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Soft delete requisition'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.deleteRequisition(req, res);
//   }
// );

// // HARD DELETE
// router.delete(
//   "/hard/:id",
//   authenticate,
//   (req, res) => {
//     /*
//       #swagger.tags = ['Requisition Postgre']
//       #swagger.summary = 'Hard delete requisition (permanent)'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     requisitionController.hardDeleteRequisition(req, res);
//   }
// );


// // Add this route with your existing routes
// router.put(
//   '/requisitions/:requisitionId/update-po-quantity',
//   authenticate,
// (req,res)=>{


//    requisitionController.updatePOraisedQuantity(req,res)
// }
//   );

//   // In requisitionRoutes.js, add this route:

// // Get PO status for PR items (for CreatePO page)
// router.get('/:pr_id/po-status', authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Requisitions']
//     #swagger.summary = 'Get PO status for requisition items'
//     #swagger.description = 'Returns items with their PO raised status'
//   */
//   requisitionController.getPRItemPOStatus(req, res);
// });


// // ==================== PR INVENTORY ROUTES (NEW) ====================

// // Approve PR with stock entry (pushes to pr_inventory)
// router.post('/:id/approve-with-stock', authenticate, requisitionController.approveRequisitionWithStock);

// // Get approved PRs for inventory management
// router.get('/inventory/approved', authenticate, requisitionController.getApprovedPRsForInventory);

// // Issue inventory from PR inventory
// router.post('/:id/issue-inventory', authenticate, requisitionController.issueInventoryFromPR);

// // Approve PR for PO (from inventory page)
// router.post('/:id/approve-for-po', authenticate, requisitionController.approvePRForPO);



// router.post('/:id/approve-items-for-po', authenticate, requisitionController.approveItemsForPO);


// module.exports = router;

const express = require("express");
const router = express.Router();
const requisitionController = require("../../controllers/procurement/requisitionController");
const authenticate = require("../../middlewares/authenticate");

// ========== SPECIFIC ROUTES (MUST COME FIRST) ==========

// CREATE
router.post("/create", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Create a new requisition' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.createRequisition(req, res);
});

// GET ALL (with pagination)
router.get("/", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get all requisitions with pagination' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getAllRequisitions(req, res);
});

// GET ALL STOCK SUMMARY FOR DASHBOARD
router.get("/stock-summary", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get stock summary for dashboard' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getStockSummary(req, res);
});

// GET ALL INVENTORY ISSUES
router.get("/inventory-issues", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get all inventory issues' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getAllInventoryIssues(req, res);
});

// GET ALL (without pagination)
router.get("/all", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get all requisitions without pagination' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getAllRequisitionsWithoutPagination(req, res);
});

// GET APPROVED PRs FOR INVENTORY
router.get("/inventory/approved", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get approved PRs for inventory management' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getApprovedPRsForInventory(req, res);
});

// GET STOCK LEDGER FOR MATERIAL
router.get("/stock-ledger/:materialName", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get stock ledger for a material' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getStockLedger(req, res);
});

// ========== REQUISITION-SPECIFIC ROUTES ==========

// GET STOCK AVAILABILITY
router.get("/:id/stock-availability", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get stock availability for a requisition' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getStockAvailabilityForPR(req, res);
});

// GET ISSUED INVENTORY DETAILS
router.get("/:id/issued-inventory", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get issued inventory details for a requisition' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getIssuedInventoryDetails(req, res);
});

// GET ISSUE HISTORY
router.get("/:id/issue-history", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get issue history for a requisition' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getIssueHistory(req, res);
});

// GET PO STATUS
router.get("/:pr_id/po-status", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get PO status for requisition items' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getPRItemPOStatus(req, res);
});

// UPDATE STATUS
router.put("/:id/status", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Update requisition status' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.updateRequisitionStatus(req, res);
});

// UPDATE COMMENT
router.put("/:id/comment", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Update requisition comment' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.updateRequisitionComment(req, res);
});

// UPDATE ITEMS
router.put("/:id/items", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Update requisition items' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.updateRequisitionItems(req, res);
});

// SEND QUOTATION
router.post("/:id/send-quotation", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Send quotation request' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.sendQuotationRequest(req, res);
});

// APPROVE WITH STOCK
router.post("/:id/approve-with-stock", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Approve requisition and move to PR inventory' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.approveRequisitionWithStock(req, res);
});

// ISSUE INVENTORY
router.post("/:id/issue-inventory", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Issue inventory from PR' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.issueInventoryFromPR(req, res);
});

// APPROVE FOR PO
router.post("/:id/approve-for-po", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Approve PR for PO creation' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.approvePRForPO(req, res);
});

// APPROVE ITEMS FOR PO
router.post("/:id/approve-items-for-po", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Approve specific items for PO' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.approveItemsForPO(req, res);
});

// UPDATE CONSUMPTION STATUS
router.put("/:requisitionId/pr-inventory/consumption-status", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Update PR inventory consumption status' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.updatePRInventoryConsumptionStatus(req, res);
});

// UPDATE PO QUANTITY
router.put("/:requisitionId/update-po-quantity", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Update PO raised quantity' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.updatePOraisedQuantity(req, res);
});

// SOFT DELETE
router.delete("/:id", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Soft delete requisition' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.deleteRequisition(req, res);
});

// HARD DELETE
router.delete("/hard/:id", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Hard delete requisition (permanent)' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.hardDeleteRequisition(req, res);
});

// ========== GENERIC GET BY ID (MUST COME LAST) ==========
router.get("/:id", authenticate, (req, res) => {
  /* #swagger.tags = ['Requisition'] */
  /* #swagger.summary = 'Get requisition by ID' */
  /* #swagger.security = [{ "bearerAuth": [] }] */
  requisitionController.getRequisitionById(req, res);
});

module.exports = router;