/**
 * 系统管理路由
 * @file system.js
 * @description 处理系统配置和菜单权限管理请求
 */
const express = require('express');
const { body } = require('express-validator');
const { verifyToken, checkRole } = require('../middleware/auth');
const systemController = require('../controllers/systemController');

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: System
 *   description: 系统管理相关接口
 */

/**
 * @swagger
 * /api/system/configs:
 *   get:
 *     summary: 获取系统配置列表
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: category
 *         schema:
 *           type: string
 *         description: 配置分类
 *       - in: query
 *         name: is_public
 *         schema:
 *           type: boolean
 *         description: 是否公开配置
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/configs', verifyToken, checkRole(['admin']), systemController.getSystemConfigs);

/**
 * @swagger
 * /api/system/configs/public:
 *   get:
 *     summary: 获取公开系统配置
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/configs/public', verifyToken, systemController.getPublicConfigs);

/**
 * @swagger
 * /api/system/configs/categories:
 *   get:
 *     summary: 获取配置分类列表
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/configs/categories', verifyToken, checkRole(['admin']), systemController.getConfigCategories);

/**
 * @swagger
 * /api/system/configs:
 *   post:
 *     summary: 创建系统配置
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - config_key
 *               - config_value
 *             properties:
 *               config_key:
 *                 type: string
 *                 description: 配置键名
 *               config_value:
 *                 description: 配置值
 *               category:
 *                 type: string
 *                 description: 配置分类
 *               description:
 *                 type: string
 *                 description: 配置描述
 *               is_public:
 *                 type: boolean
 *                 description: 是否公开
 *               is_editable:
 *                 type: boolean
 *                 description: 是否可编辑
 *               sort_order:
 *                 type: integer
 *                 description: 排序顺序
 *     responses:
 *       201:
 *         description: 创建成功
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.post('/configs', 
  verifyToken, 
  checkRole(['admin']),
  [
    body('config_key').notEmpty().withMessage('配置键名不能为空'),
    body('config_value').exists().withMessage('配置值不能为空')
  ],
  systemController.createSystemConfig
);

/**
 * @swagger
 * /api/system/configs/{id}:
 *   put:
 *     summary: 更新系统配置
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 配置ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               config_value:
 *                 description: 配置值
 *               description:
 *                 type: string
 *                 description: 配置描述
 *     responses:
 *       200:
 *         description: 更新成功
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 配置不存在
 */
router.put('/configs/:id', 
  verifyToken, 
  checkRole(['admin']),
  [
    body('config_value').exists().withMessage('配置值不能为空')
  ],
  systemController.updateSystemConfig
);

/**
 * @swagger
 * /api/system/configs/{id}:
 *   delete:
 *     summary: 删除系统配置
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 配置ID
 *     responses:
 *       200:
 *         description: 删除成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 配置不存在
 */
router.delete('/configs/:id', verifyToken, checkRole(['admin']), systemController.deleteSystemConfig);

/**
 * @swagger
 * /api/system/configs/batch:
 *   put:
 *     summary: 批量更新系统配置
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - configs
 *             properties:
 *               configs:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     config_key:
 *                       type: string
 *                     config_value:
 *                       description: 配置值
 *     responses:
 *       200:
 *         description: 更新成功
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.put('/configs/batch', 
  verifyToken, 
  checkRole(['admin']),
  [
    body('configs').isArray().withMessage('configs必须是数组')
  ],
  systemController.batchUpdateConfigs
);

/**
 * @swagger
 * /api/system/configs/{id}/reset:
 *   post:
 *     summary: 重置系统配置到默认值
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 配置ID
 *     responses:
 *       200:
 *         description: 重置成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 配置不存在
 */
router.post('/configs/:id/reset', verifyToken, checkRole(['admin']), systemController.resetSystemConfig);

/**
 * @swagger
 * /api/system/menus:
 *   get:
 *     summary: 获取菜单权限列表
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/menus', verifyToken, checkRole(['admin']), systemController.getMenuPermissions);

/**
 * @swagger
 * /api/system/menus/user:
 *   get:
 *     summary: 获取用户可访问菜单
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       404:
 *         description: 用户不存在
 */
router.get('/menus/user', verifyToken, systemController.getUserMenus);

/**
 * @swagger
 * /api/system/menus/{id}:
 *   put:
 *     summary: 更新菜单权限
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 菜单ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               menu_name:
 *                 type: string
 *                 description: 菜单名称
 *               menu_path:
 *                 type: string
 *                 description: 菜单路径
 *               required_roles:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 所需角色
 *               required_permissions:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 所需权限
 *               icon:
 *                 type: string
 *                 description: 菜单图标
 *               sort_order:
 *                 type: integer
 *                 description: 排序顺序
 *               is_visible:
 *                 type: boolean
 *                 description: 是否可见
 *               is_enabled:
 *                 type: boolean
 *                 description: 是否启用
 *               description:
 *                 type: string
 *                 description: 菜单描述
 *     responses:
 *       200:
 *         description: 更新成功
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 菜单不存在
 */
router.put('/menus/:id', 
  verifyToken, 
  checkRole(['admin']),
  [
    body('menu_name').optional().notEmpty().withMessage('菜单名称不能为空'),
    body('required_roles').optional().isArray().withMessage('所需角色必须是数组'),
    body('required_permissions').optional().isArray().withMessage('所需权限必须是数组')
  ],
  systemController.updateMenuPermission
);

/**
 * @swagger
 * /api/system/stats:
 *   get:
 *     summary: 获取系统统计信息
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/stats', verifyToken, checkRole(['admin']), systemController.getSystemStats);

/**
 * @swagger
 * /api/system/init:
 *   post:
 *     summary: 初始化系统配置
 *     tags: [System]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 初始化成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.post('/init', verifyToken, checkRole(['admin']), systemController.initializeSystem);

module.exports = router;
