const express = require('express');
const { verifyToken } = require('../middleware/auth');
const searchLogger = require('../middleware/search-logger');
const router = express.Router();
const userController = require('../controllers/userController');

/**
 * @swagger
 * tags:
 *   name: Users
 *   description: 用户管理
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     User:
 *       type: object
 *       required:
 *         - id
 *         - username
 *         - email
 *         - password
 *       properties:
 *         id:
 *           type: integer
 *           description: 用户ID
 *         username:
 *           type: string
 *           description: 用户名
 *         email:
 *           type: string
 *           description: 邮箱地址
 *         password:
 *           type: string
 *           description: 密码（加密存储）
 *         phone:
 *           type: string
 *           description: 手机号码
 *         avatar:
 *           type: string
 *           description: 头像URL
 *         status:
 *           type: string
 *           enum: [active, inactive, suspended]
 *           description: 用户状态
 *         createdAt:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updatedAt:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 *       example:
 *         id: 1
 *         username: "john_doe"
 *         email: "john@example.com"
 *         phone: "13800138000"
 *         avatar: "/uploads/avatars/default.png"
 *         status: "active"
 *         createdAt: "2024-01-01T00:00:00.000Z"
 *         updatedAt: "2024-01-01T00:00:00.000Z"
 */

/**
 * @swagger
 * /api/users:
 *   get:
 *     summary: 获取所有用户 (需要认证)
 *     tags: [Users]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 用户列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/User'
 *       401:
 *         description: 未认证
 *       500:
 *         description: 服务器错误
 */
// 获取所有用户 (需要认证)
router.get('/', verifyToken, userController.getAllUsers);

/**
 * @swagger
 * /api/users/{id}:
 *   get:
 *     summary: 根据ID获取用户 (需要认证)
 *     tags: [Users]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         schema:
 *           type: integer
 *         required: true
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 用户信息
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/User'
 *       401:
 *         description: 未认证
 *       404:
 *         description: 用户未找到
 *       500:
 *         description: 服务器错误
 */

// 根据用户名搜索用户 (需要认证，添加搜索监听中间件)
router.get('/search', verifyToken, searchLogger, userController.searchUserByUsername);

// 根据ID获取用户 (需要认证)
router.get('/:id', verifyToken, userController.getUserById);

// 创建用户 (需要认证)
router.post('/', verifyToken, userController.createUser);

// 更新用户 (需要认证)
router.put('/:id', verifyToken, userController.updateUser);

// 删除用户 (需要认证)
router.delete('/:id', verifyToken, userController.deleteUser);

module.exports = router;