const express = require('express');
const router = express.Router();
const AnimalClaimController = require('../controllers/animalClaim');
const { authenticateToken, requireRole } = require('../middleware/auth');

/**
 * @swagger
 * components:
 *   schemas:
 *     AnimalClaim:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 认领申请ID
 *         claim_no:
 *           type: string
 *           description: 认领订单号
 *         animal_id:
 *           type: integer
 *           description: 动物ID
 *         animal_name:
 *           type: string
 *           description: 动物名称
 *         animal_type:
 *           type: string
 *           description: 动物类型
 *         animal_image:
 *           type: string
 *           description: 动物图片
 *         user_id:
 *           type: integer
 *           description: 用户ID
 *         username:
 *           type: string
 *           description: 用户名
 *         user_phone:
 *           type: string
 *           description: 用户手机号
 *         claim_reason:
 *           type: string
 *           description: 认领理由
 *         claim_duration:
 *           type: integer
 *           description: 认领时长（月）
 *         total_amount:
 *           type: number
 *           format: float
 *           description: 总金额
 *         contact_info:
 *           type: string
 *           description: 联系方式
 *         status:
 *           type: string
 *           enum: [pending, approved, rejected, cancelled]
 *           description: 申请状态
 *         start_date:
 *           type: string
 *           format: date-time
 *           description: 开始日期
 *         end_date:
 *           type: string
 *           format: date-time
 *           description: 结束日期
 *         reviewed_by:
 *           type: integer
 *           description: 审核人ID
 *         reviewer_name:
 *           type: string
 *           description: 审核人姓名
 *         review_remark:
 *           type: string
 *           description: 审核备注
 *         reviewed_at:
 *           type: string
 *           format: date-time
 *           description: 审核时间
 *         approved_at:
 *           type: string
 *           format: date-time
 *           description: 通过时间
 *         cancelled_at:
 *           type: string
 *           format: date-time
 *           description: 取消时间
 *         cancel_reason:
 *           type: string
 *           description: 取消原因
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 *     
 *     ClaimStatistics:
 *       type: object
 *       properties:
 *         basic:
 *           type: object
 *           properties:
 *             total_claims:
 *               type: integer
 *               description: 总申请数
 *             pending_claims:
 *               type: integer
 *               description: 待审核申请数
 *             approved_claims:
 *               type: integer
 *               description: 已通过申请数
 *             rejected_claims:
 *               type: integer
 *               description: 已拒绝申请数
 *             cancelled_claims:
 *               type: integer
 *               description: 已取消申请数
 *             total_amount:
 *               type: number
 *               format: float
 *               description: 总金额
 *             avg_duration:
 *               type: number
 *               format: float
 *               description: 平均认领时长
 *         by_type:
 *           type: array
 *           items:
 *             type: object
 *             properties:
 *               type:
 *                 type: string
 *                 description: 动物类型
 *               claim_count:
 *                 type: integer
 *                 description: 申请数量
 *               approved_count:
 *                 type: integer
 *                 description: 通过数量
 *               total_amount:
 *                 type: number
 *                 format: float
 *                 description: 总金额
 *         by_month:
 *           type: array
 *           items:
 *             type: object
 *             properties:
 *               month:
 *                 type: string
 *                 description: 月份
 *               claim_count:
 *                 type: integer
 *                 description: 申请数量
 *               approved_count:
 *                 type: integer
 *                 description: 通过数量
 *               total_amount:
 *                 type: number
 *                 format: float
 *                 description: 总金额
 */

/**
 * @swagger
 * /api/v1/animal-claims:
 *   post:
 *     summary: 申请认领动物
 *     tags: [动物认领]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - animal_id
 *               - contact_info
 *             properties:
 *               animal_id:
 *                 type: integer
 *                 description: 动物ID
 *               claim_reason:
 *                 type: string
 *                 description: 认领理由
 *               claim_duration:
 *                 type: integer
 *                 minimum: 1
 *                 maximum: 60
 *                 description: 认领时长（月，默认12个月）
 *               contact_info:
 *                 type: string
 *                 description: 联系方式
 *     responses:
 *       201:
 *         description: 认领申请提交成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/AnimalClaim'
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 */
router.post('/', authenticateToken, AnimalClaimController.createClaim);

