// const express = require("express");
// const router = express.Router();
// const accountsPayableController = require("../../controllers/accounts/accountsPayableController");
// const authenticate = require("../../middlewares/authenticate");
// const upload = require("../../middlewares/upload"); 

// // ==================== GET ROUTES ====================



// // Get counts for both tabs
// router.get("/grns/counts", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Get payable GRN counts'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.getPayableCounts(req, res);
// });

// // Get all payable GRNs (with pagination and tab filtering)
// router.get("/grns", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Get all payable GRNs'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.getPayableGrns(req, res);
// });

// // Get single payable GRN by ID
// router.get("/grn/:id", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Get payable GRN by ID'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.getPayableGrnById(req, res);
// });

// // Get payment transactions for a GRN
// router.get("/transactions/:grnId", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Get payment transactions for GRN'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.getPaymentTransactions(req, res);
// });

// // Get vendor details by vendor name
// router.get("/vendor/:vendorName", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Get vendor details by name'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.getVendorDetails(req, res);
// });

// // ==================== PUT/POST ROUTES ====================

// // Update payment details (status, approval, payment mode)
// router.put("/update-payment/:id", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Update payment status'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.updatePaymentStatus(req, res);
// });

// // Update final invoice amount
// router.put("/update-final-amount/:id", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Update final invoice amount'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.updateFinalAmount(req, res);
// });

// // Add payment transaction (with time)
// router.post("/add-transaction", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Add payment transaction'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.addTransaction(req, res);
// });

// // Reconcile GRN (move to matched tab)
// router.post("/reconcile/:id", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Reconcile GRN'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.reconcileGrn(req, res);
// });

// // Upload invoice file - using your custom upload middleware
// router.post("/upload-invoice", authenticate, upload.single("invoiceFile"), (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Upload invoice file'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.uploadInvoiceFile(req, res);
// });

// // ==================== DELETE ROUTES ====================

// // Delete GRN (soft delete)
// router.delete("/grn/:id", authenticate, (req, res) => {
//   /*
//     #swagger.tags = ['Accounts Payable']
//     #swagger.summary = 'Delete GRN'
//     #swagger.security = [{ "bearerAuth": [] }]
//   */
//   accountsPayableController.deletePayableGrn(req, res);
// });

// module.exports = router;

/**
 * routes/accountsPayableRoutes.js
 *
 * Mount in your main app:
 *   const apRoutes = require("./routes/accountsPayableRoutes");
 *   app.use("/api/accounts-payable", authMiddleware, apRoutes);
 */

const express = require("express");
const router  = express.Router();

const {
  getPayableGRNs,
  getTabCounts,
  updateFinalAmount,
  reconcileGRN,
  updatePayment,
  addTransaction,
  uploadInvoice,
  removeFromPayables,
  getPayableSummary,
} = require("../../controllers/accounts/accountsPayableController");
const authenticate = require("../../middlewares/authenticate");
const upload = require("../../middlewares/upload");

/* ── Read ─────────────────────────────────────────────────────────────────── */

/**
 * GET /accounts-payable/grns
 * Fetch GRN-level payables.
 * Query: tab (1|2), page, limit, search, status
 */
router.get("/grns", authenticate, getPayableGRNs);

/**
 * GET /accounts-payable/tab-counts
 * Returns { tab1Count, tab2Count } — both tab badge numbers in one call.
 * Called on initial mount so both tab badges are accurate without fetching both pages.
 */
router.get("/tab-counts", authenticate, getTabCounts);

/* ── Mutations ────────────────────────────────────────────────────────────── */

/**
 * PUT /accounts-payable/update-final-amount/:grnId
 * Set vendor's invoice amount for a GRN.
 * Body: { finalAmount }
 * Auto-reconciles if finalAmount === grn_amount.
 */
router.put("/update-final-amount/:grnId", authenticate, updateFinalAmount);


// Get summary statistics for dashboard
router.get("/summary", authenticate,
  /*
    #swagger.tags = ['Accounts Payable']
    #swagger.summary = 'Get payable summary statistics'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  getPayableSummary
);
/**
 * POST /accounts-payable/reconcile/:grnId
 * Mark GRN as reconciled (moves to Tab 1).
 * Body: { matchedAmount }
 */
router.post("/reconcile/:grnId", authenticate, reconcileGRN);

/**
 * PUT /accounts-payable/update-payment/:grnId
 * Update GST/TDS toggles, payment mode, payment status.
 * Body: { applyGst, applyTds, paymentMode, status }
 */
router.put("/update-payment/:grnId", authenticate, updatePayment);

/**
 * POST /accounts-payable/add-transaction
 * Add a payment transaction against a GRN.
 * Body: { grnId, transaction: { date, time, amount, mode, note } }
 */
router.post("/add-transaction", authenticate, addTransaction);

/**
 * POST /accounts-payable/upload-invoice
 * Upload vendor invoice PDF/image for a GRN.
 * Form: multipart/form-data — fields: invoiceFile, grnId
 */
router.post("/upload-invoice", authenticate, upload.single("invoiceFile"), uploadInvoice);

/**
 * DELETE /accounts-payable/grn/:grnId
 * Remove a GRN from payables (resets AP fields).
 */
router.delete("/grn/:grnId", authenticate, removeFromPayables);

module.exports = router;