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

const accountsReceivableController = require("../../controllers/accounts/accountsReceivableController");
const authenticate = require("../../middlewares/authenticate");

// ─────────────────────────────────────────────────────────────────────────────
// SALES INVOICES
// ─────────────────────────────────────────────────────────────────────────────

router.get("/invoices", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable']
    #swagger.summary = 'Get all receivable invoices'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.getReceivableInvoices(req, res);
});

router.get("/invoices/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable']
    #swagger.summary = 'Get invoice details with payments'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.getInvoiceWithPayments(req, res);
});

router.get("/summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable']
    #swagger.summary = 'Get receivable payment summary'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.getPaymentSummary(req, res);
});

router.post("/invoices/:id/payments", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable']
    #swagger.summary = 'Add payment against invoice'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.addPayment(req, res);
});

router.patch("/invoices/:id/payment-mode", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable']
    #swagger.summary = 'Update payment mode on invoice'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.updatePaymentMode(req, res);
});

router.delete("/invoices/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable']
    #swagger.summary = 'Soft delete an invoice'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.deleteInvoice(req, res);
});

router.delete("/invoices/:id/hard", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable']
    #swagger.summary = 'Permanently delete a receivable entry'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.hardDeleteReceivable(req, res);
});

router.delete("/payments/:paymentId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable']
    #swagger.summary = 'Delete a payment record'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.deletePayment(req, res);
});

// ─────────────────────────────────────────────────────────────────────────────
// PROFORMA INVOICES
// ─────────────────────────────────────────────────────────────────────────────

router.get("/proforma-invoices", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable - Proforma']
    #swagger.summary = 'Get all proforma invoices with advance payment status'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.getProformaInvoices(req, res);
});

router.get("/proforma-summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable - Proforma']
    #swagger.summary = 'Get proforma advance payment summary'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.getProformaSummary(req, res);
});

router.post("/proforma-invoices/:id/payments", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Accounts Receivable - Proforma']
    #swagger.summary = 'Record advance payment against proforma invoice'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  accountsReceivableController.addProformaPayment(req, res);
});

module.exports = router;