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

// // Create Payroll
// router.post("/invoice/create", authenticate, authorize(["admin", "manager"]), (req, res) => {
//   // #swagger.tags = ['Invoice']
//   //  #swagger.summary = 'Create a new invoice'
//   //  #swagger.security = [{ "bearerAuth": [] }]
//   invoiceController.createInvoice(req, res);
// });

// // fetchAllPayrolls
// router.get("/fetchAllInvoices", authenticate, authorize(["admin", "manager"]), (req, res) => {
//   /*
//     #swagger.tags = ['Invoice']
//     #swagger.summary = 'Get all invoices with pagination'
//     #swagger.security = [{ "bearerAuth": [] }]
//     #swagger.parameters['page'] = { description: 'Page number', in: 'query' }
//     #swagger.parameters['limit'] = { description: 'Number of invoices per page', in: 'query' }
//   */
//   invoiceController.fetchAllInvoices(req, res)
// })


// // delete PayRollById
// router.delete("/invoice/delete/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
//   /*
//     #swagger.tags = ['Invoice']
//     #swagger.summary = 'Soft delete a invoice by ID'
//     #swagger.security = [{ "bearerAuth": [] }]
//     #swagger.parameters['id'] = { description: 'Invoice ID', required: true }
//   */
//   invoiceController.deleteInvoiceById(req, res)
// })

// // update PayRollById
// router.put("/invoice/update/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
//   /*
//     #swagger.tags = ['Invoice']
//     #swagger.summary = 'Soft delete a invoice by ID'
//     #swagger.security = [{ "bearerAuth": [] }]
//     #swagger.parameters['id'] = { description: 'Invoice ID', required: true }
//   */
//   invoiceController.updateInvoiceById(req, res)
// })

// // Fetch invoice using taskId
// // Reason: UI navigates with taskId, not invoiceId
// router.get("/task/:taskId",authenticate, authorize(["admin", "manager"]), (req, res) =>
//   /*
//     #swagger.tags = ['Invoice']     
//   getInvoiceByTaskId
//     #swagger.summary = 'Get invoice by Task ID'
//     #swagger.security = [{ "bearerAuth": [] }]
//     #swagger.parameters['taskId'] = { description: 'Task ID', required: true }
//   */
//   invoiceController.getInvoiceByTaskId(req, res)
// );

// // routes/invoiceRoutes.js

// router.get("/view/task/:taskId", authenticate, authorize(["admin", "manager"]), (req, res) =>
//   /*
//     #swagger.tags = ['Invoice']
//     #swagger.summary = 'Get invoice view details by Task ID'  
//   */
//   invoiceController.getInvoiceViewDetails(req, res)
// );



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




// /* CREATE (manual if needed) */
// router.post(
//   "/invoice",
//   authenticate,
//   authorize(["admin", "manager"]),
//   invoiceController.createInvoice
// );

// /* FETCH ALL */
// router.get(
//   "/invoice",
//   authenticate,
//   authorize(["admin", "manager"]),
//   invoiceController.fetchAllInvoices
// );

// /* UPDATE */
// router.put(
//   "/invoice/:id",
//   authenticate,
//   authorize(["admin", "manager"]),
//   invoiceController.updateInvoiceById
// );

// /* DELETE */
// router.delete(
//   "/invoice/:id",
//   authenticate,
//   authorize(["admin", "manager"]),
//   invoiceController.deleteInvoiceById
// );

// /* 🔹 MAIN VIEW API (TASK → INVOICE) */
// // router.get(
// //   "/invoice/task/:taskId/view",
// //   authenticate,
// //   authorize(["admin", "manager"]),
// //   invoiceController.getOrCreateInvoiceByTask
// // );


// // DOWNLOAD INVOICE PDF
// router.get(
//   "/invoice/:invoiceId/download",
//   authenticate,
//   authorize(["admin", "manager"]),
//   invoiceController.downloadInvoicePdf
// );


// // just for time being commented 02-01-26
// router.get(
//   "/invoice/task/:taskId",
//   authenticate,
//   authorize(["admin", "manager"]),
//   invoiceController.getInvoiceByTaskId
// );

// router.post(
//   "/invoice/task/:taskId",
//   authenticate,
//   authorize(["admin"]),
//   invoiceController.createInvoiceByTask
// );

// router.put(
//   "/invoice/:id/status",
//   authenticate,
//   authorize(["admin"]),
//   invoiceController.updateInvoiceStatus
// );

// router.put(
//   "/invoice/:id/issue",
//   authenticate,
//   authorize(["admin", "manager"]),
//   invoiceController.issueInvoice
// );

