/**
 * 贷款申请路由
 * @file loanApplications.js
 * @description 银行系统贷款申请相关路由配置
 */
const express = require('express');
const router = express.Router();
const { body } = require('express-validator');
const loanApplicationController = require('../controllers/loanApplicationController');
const { authMiddleware } = require('../middleware/auth');

// 所有路由都需要认证
router.use(authMiddleware);

/**
 * @swagger
 * /api/loan-applications:
 *   get:
 *     summary: 获取贷款申请列表
 *     tags: [贷款申请]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: searchField
 *         schema:
 *           type: string
 *           enum: [applicationNumber, customerName, productName]
 *           default: applicationNumber
 *         description: 搜索字段
 *       - in: query
 *         name: searchValue
 *         schema:
 *           type: string
 *         description: 搜索值
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending_review, verification_pending, pending_binding, approved, rejected]
 *         description: 申请状态筛选
 *       - in: query
 *         name: sortField
 *         schema:
 *           type: string
 *           default: createdAt
 *         description: 排序字段
 *       - in: query
 *         name: sortOrder
 *         schema:
 *           type: string
 *           enum: [ASC, DESC]
 *           default: DESC
 *         description: 排序方向
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     applications:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/LoanApplication'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 */
router.get('/', loanApplicationController.getApplications);

/**
 * @swagger
 * /api/loan-applications/{id}:
 *   get:
 *     summary: 获取贷款申请详情
 *     tags: [贷款申请]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 申请ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/LoanApplication'
 *       404:
 *         description: 申请不存在
 */
router.get('/:id', loanApplicationController.getApplicationById);

/**
 * @swagger
 * /api/loan-applications/{id}/audit:
 *   post:
 *     summary: 审核贷款申请
 *     tags: [贷款申请]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 申请ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - action
 *               - comment
 *             properties:
 *               action:
 *                 type: string
 *                 enum: [approve, reject]
 *                 description: 审核动作
 *               comment:
 *                 type: string
 *                 description: 审核意见
 *     responses:
 *       200:
 *         description: 审核成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: integer
 *                     status:
 *                       type: string
 *                     action:
 *                       type: string
 *                     comment:
 *                       type: string
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 申请不存在
 */
router.post('/:id/audit', [
  body('action')
    .isIn(['approve', 'reject'])
    .withMessage('审核动作必须是approve或reject'),
  body('comment')
    .notEmpty()
    .withMessage('审核意见不能为空')
    .isLength({ max: 500 })
    .withMessage('审核意见不能超过500个字符')
], loanApplicationController.auditApplication);

/**
 * @swagger
 * /api/loan-applications/stats:
 *   get:
 *     summary: 获取申请统计信息
 *     tags: [贷款申请]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: object
 *                       properties:
 *                         applications:
 *                           type: integer
 *                         amount:
 *                           type: number
 *                     byStatus:
 *                       type: object
 *                       properties:
 *                         counts:
 *                           type: object
 *                         amounts:
 *                           type: object
 */
router.get('/stats', loanApplicationController.getApplicationStats);

/**
 * @swagger
 * /api/loan-applications/batch/status:
 *   put:
 *     summary: 批量更新申请状态
 *     tags: [贷款申请]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - ids
 *               - status
 *             properties:
 *               ids:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 申请ID数组
 *               status:
 *                 type: string
 *                 enum: [approved, rejected]
 *                 description: 目标状态
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     updatedCount:
 *                       type: integer
 *                     status:
 *                       type: string
 *       400:
 *         description: 请求参数错误
 */
router.put('/batch/status', [
  body('ids')
    .isArray({ min: 1 })
    .withMessage('请选择要操作的申请'),
  body('status')
    .isIn(['approved', 'rejected'])
    .withMessage('状态必须是approved或rejected')
], loanApplicationController.batchUpdateStatus);

/**
 * @swagger
 * components:
 *   schemas:
 *     LoanApplication:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 申请ID
 *         applicationNumber:
 *           type: string
 *           description: 申请单号
 *         productName:
 *           type: string
 *           description: 贷款产品名称
 *         farmerName:
 *           type: string
 *           description: 申请养殖户姓名
 *         borrowerName:
 *           type: string
 *           description: 贷款人姓名
 *         borrowerIdNumber:
 *           type: string
 *           description: 贷款人身份证号
 *         assetType:
 *           type: string
 *           description: 生资种类
 *         applicationQuantity:
 *           type: string
 *           description: 申请数量
 *         amount:
 *           type: number
 *           description: 申请额度
 *         status:
 *           type: string
 *           enum: [pending_review, verification_pending, pending_binding, approved, rejected]
 *           description: 申请状态
 *         type:
 *           type: string
 *           enum: [personal, business, mortgage]
 *           description: 申请类型
 *         term:
 *           type: integer
 *           description: 申请期限（月）
 *         interestRate:
 *           type: number
 *           description: 预计利率
 *         phone:
 *           type: string
 *           description: 联系电话
 *         purpose:
 *           type: string
 *           description: 申请用途
 *         remark:
 *           type: string
 *           description: 备注
 *         applicationTime:
 *           type: string
 *           format: date-time
 *           description: 申请时间
 *         approvedTime:
 *           type: string
 *           format: date-time
 *           description: 审批通过时间
 *         rejectedTime:
 *           type: string
 *           format: date-time
 *           description: 审批拒绝时间
 *         auditRecords:
 *           type: array
 *           items:
 *             $ref: '#/components/schemas/AuditRecord'
 *           description: 审核记录
 *     AuditRecord:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 记录ID
 *         action:
 *           type: string
 *           enum: [submit, approve, reject, review, verification, binding]
 *           description: 审核动作
 *         auditor:
 *           type: string
 *           description: 审核人
 *         auditorId:
 *           type: integer
 *           description: 审核人ID
 *         comment:
 *           type: string
 *           description: 审核意见
 *         time:
 *           type: string
 *           format: date-time
 *           description: 审核时间
 *         previousStatus:
 *           type: string
 *           description: 审核前状态
 *         newStatus:
 *           type: string
 *           description: 审核后状态
 *     Pagination:
 *       type: object
 *       properties:
 *         current:
 *           type: integer
 *           description: 当前页码
 *         pageSize:
 *           type: integer
 *           description: 每页数量
 *         total:
 *           type: integer
 *           description: 总记录数
 *         totalPages:
 *           type: integer
 *           description: 总页数
 */

module.exports = router;