/**
 * @swagger
 * /api/v1/animal-claims/my:
 *   get:
 *     summary: 获取我的认领申请列表
 *     tags: [动物认领]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, approved, rejected, cancelled]
 *         description: 申请状态
 *       - in: query
 *         name: animal_type
 *         schema:
 *           type: string
 *         description: 动物类型
 *       - 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:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/AnimalClaim'
 *                 pagination:
 *                   $ref: '#/components/schemas/Pagination'
 */
router.get('/my', authenticateToken, AnimalClaimController.getUserClaims);

/**
 * @swagger
 * /api/v1/animal-claims/statistics:
 *   get:
 *     summary: 获取认领统计信息
 *     tags: [动物认领]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - 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: animal_type
 *         schema:
 *           type: string
 *         description: 动物类型
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/ClaimStatistics'
 */
router.get('/statistics', authenticateToken, requireRole(['admin', 'manager']), AnimalClaimController.getClaimStatistics);

/**
 * @swagger
 * /api/v1/animal-claims/animal/{animal_id}:
 *   get:
 *     summary: 获取动物的认领申请列表
 *     tags: [动物认领]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: animal_id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 动物ID
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, approved, rejected, cancelled]
 *         description: 申请状态
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/AnimalClaim'
 *                 pagination:
 *                   $ref: '#/components/schemas/Pagination'
 */
router.get('/animal/:animal_id', authenticateToken, requireRole(['admin', 'manager']), AnimalClaimController.getAnimalClaims);

/**
 * @swagger
 * /api/v1/animal-claims/check-permission/{animal_id}:
 *   get:
 *     summary: 检查认领权限
 *     tags: [动物认领]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: animal_id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 动物ID
 *     responses:
 *       200:
 *         description: 检查成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     can_claim:
 *                       type: boolean
 *                       description: 是否可以认领
 */
router.get('/check-permission/:animal_id', authenticateToken, AnimalClaimController.checkClaimPermission);

/**
 * @swagger
 * /api/v1/animal-claims:
 *   get:
 *     summary: 获取所有认领申请列表（管理员）
 *     tags: [动物认领]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, approved, rejected, cancelled]
 *         description: 申请状态
 *       - in: query
 *         name: animal_type
 *         schema:
 *           type: string
 *         description: 动物类型
 *       - 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: 结束日期
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *         description: 关键词搜索（订单号、动物名称、用户名）
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/AnimalClaim'
 *                 pagination:
 *                   $ref: '#/components/schemas/Pagination'
 */
router.get('/', authenticateToken, requireRole(['admin', 'manager']), AnimalClaimController.getAllClaims);

/**
 * @swagger
 * /api/v1/animal-claims/{id}/cancel:
 *   put:
 *     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
 *                 message:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/AnimalClaim'
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 */
router.put('/:id/cancel', authenticateToken, AnimalClaimController.cancelClaim);

/**
 * @swagger
 * /api/v1/animal-claims/{id}/review:
 *   put:
 *     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:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [approved, rejected]
 *                 description: 审核状态
 *               review_remark:
 *                 type: string
 *                 description: 审核备注
 *     responses:
 *       200:
 *         description: 审核成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/AnimalClaim'
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.put('/:id/review', authenticateToken, requireRole(['admin', 'manager']), AnimalClaimController.reviewClaim);

/**
 * @swagger
 * /api/v1/animal-claims/{id}/renew:
 *   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:
 *               - duration
 *               - payment_method
 *             properties:
 *               duration:
 *                 type: integer
 *                 minimum: 1
 *                 maximum: 60
 *                 description: 续期时长（月）
 *               payment_method:
 *                 type: string
 *                 enum: [wechat, alipay, bank_transfer]
 *                 description: 支付方式
 *     responses:
 *       200:
 *         description: 续期申请成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     renewal:
 *                       type: object
 *                       description: 续期记录
 *                     amount:
 *                       type: number
 *                       format: float
 *                       description: 续期金额
 *                     message:
 *                       type: string
 *                       description: 提示信息
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 */
router.post('/:id/renew', authenticateToken, AnimalClaimController.renewClaim);

module.exports = router;