// routes/accounts/bankReconciliationRoutes.js

const express = require("express");
const router = express.Router();
const {
  uploadBankStatement,
  getReconciliationPayables,
  getBankTransactions,
  matchTransaction,
  cancelMatch,
  getReconciliationSummary,
} = require("../../controllers/accounts/bankReconciliationController");
const authenticate = require("../../middlewares/authenticate");

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

/**
 * GET /bank-reconciliation/payables
 * Get payables for reconciliation (GRNs with PO details and payment status)
 */
router.get("/payables", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Bank Reconciliation']
    #swagger.summary = 'Get payables for reconciliation'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  getReconciliationPayables(req, res);
});

/**
 * GET /bank-reconciliation/transactions
 * Get bank transactions for the most recent statement
 */
router.get("/transactions", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Bank Reconciliation']
    #swagger.summary = 'Get bank transactions from latest statement'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  getBankTransactions(req, res);
});

/**
 * GET /bank-reconciliation/transactions/:statementId
 * Get bank transactions for a specific statement ID
 */
router.get("/transactions/:statementId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Bank Reconciliation']
    #swagger.summary = 'Get bank transactions by statement ID'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  getBankTransactions(req, res);
});

/**
 * GET /bank-reconciliation/summary
 * Get reconciliation summary statistics
 */
router.get("/summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Bank Reconciliation']
    #swagger.summary = 'Get reconciliation summary'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  getReconciliationSummary(req, res);
});

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

/**
 * POST /bank-reconciliation/upload
 * Upload and parse bank statement PDF
 * Body: multipart/form-data with file, statementDate
 */
router.post("/upload", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Bank Reconciliation']
    #swagger.summary = 'Upload bank statement PDF'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.consumes = ['multipart/form-data']
    #swagger.parameters['file'] = {
      in: 'formData',
      type: 'file',
      required: true,
      description: 'Bank statement PDF file'
    }
    #swagger.parameters['statementDate'] = {
      in: 'formData',
      type: 'string',
      required: false,
      description: 'Statement date (YYYY-MM-DD)'
    }
  */
  uploadBankStatement(req, res);
});

/**
 * POST /bank-reconciliation/match
 * Match a bank transaction to a payable (GRN)
 * Body: { transactionId, grnId, amount }
 */
router.post("/match", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Bank Reconciliation']
    #swagger.summary = 'Match bank transaction to payable'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['body'] = {
      in: 'body',
      required: true,
      schema: {
        transactionId: 1,
        grnId: 100,
        amount: 125000
      }
    }
  */
  matchTransaction(req, res);
});

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

/**
 * DELETE /bank-reconciliation/match/:matchId
 * Cancel a reconciliation match
 */
router.delete("/match/:matchId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Bank Reconciliation']
    #swagger.summary = 'Cancel a reconciliation match'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  cancelMatch(req, res);
});

module.exports = router;