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

// IMPORTANT: Specific routes must come BEFORE parameterized routes

// ─── CREATE ──────────────────────────────────────────────────────────────────
router.post(
  "/create",
  authenticate,
  // authorize(["admin", "superadmin", "manager", "accountant"]),
  (req, res) => {
    /*
      #swagger.tags = ['Journal Voucher']
      #swagger.summary = 'Create a new journal voucher'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              required: ["voucher_type", "voucher_date", "debit_account", "credit_account", "amount", "narration"],
              properties: {
                voucher_type:     { type: "string", enum: ["Journal","Payment","Receipt","Contra"] },
                voucher_date:     { type: "string", format: "date" },
                debit_account:    { type: "string" },
                credit_account:   { type: "string" },
                amount:           { type: "number" },
                narration:        { type: "string" },
                reference_number: { type: "string" },
                status:           { type: "string", enum: ["Pending","Approved","Posted"] }
              }
            }
          }
        }
      }
    */
    journalVoucherController.createJournalVoucher(req, res);
  }
);

// ─── GET ALL (without pagination) — for dropdowns / exports ──────────────────
router.get(
  "/all",
  authenticate,
  // authorize(["admin", "superadmin", "manager", "accountant"]),
  (req, res) => {
    /*
      #swagger.tags = ['Journal Voucher']
      #swagger.summary = 'Get all journal vouchers without pagination'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    journalVoucherController.getAllJournalVouchersWithoutPagination(req, res);
  }
);

// ─── GET ALL (paginated + search) ────────────────────────────────────────────
router.get(
  "/",
  authenticate,
  // authorize(["admin", "superadmin", "manager", "accountant"]),
  (req, res) => {
    /*
      #swagger.tags = ['Journal Voucher']
      #swagger.summary = 'Get all journal vouchers with pagination'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['search'] = { in: 'query', type: 'string', description: 'Search by voucher number, type, narration, accounts' }
      #swagger.parameters['page']   = { in: 'query', type: 'integer', description: 'Page number (default: 1)' }
      #swagger.parameters['limit']  = { in: 'query', type: 'integer', description: 'Items per page (default: 10)' }
    */
    journalVoucherController.getAllJournalVouchers(req, res);
  }
);

// ─── UPDATE STATUS ONLY — must come before /:id ───────────────────────────────
router.patch(
  "/:id/status",
  authenticate,
  // authorize(["admin", "superadmin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Journal Voucher']
      #swagger.summary = 'Update journal voucher status only'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              required: ["status"],
              properties: {
                status: { type: "string", enum: ["Pending","Approved","Posted"] }
              }
            }
          }
        }
      }
    */
    journalVoucherController.updateJournalVoucherStatus(req, res);
  }
);

// ─── HARD DELETE — must come before /:id ──────────────────────────────────────
router.delete(
  "/hard/:id",
  authenticate,
  // authorize(["admin", "superadmin"]),
  (req, res) => {
    /*
      #swagger.tags = ['Journal Voucher']
      #swagger.summary = 'Hard (permanent) delete journal voucher'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    journalVoucherController.hardDeleteJournalVoucher(req, res);
  }
);

// ─── GET BY ID ────────────────────────────────────────────────────────────────
router.get(
  "/:id",
  authenticate,
  // authorize(["admin", "superadmin", "manager", "accountant"]),
  (req, res) => {
    /*
      #swagger.tags = ['Journal Voucher']
      #swagger.summary = 'Get journal voucher by ID'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    journalVoucherController.getJournalVoucherById(req, res);
  }
);

// ─── UPDATE ───────────────────────────────────────────────────────────────────
router.put(
  "/:id",
  authenticate,
  // authorize(["admin", "superadmin", "manager", "accountant"]),
  (req, res) => {
    /*
      #swagger.tags = ['Journal Voucher']
      #swagger.summary = 'Update journal voucher (blocked if status is Posted)'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    journalVoucherController.updateJournalVoucher(req, res);
  }
);

// ─── SOFT DELETE ──────────────────────────────────────────────────────────────
router.delete(
  "/:id",
  authenticate,
  // authorize(["admin", "superadmin"]),
  (req, res) => {
    /*
      #swagger.tags = ['Journal Voucher']
      #swagger.summary = 'Soft delete journal voucher (blocked if status is Posted)'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    journalVoucherController.deleteJournalVoucher(req, res);
  }
);

module.exports = router;