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

// 应用认证中间件
router.use(jwtAuth);

/**
 * @swagger
 * components:
 *   schemas:
 *     RolePermissionAssignment:
 *       type: object
 *       properties:
 *         roleId:
 *           type: integer
 *           description: 角色ID
 *         permissionIds:
 *           type: array
 *           items:
 *             type: integer
 *           description: 权限ID列表
 *         operation:
 *           type: string
 *           enum: [replace, add, remove]
 *           default: replace
 *           description: 操作类型
 */

/**
 * @swagger
 * /api/role-permissions:
 *   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: object
 *                   properties:
 *                     roles:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: integer
 *                           name:
 *                             type: string
 *                           description:
 *                             type: string
 *                           permissions:
 *                             type: array
 *                           permissionCount:
 *                             type: integer
 *                     total:
 *                       type: integer
 */
router.get('/', rolePermissionController.getAllRolesWithPermissions);

/**
 * @swagger
 * /api/role-permissions/permissions:
 *   get:
 *     summary: 获取所有权限
 *     tags: [角色权限管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/permissions', rolePermissionController.getAllPermissions);

/**
 * @swagger
 * /api/role-permissions/roles:
 *   get:
 *     summary: 获取所有角色及其权限
 *     tags: [角色权限管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/roles', rolePermissionController.getAllRolesWithPermissions);

/**
 * @swagger
 * /api/role-permissions/roles/{roleId}/permissions:
 *   get:
 *     summary: 获取指定角色的权限详情
 *     tags: [角色权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: roleId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     responses:
 *       200:
 *         description: 获取成功
 *       404:
 *         description: 角色不存在
 */
router.get('/roles/:roleId/permissions', rolePermissionController.getRolePermissionDetail);

/**
 * @swagger
 * /api/role-permissions/stats:
 *   get:
 *     summary: 获取权限统计信息
 *     tags: [角色权限管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/stats', rolePermissionController.getPermissionStats);

/**
 * @swagger
 * /api/role-permissions/{roleId}:
 *   get:
 *     summary: 获取指定角色的权限详情
 *     tags: [角色权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: roleId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     responses:
 *       200:
 *         description: 获取成功
 *       404:
 *         description: 角色不存在
 */
router.get('/:roleId', rolePermissionController.getRolePermissionDetail);

/**
 * @swagger
 * /api/role-permissions/{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列表
 *               operation:
 *                 type: string
 *                 enum: [replace, add, remove]
 *                 default: replace
 *                 description: 操作类型
 *           examples:
 *             replace:
 *               summary: 替换权限
 *               value:
 *                 permissionIds: [1, 2, 3, 4]
 *                 operation: "replace"
 *             add:
 *               summary: 添加权限
 *               value:
 *                 permissionIds: [5, 6]
 *                 operation: "add"
 *             remove:
 *               summary: 移除权限
 *               value:
 *                 permissionIds: [1, 2]
 *                 operation: "remove"
 *     responses:
 *       200:
 *         description: 分配成功
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 角色不存在
 */
router.post('/:roleId/assign', rolePermissionController.batchAssignPermissions);

/**
 * @swagger
 * /api/role-permissions/copy:
 *   post:
 *     summary: 复制角色权限
 *     tags: [角色权限管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - sourceRoleId
 *               - targetRoleId
 *             properties:
 *               sourceRoleId:
 *                 type: integer
 *                 description: 源角色ID
 *               targetRoleId:
 *                 type: integer
 *                 description: 目标角色ID
 *           example:
 *             sourceRoleId: 1
 *             targetRoleId: 2
 *     responses:
 *       200:
 *         description: 复制成功
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 角色不存在
 */
router.post('/copy', rolePermissionController.copyRolePermissions);

/**
 * @swagger
 * /api/role-permissions/check/{userId}/{permissionCode}:
 *   get:
 *     summary: 检查用户权限
 *     tags: [角色权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *       - in: path
 *         name: permissionCode
 *         required: true
 *         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:
 *                     userId:
 *                       type: integer
 *                     username:
 *                       type: string
 *                     roleName:
 *                       type: string
 *                     permissionCode:
 *                       type: string
 *                     hasPermission:
 *                       type: boolean
 *                     checkTime:
 *                       type: string
 *                       format: date-time
 *       404:
 *         description: 用户不存在
 */
router.get('/check/:userId/:permissionCode', rolePermissionController.checkUserPermission);

/**
 * @swagger
 * /api/role-permissions/stats:
 *   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: object
 *                   properties:
 *                     overview:
 *                       type: object
 *                       properties:
 *                         totalRoles:
 *                           type: integer
 *                         totalPermissions:
 *                           type: integer
 *                         totalAssignments:
 *                           type: integer
 *                         averagePermissionsPerRole:
 *                           type: integer
 *                     moduleDistribution:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           module:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     roleDistribution:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           roleId:
 *                             type: integer
 *                           roleName:
 *                             type: string
 *                           permissionCount:
 *                             type: integer
 */

module.exports = router;