/**
 * 备份管理路由
 * @file backup.js
 * @description 处理数据备份和恢复请求
 */
const express = require('express');
const { body } = require('express-validator');
const { verifyToken, checkRole } = require('../middleware/auth');
const backupController = require('../controllers/backupController');

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: Backup
 *   description: 数据备份管理相关接口
 */

/**
 * @swagger
 * /api/backup/create:
 *   post:
 *     summary: 创建数据备份
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: false
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               type:
 *                 type: string
 *                 enum: [full, daily, weekly, monthly]
 *                 description: 备份类型
 *                 default: full
 *               description:
 *                 type: string
 *                 description: 备份描述
 *     responses:
 *       200:
 *         description: 备份创建成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器错误
 */
router.post('/create', 
  verifyToken, 
  checkRole(['admin']),
  [
    body('type').optional().isIn(['full', 'daily', 'weekly', 'monthly']).withMessage('备份类型无效'),
    body('description').optional().isLength({ max: 255 }).withMessage('描述长度不能超过255字符')
  ],
  backupController.createBackup
);

/**
 * @swagger
 * /api/backup/list:
 *   get:
 *     summary: 获取备份列表
 *     tags: [Backup]
 *     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: type
 *         schema:
 *           type: string
 *           enum: [full, daily, weekly, monthly]
 *         description: 备份类型过滤
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/list', verifyToken, checkRole(['admin']), backupController.getBackupList);

/**
 * @swagger
 * /api/backup/stats:
 *   get:
 *     summary: 获取备份统计信息
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/stats', verifyToken, checkRole(['admin']), backupController.getBackupStats);

/**
 * @swagger
 * /api/backup/health:
 *   get:
 *     summary: 获取备份系统健康状态
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/health', verifyToken, checkRole(['admin']), backupController.getBackupHealth);

/**
 * @swagger
 * /api/backup/{id}:
 *   delete:
 *     summary: 删除备份
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 备份ID
 *     responses:
 *       200:
 *         description: 删除成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 备份不存在
 */
router.delete('/:id', verifyToken, checkRole(['admin']), backupController.deleteBackup);

/**
 * @swagger
 * /api/backup/{id}/restore:
 *   post:
 *     summary: 恢复数据备份
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 备份ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - confirm
 *             properties:
 *               confirm:
 *                 type: boolean
 *                 description: 确认恢复操作
 *     responses:
 *       200:
 *         description: 恢复成功
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 恢复失败
 */
router.post('/:id/restore', 
  verifyToken, 
  checkRole(['admin']),
  [
    body('confirm').isBoolean().withMessage('confirm必须是布尔值')
  ],
  backupController.restoreBackup
);

/**
 * @swagger
 * /api/backup/{id}/download:
 *   get:
 *     summary: 下载备份文件
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 备份ID
 *     responses:
 *       200:
 *         description: 文件下载
 *         content:
 *           application/zip:
 *             schema:
 *               type: string
 *               format: binary
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 备份文件不存在
 */
router.get('/:id/download', verifyToken, checkRole(['admin']), backupController.downloadBackup);

/**
 * @swagger
 * /api/backup/cleanup:
 *   post:
 *     summary: 清理过期备份
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 清理成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.post('/cleanup', verifyToken, checkRole(['admin']), backupController.cleanupBackups);

/**
 * @swagger
 * /api/backup/schedule/start:
 *   post:
 *     summary: 启动自动备份调度
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 启动成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.post('/schedule/start', verifyToken, checkRole(['admin']), backupController.startScheduler);

/**
 * @swagger
 * /api/backup/schedule/stop:
 *   post:
 *     summary: 停止自动备份调度
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 停止成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.post('/schedule/stop', verifyToken, checkRole(['admin']), backupController.stopScheduler);

/**
 * @swagger
 * /api/backup/schedule/status:
 *   get:
 *     summary: 获取自动备份调度状态
 *     tags: [Backup]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/schedule/status', verifyToken, checkRole(['admin']), backupController.getSchedulerStatus);

module.exports = router;
