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

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

/**
 * @swagger
 * /budget-master/summary:
 *   get:
 *     summary: Get budget summary statistics
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.get("/summary", authenticate, (req, res) => {
budgetController.getBudgetSummary(req, res);
});

/**
 * @swagger
 * /budget-master/budgets:
 *   get:
 *     summary: Get all budgets with pagination
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.get("/budgets", authenticate, (req, res) => {
budgetController.getAllBudgets(req, res);
});

/**
 * @swagger
 * /budget-master/budgets/{id}:
 *   get:
 *     summary: Get single budget by ID with departments
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.get("/budgets/:id", authenticate, (req, res) => {
budgetController.getBudgetById(req, res);
});

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

/**
 * @swagger
 * /budget-master/budgets:
 *   post:
 *     summary: Create a new budget with default departments
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.post("/budgets", authenticate, (req, res) => {
budgetController.createBudget(req, res);
});

/**
 * @swagger
 * /budget-master/budgets/{id}/clone:
 *   post:
 *     summary: Clone budget to next month
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.post("/budgets/:id/clone", authenticate, (req, res) => {
budgetController.cloneBudget(req, res);
});

/**
 * @swagger
 * /budget-master/budgets/{budgetId}/departments:
 *   post:
 *     summary: Add new department to budget
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.post("/budgets/:budgetId/departments", authenticate, (req, res) => {
budgetController.addDepartment(req, res);
});

/**
 * @swagger
 * /budget-master/budgets/{budgetId}/departments/{deptId}/sub-departments:
 *   post:
 *     summary: Add new sub-department to department
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.post("/budgets/:budgetId/departments/:deptId/sub-departments", authenticate, (req, res) => {
budgetController.addSubDepartment(req, res);
});

// ==================== PUT ROUTES ====================

/**
 * @swagger
 * /budget-master/budgets/{id}:
 *   put:
 *     summary: Update budget basic info
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.put("/budgets/:id", authenticate, (req, res) => {
budgetController.updateBudget(req, res);
});

/**
 * @swagger
 * /budget-master/budgets/{budgetId}/departments/{deptId}:
 *   put:
 *     summary: Update department allocation amount
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.put("/budgets/:budgetId/departments/:deptId", authenticate, (req, res) => {
budgetController.updateDepartmentAllocation(req, res);
});

/**
 * @swagger
 * /budget-master/budgets/{budgetId}/departments/{deptId}/sub-departments/{subDeptId}:
 *   put:
 *     summary: Update sub-department allocation amount
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.put("/budgets/:budgetId/departments/:deptId/sub-departments/:subDeptId", authenticate, (req, res) => {
budgetController.updateSubDepartmentAllocation(req, res);
});

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

/**
 * @swagger
 * /budget-master/budgets/{id}:
 *   delete:
 *     summary: Delete budget (soft delete)
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.delete("/budgets/:id", authenticate, (req, res) => {
budgetController.deleteBudget(req, res);
});

/**
 * @swagger
 * /budget-master/budgets/{budgetId}/departments/{deptId}:
 *   delete:
 *     summary: Delete department (soft delete)
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.delete("/budgets/:budgetId/departments/:deptId", authenticate, (req, res) => {
budgetController.deleteDepartment(req, res);
});

/**
 * @swagger
 * /budget-master/budgets/{budgetId}/departments/{deptId}/sub-departments/{subDeptId}:
 *   delete:
 *     summary: Delete sub-department (soft delete)
 *     tags: [Budget Master]
 *     security:
 *       - bearerAuth: []
 */
router.delete("/budgets/:budgetId/departments/:deptId/sub-departments/:subDeptId", authenticate, (req, res) => {
budgetController.deleteSubDepartment(req, res);
});


module.exports = router;