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

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

/**
 * @swagger
 * tags:
 *   name: Expenses
 *   description: Expense management APIs
 */

/* =====================================================
   CREATE EXPENSE
   Admin / Manager
   POST /expenses
===================================================== */
router.post(
  "/create",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Expenses']
      #swagger.summary = 'Create a new expense'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          expenseNumber: "EXP-1001",
          category: "Office",
          reimbursementType: "REIMBURSABLE",
          amount: 1250,
          expenseDate: "2025-07-15",
          notes: "Office electricity bill",
          attachments: [
            { url: "/uploads/bill.pdf", fileType: "application/pdf" }
          ]
        }
      }
    */
    expenseController.createExpense(req, res);
  }
);

/* =====================================================
   GET ALL EXPENSES
   Admin / Manager
   GET /expenses
===================================================== */
router.get(
  "/expenses",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Expenses']
      #swagger.summary = 'Get all expenses'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['status'] = {
        in: 'query',
        description: 'Filter by status (PENDING / PAID / REJECTED)'
      }
      #swagger.parameters['category'] = {
        in: 'query',
        description: 'Filter by category'
      }
    */
    expenseController.getAllExpenses(req, res);
  }
);

/* =====================================================
   GET SINGLE EXPENSE
   Admin / Manager
   GET /expenses/:id
===================================================== */
router.get(
  "/:id",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Expenses']
      #swagger.summary = 'Get expense by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Expense ID'
      }
    */
    expenseController.getExpenseById(req, res);
  }
);

/* =====================================================
   UPDATE EXPENSE (EDIT)
   Admin / Manager
   PUT /expenses/:id
===================================================== */
router.put(
  "/update/:id",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Expenses']
      #swagger.summary = 'Update an expense'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Expense ID'
      }
      #swagger.parameters['body'] = {
        in: 'body',
        description: 'Fields to update',
        schema: {
          category: "Travel",
          amount: 2200,
          notes: "Taxi reimbursement"
        }
      }
    */
    expenseController.updateExpense(req, res);
  }
);

/* =====================================================
   UPDATE EXPENSE STATUS
   Admin / Manager
   PATCH /expenses/:id/status
===================================================== */
router.put(
  "/:id/status",
  authenticate,
  authorize(["admin", "manager"]),  
  (req, res) => {
    /*
      #swagger.tags = ['Expenses']
      #swagger.summary = 'Update expense status'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Expense ID'
      }
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          status: "PAID"
        }
      }
    */
    expenseController.updateExpenseStatus(req, res);
  }
);

/* =====================================================
   DELETE EXPENSE (SOFT DELETE)
   Admin / Manager
   DELETE /expenses/:id
===================================================== */
router.delete(
  "/delete/:id",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Expenses']
      #swagger.summary = 'Delete an expense (soft delete)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Expense ID'
      }
    */
    expenseController.deleteExpense(req, res);
  }
);

module.exports = router;
