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

/**
 * @swagger
 * tags:
 *   name: RegulatoryTaskCompletion
 *   description: 监管任务结项管理API
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     RegulatoryTaskCompletion:
 *       type: object
 *       required:
 *         - application_no
 *         - policy_no
 *         - product_name
 *         - customer_name
 *       properties:
 *         id:
 *           type: integer
 *           description: 记录ID
 *         application_no:
 *           type: string
 *           description: 申请编号
 *         policy_no:
 *           type: string
 *           description: 保单号
 *         product_name:
 *           type: string
 *           description: 产品名称
 *         customer_name:
 *           type: string
 *           description: 客户名称
 *         customer_phone:
 *           type: string
 *           description: 客户电话
 *         insurance_amount:
 *           type: number
 *           format: decimal
 *           description: 保险金额
 *         premium_amount:
 *           type: number
 *           format: decimal
 *           description: 保费金额
 *         start_date:
 *           type: string
 *           format: date
 *           description: 保险起期
 *         end_date:
 *           type: string
 *           format: date
 *           description: 保险止期
 *         completion_date:
 *           type: string
 *           format: date
 *           description: 结项日期
 *         status:
 *           type: string
 *           enum: [pending, approved, rejected, completed]
 *           description: 状态
 *         reviewer_name:
 *           type: string
 *           description: 审核人员
 *         review_comments:
 *           type: string
 *           description: 审核意见
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 */

/**
 * @swagger
 * /api/regulatory-task-completion:
 *   get:
 *     summary: 获取监管任务结项列表
 *     tags: [RegulatoryTaskCompletion]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: application_no
 *         schema:
 *           type: string
 *         description: 申请编号（模糊查询）
 *       - in: query
 *         name: policy_no
 *         schema:
 *           type: string
 *         description: 保单号（模糊查询）
 *       - in: query
 *         name: product_name
 *         schema:
 *           type: string
 *         description: 产品名称（模糊查询）
 *       - in: query
 *         name: customer_name
 *         schema:
 *           type: string
 *         description: 客户名称（模糊查询）
 *       - in: query
 *         name: completion_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 结项日期
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, approved, rejected, completed]
 *         description: 状态
 *       - in: query
 *         name: reviewer_name
 *         schema:
 *           type: string
 *         description: 审核人员（模糊查询）
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 成功获取监管任务结项列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: success
 *                 data:
 *                   type: object
 *                   properties:
 *                     list:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/RegulatoryTaskCompletion'
 *                     pagination:
 *                       type: object
 *                       properties:
 *                         current:
 *                           type: integer
 *                         pageSize:
 *                           type: integer
 *                         total:
 *                           type: integer
 *                         totalPages:
 *                           type: integer
 */
router.get('/', jwtAuth, checkPermission('regulatory_task', 'read'), 
  regulatoryTaskCompletionController.getTaskCompletions
);

/**
 * @swagger
 * /api/regulatory-task-completion/{id}:
 *   get:
 *     summary: 获取监管任务结项详情
 *     tags: [RegulatoryTaskCompletion]
 *     security:
 *       - bearerAuth: []
 *     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
 *                 message:
 *                   type: string
 *                   example: success
 *                 data:
 *                   allOf:
 *                     - $ref: '#/components/schemas/RegulatoryTaskCompletion'
 *                     - type: object
 *                       properties:
 *                         attachments:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               id:
 *                                 type: integer
 *                               file_name:
 *                                 type: string
 *                               file_path:
 *                                 type: string
 *                               file_size:
 *                                 type: integer
 *                               file_type:
 *                                 type: string
 *                               upload_time:
 *                                 type: string
 *                                 format: date-time
 *                         operation_logs:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               id:
 *                                 type: integer
 *                               operation_type:
 *                                 type: string
 *                               operation_description:
 *                                 type: string
 *                               operator_name:
 *                                 type: string
 *                               operation_time:
 *                                 type: string
 *                                 format: date-time
 *                               ip_address:
 *                                 type: string
 *       404:
 *         description: 记录不存在
 */
router.get('/:id', jwtAuth, checkPermission('regulatory_task', 'read'),
  regulatoryTaskCompletionController.getTaskCompletionById
);

