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

// 获取用户列表（需要管理员权限）
router.get('/', jwtAuth, checkPermission('user', 'read'), userController.getUsers);

// 个人中心相关路由（必须放在 /:id 路由之前）
// 获取个人资料（不需要特殊权限，用户可以查看自己的资料）
router.get('/profile', jwtAuth, userController.getProfile);

// 更新个人资料（不需要特殊权限，用户可以更新自己的资料）
router.put('/profile', jwtAuth, userController.updateProfile);

// 导出用户列表（必须在 /:id 之前）
router.get('/export', jwtAuth, checkPermission('user', 'read'), userController.exportToExcel);

// 获取单个用户信息
router.get('/:id', jwtAuth, checkPermission('user', 'read'), userController.getUser);

// 创建用户（需要管理员权限）
router.post('/', jwtAuth, checkPermission('user', 'create'), userController.createUser);

// 更新用户信息
router.put('/:id', jwtAuth, checkPermission('user', 'update'), userController.updateUser);

// 删除用户（需要管理员权限）
router.delete('/:id', jwtAuth, checkPermission('user', 'delete'), userController.deleteUser);

// 更新用户状态
router.patch('/:id/status', jwtAuth, checkPermission('user', 'update'), userController.updateUserStatus);

// 修改密码（不需要特殊权限，用户可以修改自己的密码）
router.put('/change-password', jwtAuth, userController.changePassword);

// 上传头像（不需要特殊权限，用户可以上传自己的头像）
router.post('/avatar', jwtAuth, userController.uploadAvatar);

/**
 * @swagger
 * /users/{id}/fixed-token:
 *   get:
 *     summary: 获取用户固定令牌信息
 *     description: 获取指定用户的固定令牌信息，包括是否已生成令牌、令牌预览等
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *       - fixedTokenAuth: []
 *     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
 *                 data:
 *                   $ref: '#/components/schemas/FixedTokenInfo'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       403:
 *         $ref: '#/components/responses/ForbiddenError'
 *       404:
 *         $ref: '#/components/responses/NotFoundError'
 *   post:
 *     summary: 生成用户固定令牌
 *     description: 为指定用户生成新的固定令牌，用于API访问验证
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *       - fixedTokenAuth: []
 *     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
 *                 data:
 *                   $ref: '#/components/schemas/FixedTokenGenerated'
 *       400:
 *         description: 用户已有固定令牌
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       403:
 *         $ref: '#/components/responses/ForbiddenError'
 *       404:
 *         $ref: '#/components/responses/NotFoundError'
 *   put:
 *     summary: 重新生成用户固定令牌
 *     description: 重新生成指定用户的固定令牌，原令牌将失效
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *       - fixedTokenAuth: []
 *     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
 *                 data:
 *                   $ref: '#/components/schemas/FixedTokenGenerated'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       403:
 *         $ref: '#/components/responses/ForbiddenError'
 *       404:
 *         $ref: '#/components/responses/NotFoundError'
 *   delete:
 *     summary: 删除用户固定令牌
 *     description: 删除指定用户的固定令牌，令牌将立即失效
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *       - fixedTokenAuth: []
 *     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: 固定令牌删除成功
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       403:
 *         $ref: '#/components/responses/ForbiddenError'
 *       404:
 *         $ref: '#/components/responses/NotFoundError'
 */
// 固定Token管理路由（需要管理员权限）
// 获取用户固定Token信息
router.get('/:id/fixed-token', jwtAuth, checkPermission('user', 'read'), userController.getFixedTokenInfo);

// 生成用户固定Token
router.post('/:id/fixed-token', jwtAuth, checkPermission('user', 'update'), userController.generateFixedToken);

// 重新生成用户固定Token
router.put('/:id/fixed-token', jwtAuth, checkPermission('user', 'update'), userController.regenerateFixedToken);

// 删除用户固定Token
router.delete('/:id/fixed-token', jwtAuth, checkPermission('user', 'delete'), userController.deleteFixedToken);

module.exports = router;