/**
 * 管理员文件管理路由
 * 定义文件上传、管理、统计等API接口
 */

const express = require('express');
const router = express.Router();
const {
  getFileList,
  getFileDetail,
  deleteFileById,
  batchDeleteFiles,
  getFileStatistics,
  cleanupUnusedFiles,
  uploadFile
} = require('../../controllers/admin/fileManagement');
const { uploadMiddlewares, imageProcessors } = require('../../middleware/upload');

/**
 * @swagger
 * components:
 *   schemas:
 *     FileInfo:
 *       type: object
 *       properties:
 *         id:
 *           type: string
 *           description: 文件ID（Base64编码的文件路径）
 *         filename:
 *           type: string
 *           description: 文件名
 *         originalName:
 *           type: string
 *           description: 原始文件名
 *         type:
 *           type: string
 *           enum: [avatar, animal, travel, document]
 *           description: 文件类型
 *         size:
 *           type: integer
 *           description: 文件大小（字节）
 *         mimetype:
 *           type: string
 *           description: MIME类型
 *         isImage:
 *           type: boolean
 *           description: 是否为图片
 *         url:
 *           type: string
 *           description: 文件访问URL
 *         thumbnailUrl:
 *           type: string
 *           description: 缩略图URL（仅图片）
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         modified_at:
 *           type: string
 *           format: date-time
 *           description: 修改时间
 *     
 *     FileStatistics:
 *       type: object
 *       properties:
 *         totalFiles:
 *           type: integer
 *           description: 文件总数
 *         totalSize:
 *           type: integer
 *           description: 总大小（字节）
 *         typeStats:
 *           type: array
 *           items:
 *             type: object
 *             properties:
 *               type:
 *                 type: string
 *                 description: 文件类型
 *               count:
 *                 type: integer
 *                 description: 文件数量
 *               size:
 *                 type: integer
 *                 description: 总大小
 *               avgSize:
 *                 type: integer
 *                 description: 平均大小
 *         sizeDistribution:
 *           type: object
 *           properties:
 *             small:
 *               type: integer
 *               description: 小文件数量（<1MB）
 *             medium:
 *               type: integer
 *               description: 中等文件数量（1-5MB）
 *             large:
 *               type: integer
 *               description: 大文件数量（>5MB）
 *         formatStats:
 *           type: array
 *           items:
 *             type: object
 *             properties:
 *               format:
 *                 type: string
 *                 description: 文件格式
 *               count:
 *                 type: integer
 *                 description: 数量
 *               size:
 *                 type: integer
 *                 description: 总大小
 *               percentage:
 *                 type: string
 *                 description: 占比百分比
 */

/**
 * @swagger
 * /admin/files:
 *   get:
 *     summary: 获取文件列表
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [all, avatar, animal, travel, document]
 *           default: all
 *         description: 文件类型
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *       - in: query
 *         name: start_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期
 *       - in: query
 *         name: end_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期
 *       - in: query
 *         name: sort_by
 *         schema:
 *           type: string
 *           enum: [created_at, modified_at, size, filename]
 *           default: created_at
 *         description: 排序字段
 *       - in: query
 *         name: sort_order
 *         schema:
 *           type: string
 *           enum: [asc, desc]
 *           default: desc
 *         description: 排序方向
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     files:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/FileInfo'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 */
router.get('/', getFileList);

/**
 * @swagger
 * /admin/files/{file_id}:
 *   get:
 *     summary: 获取文件详情
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: file_id
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     file:
 *                       $ref: '#/components/schemas/FileInfo'
 *       404:
 *         description: 文件不存在
 */
router.get('/:file_id', getFileDetail);

/**
 * @swagger
 * /admin/files/{file_id}:
 *   delete:
 *     summary: 删除文件
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: file_id
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *       404:
 *         description: 文件不存在
 */
router.delete('/:file_id', deleteFileById);

/**
 * @swagger
 * /admin/files/batch/delete:
 *   post:
 *     summary: 批量删除文件
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - file_ids
 *             properties:
 *               file_ids:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 文件ID列表（最多50个）
 *     responses:
 *       200:
 *         description: 批量删除完成
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     success:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           file_id:
 *                             type: string
 *                           filename:
 *                             type: string
 *                           message:
 *                             type: string
 *                     failed:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           file_id:
 *                             type: string
 *                           filename:
 *                             type: string
 *                           message:
 *                             type: string
 */
router.post('/batch/delete', batchDeleteFiles);

/**
 * @swagger
 * /admin/files/statistics:
 *   get:
 *     summary: 获取文件统计信息
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/FileStatistics'
 */
router.get('/statistics', getFileStatistics);

/**
 * @swagger
 * /admin/files/cleanup:
 *   post:
 *     summary: 清理无用文件
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: dry_run
 *         schema:
 *           type: boolean
 *           default: true
 *         description: 是否为试运行（不实际删除文件）
 *     responses:
 *       200:
 *         description: 清理完成
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     scanned:
 *                       type: integer
 *                       description: 扫描的文件数量
 *                     unused:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           filename:
 *                             type: string
 *                           type:
 *                             type: string
 *                           size:
 *                             type: integer
 *                           lastModified:
 *                             type: string
 *                             format: date-time
 *                     deleted:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           filename:
 *                             type: string
 *                           type:
 *                             type: string
 *                           size:
 *                             type: integer
 *                     errors:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           filename:
 *                             type: string
 *                           type:
 *                             type: string
 *                           error:
 *                             type: string
 */
router.post('/cleanup', cleanupUnusedFiles);

/**
 * @swagger
 * /admin/files/upload/avatar:
 *   post:
 *     summary: 上传头像
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               avatar:
 *                 type: string
 *                 format: binary
 *                 description: 头像文件（支持jpg、png格式，最大2MB）
 *     responses:
 *       200:
 *         description: 上传成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     files:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/FileInfo'
 */
router.post('/upload/avatar', uploadMiddlewares.avatar, imageProcessors.avatar, uploadFile);

/**
 * @swagger
 * /admin/files/upload/animal:
 *   post:
 *     summary: 上传动物图片
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               images:
 *                 type: array
 *                 items:
 *                   type: string
 *                   format: binary
 *                 description: 动物图片文件（支持jpg、png、gif、webp格式，最大5MB，最多5张）
 *     responses:
 *       200:
 *         description: 上传成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     files:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/FileInfo'
 */
router.post('/upload/animal', uploadMiddlewares.animalImages, imageProcessors.animal, uploadFile);

/**
 * @swagger
 * /admin/files/upload/travel:
 *   post:
 *     summary: 上传旅行图片
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               images:
 *                 type: array
 *                 items:
 *                   type: string
 *                   format: binary
 *                 description: 旅行图片文件（支持jpg、png、gif、webp格式，最大5MB，最多10张）
 *     responses:
 *       200:
 *         description: 上传成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     files:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/FileInfo'
 */
router.post('/upload/travel', uploadMiddlewares.travelImages, imageProcessors.travel, uploadFile);

/**
 * @swagger
 * /admin/files/upload/document:
 *   post:
 *     summary: 上传文档
 *     tags: [管理员-文件管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               documents:
 *                 type: array
 *                 items:
 *                   type: string
 *                   format: binary
 *                 description: 文档文件（支持pdf、doc、docx、xls、xlsx、txt格式，最大10MB，最多3个）
 *     responses:
 *       200:
 *         description: 上传成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     files:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/FileInfo'
 */
router.post('/upload/document', uploadMiddlewares.documents, uploadFile);

module.exports = router;