/**
 * 贷款解押路由
 * @file loanRelease.js
 * @description 银行系统贷款解押相关路由
 */
const express = require('express');
const router = express.Router();
const { authMiddleware } = require('../middleware/auth');
const {
  getReleases,
  getReleaseDetail,
  createRelease,
  updateRelease,
  processRelease,
  completeRelease,
  deleteRelease,
  getReleaseStats
} = require('../controllers/loanReleaseController');

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

/**
 * @swagger
 * /api/loan-releases:
 *   get:
 *     summary: 获取解押申请列表
 *     tags: [Loan Release]
 *     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: [application_number, customer_name, product_name]
 *           default: application_number
 *         description: 搜索字段
 *       - in: query
 *         name: searchValue
 *         schema:
 *           type: string
 *         description: 搜索值
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, processing, approved, rejected, completed, released]
 *         description: 解押状态筛选
 *       - in: query
 *         name: sortField
 *         schema:
 *           type: string
 *           default: created_at
 *         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:
 *                     releases:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/LoanRelease'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       500:
 *         $ref: '#/components/responses/InternalServerError'
 */
router.get('/', getReleases);

/**
 * @swagger
 * /api/loan-releases/stats:
 *   get:
 *     summary: 获取解押统计信息
 *     tags: [Loan Release]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 *                       example: 6
 *                     pending:
 *                       type: integer
 *                       example: 0
 *                     processing:
 *                       type: integer
 *                       example: 0
 *                     approved:
 *                       type: integer
 *                       example: 0
 *                     rejected:
 *                       type: integer
 *                       example: 0
 *                     completed:
 *                       type: integer
 *                       example: 0
 *                     released:
 *                       type: integer
 *                       example: 6
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       500:
 *         $ref: '#/components/responses/InternalServerError'
 */
router.get('/stats', getReleaseStats);

/**
 * @swagger
 * /api/loan-releases/{id}:
 *   get:
 *     summary: 获取解押申请详情
 *     tags: [Loan Release]
 *     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:
 *                   allOf:
 *                     - $ref: '#/components/schemas/LoanRelease'
 *                     - type: object
 *                       properties:
 *                         history:
 *                           type: array
 *                           items:
 *                             $ref: '#/components/schemas/LoanReleaseHistory'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       404:
 *         $ref: '#/components/responses/NotFoundError'
 *       500:
 *         $ref: '#/components/responses/InternalServerError'
 */
router.get('/:id', getReleaseDetail);

/**
 * @swagger
 * /api/loan-releases:
 *   post:
 *     summary: 创建解押申请
 *     tags: [Loan Release]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/LoanReleaseCreateRequest'
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: integer
 *                     applicationNumber:
 *                       type: string
 *       400:
 *         $ref: '#/components/responses/ValidationError'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       500:
 *         $ref: '#/components/responses/InternalServerError'
 */
router.post('/', createRelease);

/**
 * @swagger
 * /api/loan-releases/{id}:
 *   put:
 *     summary: 更新解押申请
 *     tags: [Loan Release]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 解押申请ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/LoanReleaseCreateRequest'
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Success'
 *       400:
 *         $ref: '#/components/responses/ValidationError'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       404:
 *         $ref: '#/components/responses/NotFoundError'
 *       500:
 *         $ref: '#/components/responses/InternalServerError'
 */
router.put('/:id', updateRelease);

/**
 * @swagger
 * /api/loan-releases/{id}/process:
 *   post:
 *     summary: 处理解押申请（通过/拒绝）
 *     tags: [Loan Release]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 解押申请ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/LoanReleaseProcessRequest'
 *     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:
 *         $ref: '#/components/responses/ValidationError'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       404:
 *         $ref: '#/components/responses/NotFoundError'
 *       500:
 *         $ref: '#/components/responses/InternalServerError'
 */
router.post('/:id/process', processRelease);

/**
 * @swagger
 * /api/loan-releases/{id}/complete:
 *   post:
 *     summary: 完成解押
 *     tags: [Loan Release]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 解押申请ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - comment
 *             properties:
 *               comment:
 *                 type: string
 *                 example: 解押手续办理完成
 *     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
 *       400:
 *         $ref: '#/components/responses/ValidationError'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       404:
 *         $ref: '#/components/responses/NotFoundError'
 *       500:
 *         $ref: '#/components/responses/InternalServerError'
 */
router.post('/:id/complete', completeRelease);

/**
 * @swagger
 * /api/loan-releases/{id}:
 *   delete:
 *     summary: 删除解押申请
 *     tags: [Loan Release]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 解押申请ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Success'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       404:
 *         $ref: '#/components/responses/NotFoundError'
 *       500:
 *         $ref: '#/components/responses/InternalServerError'
 */
router.delete('/:id', deleteRelease);

module.exports = router;
