const express = require('express');
const router = express.Router();
const mapController = require('../controllers/mapController');
const farmController = require('../controllers/farmController');
const { verifyToken } = require('../middleware/auth');

// 公开API路由，不需要验证token
const publicRoutes = express.Router();
router.use('/public', publicRoutes);

// 公开地理编码接口
publicRoutes.get('/geocode', mapController.geocode);

// 公开反向地理编码接口
publicRoutes.get('/reverse-geocode', mapController.reverseGeocode);

// 公开路线规划接口
publicRoutes.get('/direction', mapController.direction);

// 公开周边搜索接口
publicRoutes.get('/place-search', mapController.placeSearch);

// 公开静态地图接口
publicRoutes.get('/static-map', mapController.staticMap);

// 公开IP定位接口
publicRoutes.get('/ip-location', mapController.ipLocation);

// 公开获取养殖场地理位置数据
publicRoutes.get('/farms', farmController.getAllFarms);

/**
 * @swagger
 * tags:
 *   name: Map
 *   description: 百度地图API服务
 */

/**
 * @swagger
 * /api/map/geocode:
 *   get:
 *     summary: 地理编码 - 将地址转换为经纬度坐标
 *     tags: [Map]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: address
 *         schema:
 *           type: string
 *         required: true
 *         description: 地址
 *     responses:
 *       200:
 *         description: 地理编码成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 result:
 *                   type: object
 *                   properties:
 *                     location:
 *                       type: object
 *                       properties:
 *                         lng:
 *                           type: number
 *                           description: 经度
 *                         lat:
 *                           type: number
 *                           description: 纬度
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/geocode', verifyToken, mapController.geocode);

/**
 * @swagger
 * /api/map/reverse-geocode:
 *   get:
 *     summary: 逆地理编码 - 将经纬度坐标转换为地址
 *     tags: [Map]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: lat
 *         schema:
 *           type: number
 *         required: true
 *         description: 纬度
 *       - in: query
 *         name: lng
 *         schema:
 *           type: number
 *         required: true
 *         description: 经度
 *     responses:
 *       200:
 *         description: 逆地理编码成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 result:
 *                   type: object
 *                   properties:
 *                     formatted_address:
 *                       type: string
 *                       description: 结构化地址
 *                     addressComponent:
 *                       type: object
 *                       description: 地址组成部分
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/reverse-geocode', verifyToken, mapController.reverseGeocode);

/**
 * @swagger
 * /api/map/direction:
 *   get:
 *     summary: 路线规划
 *     tags: [Map]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: origin
 *         schema:
 *           type: string
 *         required: true
 *         description: 起点坐标，格式：纬度,经度
 *       - in: query
 *         name: destination
 *         schema:
 *           type: string
 *         required: true
 *         description: 终点坐标，格式：纬度,经度
 *       - in: query
 *         name: mode
 *         schema:
 *           type: string
 *           enum: [driving, walking, riding, transit]
 *         required: false
 *         description: 交通方式：driving(驾车)、walking(步行)、riding(骑行)、transit(公交)
 *     responses:
 *       200:
 *         description: 路线规划成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 result:
 *                   type: object
 *                   description: 路线规划结果
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/direction', verifyToken, mapController.direction);

/**
 * @swagger
 * /api/map/place-search:
 *   get:
 *     summary: 周边搜索
 *     tags: [Map]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: query
 *         schema:
 *           type: string
 *         required: true
 *         description: 搜索关键词
 *       - in: query
 *         name: location
 *         schema:
 *           type: string
 *         required: true
 *         description: 中心点坐标，格式：纬度,经度
 *       - in: query
 *         name: radius
 *         schema:
 *           type: number
 *         required: false
 *         description: 搜索半径，单位：米，默认1000米
 *     responses:
 *       200:
 *         description: 周边搜索成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 results:
 *                   type: array
 *                   items:
 *                     type: object
 *                     description: 搜索结果
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/place-search', verifyToken, mapController.placeSearch);

/**
 * @swagger
 * /api/map/static-map:
 *   get:
 *     summary: 获取静态地图
 *     tags: [Map]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: center
 *         schema:
 *           type: string
 *         required: true
 *         description: 地图中心点坐标，格式：纬度,经度
 *       - in: query
 *         name: width
 *         schema:
 *           type: number
 *         required: false
 *         description: 地图图片宽度，默认400
 *       - in: query
 *         name: height
 *         schema:
 *           type: number
 *         required: false
 *         description: 地图图片高度，默认300
 *       - in: query
 *         name: zoom
 *         schema:
 *           type: number
 *         required: false
 *         description: 地图缩放级别，默认12
 *     responses:
 *       200:
 *         description: 获取静态地图成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 url:
 *                   type: string
 *                   description: 静态地图URL
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/static-map', verifyToken, mapController.staticMap);

/**
 * @swagger
 * /api/map/ip-location:
 *   get:
 *     summary: IP定位
 *     tags: [Map]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: ip
 *         schema:
 *           type: string
 *         required: false
 *         description: IP地址，可选，默认使用用户当前IP
 *     responses:
 *       200:
 *         description: IP定位成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 result:
 *                   type: object
 *                   description: IP定位结果
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/ip-location', verifyToken, mapController.ipLocation);

module.exports = router;