/**
 * 菜单管理路由
 * @file menus.js
 * @description 定义菜单管理相关的API路由
 */

const express = require('express');
const router = express.Router();
const menuController = require('../controllers/menuController');
const { verifyToken } = require('../middleware/auth');
const { requirePermission } = require('../middleware/permission');

// 公开API路由，不需要验证token
const publicRoutes = express.Router();
router.use('/public', publicRoutes);

// 公开获取角色菜单权限（用于权限管理）
publicRoutes.get('/role/:roleId', menuController.getRoleMenus);

// 所有其他路由都需要认证
router.use(verifyToken);

// 需要认证的菜单API
router.get('/', menuController.getAllMenus);
router.get('/:id', menuController.getMenuById);

/**
 * @swagger
 * components:
 *   schemas:
 *     MenuPermission:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 菜单ID
 *         name:
 *           type: string
 *           description: 菜单名称
 *         path:
 *           type: string
 *           description: 菜单路径
 *         component:
 *           type: string
 *           description: 组件路径
 *         icon:
 *           type: string
 *           description: 图标
 *         parent_id:
 *           type: integer
 *           description: 父菜单ID
 *         sort_order:
 *           type: integer
 *           description: 排序
 *         status:
 *           type: boolean
 *           description: 状态
 *         description:
 *           type: string
 *           description: 描述
 *         permission_code:
 *           type: string
 *           description: 权限代码
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 */

/**
 * @swagger
 * /api/menus:
 *   get:
 *     summary: 获取菜单列表
 *     tags: [Menus]
 *     parameters:
 *       - in: query
 *         name: roleId
 *         schema:
 *           type: integer
 *         description: 角色ID（可选）
 *     responses:
 *       200:
 *         description: 成功获取菜单列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/MenuPermission'
 *                 message:
 *                   type: string
 */
router.get('/', requirePermission('menu:view'), menuController.getAllMenus);

/**
 * @swagger
 * /api/menus/{id}:
 *   get:
 *     summary: 获取菜单详情
 *     tags: [Menus]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 菜单ID
 *     responses:
 *       200:
 *         description: 成功获取菜单详情
 *       404:
 *         description: 菜单不存在
 */
router.get('/:id', requirePermission('menu:view'), menuController.getMenuById);

/**
 * @swagger
 * /api/menus:
 *   post:
 *     summary: 创建菜单
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - path
 *             properties:
 *               name:
 *                 type: string
 *                 description: 菜单名称
 *               path:
 *                 type: string
 *                 description: 菜单路径
 *               component:
 *                 type: string
 *                 description: 组件路径
 *               icon:
 *                 type: string
 *                 description: 图标
 *               parent_id:
 *                 type: integer
 *                 description: 父菜单ID
 *               sort_order:
 *                 type: integer
 *                 description: 排序
 *               status:
 *                 type: boolean
 *                 description: 状态
 *               description:
 *                 type: string
 *                 description: 描述
 *               permission_code:
 *                 type: string
 *                 description: 权限代码
 *     responses:
 *       201:
 *         description: 菜单创建成功
 *       400:
 *         description: 请求参数错误
 */
router.post('/', requirePermission('menu:create'), menuController.createMenu);

/**
 * @swagger
 * /api/menus/{id}:
 *   put:
 *     summary: 更新菜单
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 菜单ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 菜单名称
 *               path:
 *                 type: string
 *                 description: 菜单路径
 *               component:
 *                 type: string
 *                 description: 组件路径
 *               icon:
 *                 type: string
 *                 description: 图标
 *               parent_id:
 *                 type: integer
 *                 description: 父菜单ID
 *               sort_order:
 *                 type: integer
 *                 description: 排序
 *               status:
 *                 type: boolean
 *                 description: 状态
 *               description:
 *                 type: string
 *                 description: 描述
 *               permission_code:
 *                 type: string
 *                 description: 权限代码
 *     responses:
 *       200:
 *         description: 菜单更新成功
 *       404:
 *         description: 菜单不存在
 */
router.put('/:id', requirePermission('menu:update'), menuController.updateMenu);

/**
 * @swagger
 * /api/menus/{id}:
 *   delete:
 *     summary: 删除菜单
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 菜单ID
 *     responses:
 *       200:
 *         description: 菜单删除成功
 *       404:
 *         description: 菜单不存在
 */
router.delete('/:id', requirePermission('menu:delete'), menuController.deleteMenu);

/**
 * @swagger
 * /api/menus/role/{roleId}:
 *   get:
 *     summary: 获取角色的菜单权限
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: roleId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     responses:
 *       200:
 *         description: 成功获取角色菜单权限
 *       404:
 *         description: 角色不存在
 */
router.get('/role/:roleId', requirePermission('menu:view'), menuController.getRoleMenus);

/**
 * @swagger
 * /api/menus/role/{roleId}:
 *   post:
 *     summary: 设置角色的菜单权限
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: roleId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               menuIds:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 菜单ID数组
 *     responses:
 *       200:
 *         description: 设置角色菜单权限成功
 *       404:
 *         description: 角色不存在
 */
router.post('/role/:roleId', requirePermission('menu:assign'), menuController.setRoleMenus);

module.exports = router;