// router.put(
//   "/invoice/:id/update-discount",
//   authenticate,
//   authorize(["admin", "accounts", "manager"]),
//   invoiceController.updateInvoiceDiscount
// );

// router.get(
//   "/invoice/:invoiceId/statement/pdf",
//   authenticate,
//   authorize(["admin", "manager"]),
//   invoiceController.downloadInvoiceStatementPdf
// );

// router.get(
//   "/reminders",
//   authenticate,
//   authorize(["admin", "accounts"]),
//   invoiceController.getInvoiceReminders
// );


// router.get(
//   "/customer-statement/pdf",
//   authenticate,
//   authorize(["admin", "accounts"]),
//   invoiceController.downloadCustomerStatementPdf
// );



/**
 * @swagger
 * tags:
 *   name: Invoice
 *   description: Invoice management APIs
 */


router.post(
  "/invoice",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Create invoice manually'
    // #swagger.description = 'Creates a new invoice manually by admin or manager'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.createInvoice(req, res);
  }
);

/* ===================== FETCH ALL INVOICES ===================== */
router.get(
  "/invoice",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Fetch all invoices'
    // #swagger.description = 'Get list of all invoices for the organization'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.fetchAllInvoices(req, res);
  }
);

/* ===================== UPDATE INVOICE ===================== */
router.put(
  "/invoice/:id",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Update invoice'
    // #swagger.description = 'Update invoice details using invoice ID'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.updateInvoiceById(req, res);
  }
);

/* ===================== DELETE INVOICE ===================== */
router.delete(
  "/invoice/:id",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Delete invoice'
    // #swagger.description = 'Soft delete an invoice by ID'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.deleteInvoiceById(req, res);
  }
);

/* ===================== DOWNLOAD INVOICE PDF ===================== */
router.get(
  "/invoice/:invoiceId/download",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Download invoice PDF'
    // #swagger.description = 'Download invoice PDF using invoice ID'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.downloadInvoicePdf(req, res);
  }
);

/* ===================== GET INVOICE BY TASK ===================== */
router.get(
  "/invoice/task/:taskId",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Get invoice by task ID'
    // #swagger.description = 'Fetch invoice linked to a specific task'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.getInvoiceByTaskId(req, res);
  }
);

/* ===================== CREATE INVOICE BY TASK ===================== */
router.post(
  "/invoice/task/:taskId",
  authenticate,
  authorize(["admin"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Create invoice from task'
    // #swagger.description = 'Create a new invoice using task ID'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.createInvoiceByTask(req, res);
  }
);

/* ===================== UPDATE INVOICE STATUS ===================== */
router.put(
  "/invoice/:id/status",
  authenticate,
  authorize(["admin"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Update invoice status'
    // #swagger.description = 'Update status of invoice (Paid, Pending, Overdue)'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.updateInvoiceStatus(req, res);
  }
);

/* ===================== ISSUE INVOICE ===================== */
router.put(
  "/invoice/:id/issue",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Issue invoice'
    // #swagger.description = 'Mark invoice as issued'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.issueInvoice(req, res);
  }
);

/* ===================== UPDATE INVOICE DISCOUNT ===================== */
router.put(
  "/invoice/:id/update-discount",
  authenticate,
  authorize(["admin", "accounts", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Update invoice discount'
    // #swagger.description = 'Update discount applied on an invoice'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.updateInvoiceDiscount(req, res);
  }
);

/* ===================== DOWNLOAD INVOICE STATEMENT PDF ===================== */
router.get(
  "/invoice/:invoiceId/statement/pdf",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Download invoice statement PDF'
    // #swagger.description = 'Download statement PDF for a specific invoice'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.downloadInvoiceStatementPdf(req, res);
  }
);

/* ===================== INVOICE REMINDERS ===================== */
router.get(
  "/reminders",
  authenticate,
  authorize(["admin", "accounts"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Get invoice reminders'
    // #swagger.description = 'Fetch invoices pending for reminders'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.getInvoiceReminders(req, res);
  }
);

/* ===================== CUSTOMER STATEMENT PDF ===================== */
router.get(
  "/customer-statement/pdf",
  authenticate,
  authorize(["admin", "accounts"]),
  (req, res) => {
    // #swagger.tags = ['Invoice']
    // #swagger.summary = 'Download customer statement PDF'
    // #swagger.description = 'Generate customer-wise invoice statement PDF'
    // #swagger.security = [{ "bearerAuth": [] }]
    return invoiceController.downloadCustomerStatementPdf(req, res);
  }
);



module.exports = router;