/**
 * @swagger
 * /api/regulatory-task-completion:
 *   post:
 *     summary: 创建监管任务结项记录
 *     tags: [RegulatoryTaskCompletion]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - application_no
 *               - policy_no
 *               - product_name
 *               - customer_name
 *             properties:
 *               application_no:
 *                 type: string
 *                 description: 申请编号
 *               policy_no:
 *                 type: string
 *                 description: 保单号
 *               product_name:
 *                 type: string
 *                 description: 产品名称
 *               customer_name:
 *                 type: string
 *                 description: 客户名称
 *               customer_phone:
 *                 type: string
 *                 description: 客户电话
 *               insurance_amount:
 *                 type: number
 *                 format: decimal
 *                 description: 保险金额
 *               premium_amount:
 *                 type: number
 *                 format: decimal
 *                 description: 保费金额
 *               start_date:
 *                 type: string
 *                 format: date
 *                 description: 保险起期
 *               end_date:
 *                 type: string
 *                 format: date
 *                 description: 保险止期
 *               completion_date:
 *                 type: string
 *                 format: date
 *                 description: 结项日期
 *               status:
 *                 type: string
 *                 enum: [pending, approved, rejected, completed]
 *                 default: pending
 *                 description: 状态
 *               review_comments:
 *                 type: string
 *                 description: 审核意见
 *               attachments:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     file_name:
 *                       type: string
 *                     file_path:
 *                       type: string
 *                     file_size:
 *                       type: integer
 *                     file_type:
 *                       type: string
 *     responses:
 *       201:
 *         description: 监管任务结项记录创建成功
 *       400:
 *         description: 参数错误或记录已存在
 */
router.post('/', jwtAuth, checkPermission('regulatory_task', 'create'),
  regulatoryTaskCompletionController.createTaskCompletion
);

/**
 * @swagger
 * /api/regulatory-task-completion/{id}:
 *   put:
 *     summary: 更新监管任务结项记录
 *     tags: [RegulatoryTaskCompletion]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 记录ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               application_no:
 *                 type: string
 *               policy_no:
 *                 type: string
 *               product_name:
 *                 type: string
 *               customer_name:
 *                 type: string
 *               customer_phone:
 *                 type: string
 *               insurance_amount:
 *                 type: number
 *                 format: decimal
 *               premium_amount:
 *                 type: number
 *                 format: decimal
 *               start_date:
 *                 type: string
 *                 format: date
 *               end_date:
 *                 type: string
 *                 format: date
 *               completion_date:
 *                 type: string
 *                 format: date
 *               status:
 *                 type: string
 *                 enum: [pending, approved, rejected, completed]
 *               review_comments:
 *                 type: string
 *     responses:
 *       200:
 *         description: 监管任务结项记录更新成功
 *       404:
 *         description: 记录不存在
 */
router.put('/:id', jwtAuth, checkPermission('regulatory_task', 'update'),
  regulatoryTaskCompletionController.updateTaskCompletion
);

/**
 * @swagger
 * /api/regulatory-task-completion/{id}:
 *   delete:
 *     summary: 删除监管任务结项记录
 *     tags: [RegulatoryTaskCompletion]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 记录ID
 *     responses:
 *       200:
 *         description: 监管任务结项记录删除成功
 *       404:
 *         description: 记录不存在
 */
router.delete('/:id', jwtAuth, checkPermission('regulatory_task', 'delete'),
  regulatoryTaskCompletionController.deleteTaskCompletion
);

/**
 * @swagger
 * /api/regulatory-task-completion/{id}/review:
 *   patch:
 *     summary: 审核监管任务结项记录
 *     tags: [RegulatoryTaskCompletion]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 记录ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *               - reviewer_name
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [pending, approved, rejected, completed]
 *                 description: 审核状态
 *               review_comments:
 *                 type: string
 *                 description: 审核意见
 *               reviewer_name:
 *                 type: string
 *                 description: 审核人员
 *     responses:
 *       200:
 *         description: 监管任务结项记录审核成功
 *       400:
 *         description: 无效的状态值
 *       404:
 *         description: 记录不存在
 */
router.patch('/:id/review', jwtAuth, checkPermission('regulatory_task', 'review'),
  regulatoryTaskCompletionController.reviewTaskCompletion
);

/**
 * @swagger
 * /api/regulatory-task-completion/stats:
 *   get:
 *     summary: 获取监管任务结项统计数据
 *     tags: [RegulatoryTaskCompletion]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取统计数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: success
 *                 data:
 *                   type: object
 *                   properties:
 *                     statusStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           status:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     monthlyStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           month:
 *                             type: string
 *                           count:
 *                             type: integer
 *                           total_amount:
 *                             type: number
 *                     productStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           product_name:
 *                             type: string
 *                           count:
 *                             type: integer
 *                           total_amount:
 *                             type: number
 */
router.get('/stats', jwtAuth, checkPermission('regulatory_task', 'read'),
  regulatoryTaskCompletionController.getTaskCompletionStats
);

module.exports = router;