const express = require('express');
const { body, query } = require('express-validator');
const AnimalController = require('../controllers/animal');
const { authenticateUser: authenticate, requireRole: requireAdmin, requireRole: requireMerchant } = require('../middleware/auth');

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: Animals
 *   description: 动物管理相关接口
 */

/**
 * @swagger
 * /animals/search:
 *   get:
 *     summary: 搜索动物（公开接口）
 *     tags: [Animals]
 *     parameters:
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *       - in: query
 *         name: species
 *         schema:
 *           type: string
 *         description: 物种
 *       - in: query
 *         name: minPrice
 *         schema:
 *           type: number
 *         description: 最低价格
 *       - in: query
 *         name: maxPrice
 *         schema:
 *           type: number
 *         description: 最高价格
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 搜索成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     animals:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/Animal'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 *       500:
 *         description: 服务器内部错误
 */
router.get('/search', AnimalController.searchAnimals);

/**
 * @swagger
 * /animals:
 *   get:
 *     summary: 获取动物列表（商家）
 *     tags: [Animals]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *         description: 每页数量
 *       - in: query
 *         name: species
 *         schema:
 *           type: string
 *         description: 物种
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *         description: 状态
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     animals:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/Animal'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/', authenticate, requireMerchant, AnimalController.getAnimals);

/**
 * @swagger
 * /animals/stats:
 *   get:
 *     summary: 获取动物统计信息（商家）
 *     tags: [Animals]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     statistics:
 *                       $ref: '#/components/schemas/AnimalStatistics'
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/stats', authenticate, requireMerchant, AnimalController.getAnimalStatistics);

/**
 * @swagger
 * /animals/admin/all:
 *   get:
 *     summary: 获取所有动物信息（管理员）
 *     tags: [Animals]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *         description: 每页数量
 *       - in: query
 *         name: species
 *         schema:
 *           type: string
 *         description: 物种
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *         description: 状态
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     animals:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/Animal'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.get('/admin/all', authenticate, requireAdmin, AnimalController.getAllAnimals);

/**
 * @swagger
 * /animals/statistics:
 *   get:
 *     summary: 获取动物统计信息
 *     tags: [Animals]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     statistics:
 *                       type: object
 *                       properties:
 *                         total:
 *                           type: integer
 *                         bySpecies:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               species:
 *                                 type: string
 *                               count:
 *                                 type: integer
 *                         byStatus:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               status:
 *                                 type: string
 *                               count:
 *                                 type: integer
 *                         topMerchants:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               merchant_name:
 *                                 type: string
 *                               animal_count:
 *                                 type: integer
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/statistics', authenticate, AnimalController.getAnimalStatistics);

/**
 * @swagger
 * /animals/{animalId}:
 *   get:
 *     summary: 获取单个动物详情
 *     tags: [Animals]
 *     parameters:
 *       - in: path
 *         name: animalId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 动物ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     animal:
 *                       $ref: '#/components/schemas/AnimalDetail'
 *       404:
 *         description: 动物不存在
 *       500:
 *         description: 服务器内部错误
 */
router.get('/:animalId', AnimalController.getAnimal);

/**
 * @swagger
 * /animals:
 *   post:
 *     summary: 创建动物信息
 *     tags: [Animals]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - species
 *               - price
 *             properties:
 *               name:
 *                 type: string
 *                 description: 动物名称
 *               species:
 *                 type: string
 *                 description: 动物种类
 *               breed:
 *                 type: string
 *                 description: 品种
 *               age:
 *                 type: integer
 *                 description: 年龄
 *               gender:
 *                 type: string
 *                 enum: [male, female]
 *                 description: 性别
 *               price:
 *                 type: number
 *                 description: 价格
 *               description:
 *                 type: string
 *                 description: 描述
 *               images:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 图片URL列表
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     animal:
 *                       $ref: '#/components/schemas/Animal'
 *                     message:
 *                       type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.post('/', 
  authenticate, 
  requireMerchant,
  [
    body('name').notEmpty().withMessage('名称不能为空'),
    body('species').notEmpty().withMessage('种类不能为空'),
    body('price').isFloat({ min: 0 }).withMessage('价格必须大于0')
  ],
  AnimalController.createAnimal
);

/**
 * @swagger
 * /animals/{animalId}:
 *   put:
 *     summary: 更新动物信息
 *     tags: [Animals]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: animalId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 动物ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 动物名称
 *               species:
 *                 type: string
 *                 description: 动物种类
 *               breed:
 *                 type: string
 *                 description: 品种
 *               age:
 *                 type: integer
 *                 description: 年龄
 *               gender:
 *                 type: string
 *                 enum: [male, female]
 *                 description: 性别
 *               price:
 *                 type: number
 *                 description: 价格
 *               description:
 *                 type: string
 *                 description: 描述
 *               images:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 图片URL列表
 *               status:
 *                 type: string
 *                 enum: [available, sold, reserved]
 *                 description: 状态
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     animal:
 *                       $ref: '#/components/schemas/Animal'
 *                     message:
 *                       type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 动物不存在
 *       500:
 *         description: 服务器内部错误
 */
router.put('/:animalId', 
  authenticate, 
  requireMerchant,
  AnimalController.updateAnimal
);

/**
 * @swagger
 * /animals/{animalId}:
 *   delete:
 *     summary: 删除动物信息
 *     tags: [Animals]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: animalId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 动物ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     message:
 *                       type: string
 *                     animalId:
 *                       type: integer
 *       401:
 *         description: 未授权
 *       404:
 *         description: 动物不存在
 *       500:
 *         description: 服务器内部错误
 */
router.delete('/:animalId', 
  authenticate, 
  requireMerchant,
  AnimalController.deleteAnimal
);

module.exports = router;