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

/**
 * @swagger
 * tags:
 *   name: OperationLogs
 *   description: 系统操作日志管理
 */

/**
 * @swagger
 * /api/operation-logs:
 *   get:
 *     summary: 获取操作日志列表
 *     tags: [OperationLogs]
 *     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: user_id
 *         schema:
 *           type: integer
 *         description: 用户ID
 *       - in: query
 *         name: operation_type
 *         schema:
 *           type: string
 *           enum: [login, logout, create, update, delete, view, export, import, approve, reject, system_config, user_manage, role_manage, other]
 *         description: 操作类型
 *       - in: query
 *         name: operation_module
 *         schema:
 *           type: string
 *         description: 操作模块
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [success, failed, error]
 *         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: keyword
 *         schema:
 *           type: string
 *         description: 关键词搜索
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: success
 *                 data:
 *                   type: object
 *                   properties:
 *                     logs:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/OperationLog'
 *                     total:
 *                       type: integer
 *                     page:
 *                       type: integer
 *                     limit:
 *                       type: integer
 *                     totalPages:
 *                       type: integer
 */
router.get('/', jwtAuth, checkPermission('system', 'read'), operationLogController.getOperationLogs);

/**
 * @swagger
 * /api/operation-logs/stats:
 *   get:
 *     summary: 获取操作日志统计信息
 *     tags: [OperationLogs]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: user_id
 *         schema:
 *           type: integer
 *         description: 用户ID
 *       - in: query
 *         name: start_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期
 *       - in: query
 *         name: end_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: success
 *                 data:
 *                   type: object
 *                   properties:
 *                     typeStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           operation_type:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     moduleStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           operation_module:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     statusStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           status:
 *                             type: string
 *                           count:
 *                             type: integer
 */
router.get('/stats', jwtAuth, checkPermission('system', 'read'), operationLogController.getOperationStats);

// 导出操作日志到Excel
router.get('/export', jwtAuth, checkPermission('system', 'read'), operationLogController.exportToExcel);

/**
 * @swagger
 * /api/operation-logs/{id}:
 *   get:
 *     summary: 获取操作日志详情
 *     tags: [OperationLogs]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 操作日志ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: success
 *                 data:
 *                   $ref: '#/components/schemas/OperationLog'
 */
router.get('/:id', jwtAuth, checkPermission('system', 'read'), operationLogController.getOperationLogById);

/**
 * @swagger
 * /api/operation-logs/export:
 *   post:
 *     summary: 导出操作日志
 *     tags: [OperationLogs]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               user_id:
 *                 type: integer
 *               operation_type:
 *                 type: string
 *               operation_module:
 *                 type: string
 *               status:
 *                 type: string
 *               start_date:
 *                 type: string
 *                 format: date
 *               end_date:
 *                 type: string
 *                 format: date
 *               keyword:
 *                 type: string
 *     responses:
 *       200:
 *         description: 导出成功
 *         content:
 *           application/vnd.openxmlformats-officedocument.spreadsheetml.sheet:
 *             schema:
 *               type: string
 *               format: binary
 */
router.post('/export', jwtAuth, checkPermission('system', 'export'), operationLogController.exportOperationLogs);

/**
 * @swagger
 * components:
 *   schemas:
 *     OperationLog:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 日志ID
 *         user_id:
 *           type: integer
 *           description: 操作用户ID
 *         operation_type:
 *           type: string
 *           enum: [login, logout, create, update, delete, view, export, import, approve, reject, system_config, user_manage, role_manage, other]
 *           description: 操作类型
 *         operation_module:
 *           type: string
 *           description: 操作模块
 *         operation_content:
 *           type: string
 *           description: 操作内容描述
 *         operation_target:
 *           type: string
 *           description: 操作目标
 *         request_method:
 *           type: string
 *           enum: [GET, POST, PUT, DELETE, PATCH]
 *           description: HTTP请求方法
 *         request_url:
 *           type: string
 *           description: 请求URL
 *         request_params:
 *           type: object
 *           description: 请求参数
 *         response_status:
 *           type: integer
 *           description: 响应状态码
 *         ip_address:
 *           type: string
 *           description: IP地址
 *         user_agent:
 *           type: string
 *           description: 用户代理信息
 *         execution_time:
 *           type: integer
 *           description: 执行时间（毫秒）
 *         status:
 *           type: string
 *           enum: [success, failed, error]
 *           description: 操作状态
 *         error_message:
 *           type: string
 *           description: 错误信息
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 *         user:
 *           type: object
 *           properties:
 *             id:
 *               type: integer
 *             username:
 *               type: string
 *             real_name:
 *               type: string
 */

module.exports = router;