const express = require('express');
const router = express.Router();
const permissionController = require('../controllers/permissionController');
const { jwtAuth } = require('../middleware/auth');

// 所有权限管理路由都需要认证
router.use(jwtAuth);

/**
 * @swagger
 * components:
 *   schemas:
 *     Permission:
 *       type: object
 *       required:
 *         - name
 *         - code
 *         - module
 *       properties:
 *         id:
 *           type: integer
 *           description: 权限ID
 *         name:
 *           type: string
 *           description: 权限名称
 *         code:
 *           type: string
 *           description: 权限代码
 *         description:
 *           type: string
 *           description: 权限描述
 *         module:
 *           type: string
 *           description: 所属模块
 *         type:
 *           type: string
 *           enum: [menu, operation]
 *           description: 权限类型
 *         parent_id:
 *           type: integer
 *           description: 父权限ID
 *         status:
 *           type: string
 *           enum: [active, inactive]
 *           description: 状态
 *         sort_order:
 *           type: integer
 *           description: 排序
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 */

/**
 * @swagger
 * /api/permissions:
 *   get:
 *     summary: 获取权限列表
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: module
 *         schema:
 *           type: string
 *         description: 模块筛选
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [menu, operation]
 *         description: 类型筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [active, inactive]
 *           default: active
 *         description: 状态筛选
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *         description: 关键词搜索
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 message:
 *                   type: string
 *                   example: 获取权限列表成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     permissions:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/Permission'
 *                     pagination:
 *                       type: object
 *                       properties:
 *                         total:
 *                           type: integer
 *                         page:
 *                           type: integer
 *                         limit:
 *                           type: integer
 *                         pages:
 *                           type: integer
 */
router.get('/', permissionController.getPermissions);

/**
 * @swagger
 * /api/permissions/tree:
 *   get:
 *     summary: 获取权限树形结构
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: module
 *         schema:
 *           type: string
 *         description: 模块筛选
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [menu, operation]
 *         description: 类型筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 message:
 *                   type: string
 *                   example: 获取权限树成功
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Permission'
 */
router.get('/tree', permissionController.getPermissionTree);

/**
 * @swagger
 * /api/permissions/modules:
 *   get:
 *     summary: 获取模块列表
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 message:
 *                   type: string
 *                   example: 获取模块列表成功
 *                 data:
 *                   type: array
 *                   items:
 *                     type: string
 */
router.get('/modules', permissionController.getModules);

/**
 * @swagger
 * /api/permissions/{id}:
 *   get:
 *     summary: 获取权限详情
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 权限ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 message:
 *                   type: string
 *                   example: 获取权限详情成功
 *                 data:
 *                   $ref: '#/components/schemas/Permission'
 *       404:
 *         description: 权限不存在
 */
router.get('/:id', permissionController.getPermissionById);

/**
 * @swagger
 * /api/permissions:
 *   post:
 *     summary: 创建权限
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - code
 *               - module
 *             properties:
 *               name:
 *                 type: string
 *                 description: 权限名称
 *               code:
 *                 type: string
 *                 description: 权限代码
 *               description:
 *                 type: string
 *                 description: 权限描述
 *               module:
 *                 type: string
 *                 description: 所属模块
 *               type:
 *                 type: string
 *                 enum: [menu, operation]
 *                 default: operation
 *                 description: 权限类型
 *               parent_id:
 *                 type: integer
 *                 description: 父权限ID
 *               sort_order:
 *                 type: integer
 *                 default: 0
 *                 description: 排序
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 message:
 *                   type: string
 *                   example: 创建权限成功
 *                 data:
 *                   $ref: '#/components/schemas/Permission'
 *       400:
 *         description: 请求参数错误
 */
router.post('/', permissionController.createPermission);

/**
 * @swagger
 * /api/permissions/{id}:
 *   put:
 *     summary: 更新权限
 *     tags: [权限管理]
 *     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: 权限名称
 *               code:
 *                 type: string
 *                 description: 权限代码
 *               description:
 *                 type: string
 *                 description: 权限描述
 *               module:
 *                 type: string
 *                 description: 所属模块
 *               type:
 *                 type: string
 *                 enum: [menu, operation]
 *                 description: 权限类型
 *               parent_id:
 *                 type: integer
 *                 description: 父权限ID
 *               sort_order:
 *                 type: integer
 *                 description: 排序
 *               status:
 *                 type: string
 *                 enum: [active, inactive]
 *                 description: 状态
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 message:
 *                   type: string
 *                   example: 更新权限成功
 *                 data:
 *                   $ref: '#/components/schemas/Permission'
 *       404:
 *         description: 权限不存在
 */
router.put('/:id', permissionController.updatePermission);

/**
 * @swagger
 * /api/permissions/{id}:
 *   delete:
 *     summary: 删除权限
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 权限ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 message:
 *                   type: string
 *                   example: 删除权限成功
 *       404:
 *         description: 权限不存在
 *       400:
 *         description: 权限正在使用中，无法删除
 */
router.delete('/:id', permissionController.deletePermission);

/**
 * @swagger
 * /api/permissions/roles/{roleId}:
 *   get:
 *     summary: 获取角色权限
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: roleId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 message:
 *                   type: string
 *                   example: 获取角色权限成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     role:
 *                       type: object
 *                       properties:
 *                         id:
 *                           type: integer
 *                         name:
 *                           type: string
 *                         description:
 *                           type: string
 *                     permissions:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/Permission'
 *       404:
 *         description: 角色不存在
 */
router.get('/roles/:roleId', permissionController.getRolePermissions);

/**
 * @swagger
 * /api/permissions/roles/{roleId}/assign:
 *   post:
 *     summary: 分配角色权限
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: roleId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - permissionIds
 *             properties:
 *               permissionIds:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 权限ID列表
 *           example:
 *             permissionIds: [1, 2, 3, 4]
 *     responses:
 *       200:
 *         description: 分配成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 message:
 *                   type: string
 *                   example: 分配角色权限成功
 *       404:
 *         description: 角色不存在
 *       400:
 *         description: 请求参数错误
 */
router.post('/roles/:roleId/assign', permissionController.assignRolePermissions);

module.exports = router;