const express = require('express');
const router = express.Router();
const farmController = require('../controllers/farmController');
const searchLogger = require('../middleware/search-logger');
const { createSuccessResponse, createErrorResponse, createPaginatedResponse, SUCCESS_MESSAGES, ERROR_CODES } = require('../utils/apiResponse');

/**
 * @swagger
 * /api/farms:
 *   get:
 *     summary: 获取所有养殖场
 *     tags: [Farms]
 *     responses:
 *       200:
 *         description: 成功获取养殖场列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Farm'
 */
router.get('/', farmController.getAllFarms);

// 根据养殖场名称搜索养殖场（添加搜索监听中间件）
router.get('/search', searchLogger, farmController.searchFarmsByName);

// 公共路由必须在参数路由之前定义
router.get('/public', farmController.getAllFarms);

/**
 * @swagger
 * /api/farms/{id}:
 *   get:
 *     summary: 根据ID获取养殖场
 *     tags: [Farms]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 养殖场ID
 *     responses:
 *       200:
 *         description: 成功获取养殖场详情
 *       404:
 *         description: 养殖场不存在
 */
router.get('/:id', farmController.getFarmById);

/**
 * @swagger
 * /api/farms:
 *   post:
 *     summary: 创建新养殖场
 *     tags: [Farms]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/FarmInput'
 *     responses:
 *       201:
 *         description: 养殖场创建成功
 *       400:
 *         description: 请求参数错误
 */
router.post('/', farmController.createFarm);

/**
 * @swagger
 * /api/farms/{id}:
 *   put:
 *     summary: 更新养殖场信息
 *     tags: [Farms]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 养殖场ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/FarmInput'
 *     responses:
 *       200:
 *         description: 养殖场更新成功
 *       404:
 *         description: 养殖场不存在
 */
router.put('/:id', farmController.updateFarm);

/**
 * @swagger
 * /api/farms/{id}:
 *   delete:
 *     summary: 删除养殖场
 *     tags: [Farms]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 养殖场ID
 *     responses:
 *       200:
 *         description: 养殖场删除成功
 *       404:
 *         description: 养殖场不存在
 */
router.delete('/:id', farmController.deleteFarm);

/**
 * @swagger
 * /api/farms/{id}/animals:
 *   get:
 *     summary: 获取养殖场的动物列表
 *     tags: [Farms]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 养殖场ID
 *     responses:
 *       200:
 *         description: 成功获取动物列表
 *       404:
 *         description: 养殖场不存在
 */
// router.get('/:id/animals', farmController.getFarmAnimals); // 暂时注释，方法不存在

/**
 * @swagger
 * /api/farms/{id}/devices:
 *   get:
 *     summary: 获取养殖场的设备列表
 *     tags: [Farms]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 养殖场ID
 *     responses:
 *       200:
 *         description: 成功获取设备列表
 *       404:
 *         description: 养殖场不存在
 */
// router.get('/:id/devices', farmController.getFarmDevices); // 暂时注释，方法不存在

// 公共农场数据接口（保留兼容性）
module.exports = router;