const express = require('express');
const { body, query, param } = require('express-validator');
const MerchantController = require('../controllers/merchant');
const { authenticateUser, requireRole } = require('../middleware/auth');

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: Merchants
 *   description: 商户管理相关接口
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     Merchant:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 商户ID
 *         name:
 *           type: string
 *           description: 商户名称
 *         type:
 *           type: string
 *           enum: [individual, company]
 *           description: 商户类型
 *         contact_person:
 *           type: string
 *           description: 联系人
 *         contact_phone:
 *           type: string
 *           description: 联系电话
 *         email:
 *           type: string
 *           description: 邮箱
 *         address:
 *           type: string
 *           description: 地址
 *         description:
 *           type: string
 *           description: 描述
 *         status:
 *           type: string
 *           enum: [active, inactive, banned]
 *           description: 状态
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 */

/**
 * @swagger
 * /merchants:
 *   get:
 *     summary: 获取商户列表
 *     tags: [Merchants]
 *     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: 20
 *         description: 每页数量
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *         description: 搜索关键词（商户名称、联系人、电话）
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [active, inactive, banned]
 *         description: 状态筛选
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [individual, company]
 *         description: 类型筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Merchant'
 *                 pagination:
 *                   type: object
 *                   properties:
 *                     page:
 *                       type: integer
 *                     limit:
 *                       type: integer
 *                     total:
 *                       type: integer
 *                     totalPages:
 *                       type: integer
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器内部错误
 */
router.get('/', 
  [
    query('page').optional().isInt({ min: 1 }).withMessage('页码必须是正整数'),
    query('limit').optional().isInt({ min: 1, max: 100 }).withMessage('每页数量必须在1-100之间'),
    query('status').optional().isIn(['active', 'inactive', 'banned']).withMessage('状态值无效'),
    query('type').optional().isIn(['individual', 'company']).withMessage('类型值无效')
  ],
  MerchantController.getMerchantList
);

/**
 * @swagger
 * /merchants/{merchantId}:
 *   get:
 *     summary: 获取商户详情
 *     tags: [Merchants]
 *     parameters:
 *       - in: path
 *         name: merchantId
 *         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/Merchant'
 *                     - type: object
 *                       properties:
 *                         animal_count:
 *                           type: integer
 *                           description: 动物数量
 *                         order_count:
 *                           type: integer
 *                           description: 订单数量
 *       400:
 *         description: 商户ID无效
 *       404:
 *         description: 商户不存在
 *       500:
 *         description: 服务器内部错误
 */
router.get('/:merchantId',
  [
    param('merchantId').isInt({ min: 1 }).withMessage('商户ID必须是正整数')
  ],
  MerchantController.getMerchantDetail
);

/**
 * @swagger
 * /merchants:
 *   post:
 *     summary: 创建商户
 *     tags: [Merchants]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - type
 *               - contact_person
 *               - contact_phone
 *             properties:
 *               name:
 *                 type: string
 *                 description: 商户名称
 *               type:
 *                 type: string
 *                 enum: [individual, company]
 *                 description: 商户类型
 *               contact_person:
 *                 type: string
 *                 description: 联系人
 *               contact_phone:
 *                 type: string
 *                 description: 联系电话
 *               email:
 *                 type: string
 *                 description: 邮箱
 *               address:
 *                 type: string
 *                 description: 地址
 *               description:
 *                 type: string
 *                 description: 描述
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/Merchant'
 *                 message:
 *                   type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.post('/',
  authenticateUser,
  requireRole(['admin', 'super_admin']),
  [
    body('name').notEmpty().withMessage('商户名称不能为空'),
    body('type').isIn(['individual', 'company']).withMessage('商户类型必须是 individual 或 company'),
    body('contact_person').notEmpty().withMessage('联系人不能为空'),
    body('contact_phone').notEmpty().withMessage('联系电话不能为空'),
    body('email').optional().isEmail().withMessage('邮箱格式无效')
  ],
  MerchantController.createMerchant
);

/**
 * @swagger
 * /merchants/{merchantId}:
 *   put:
 *     summary: 更新商户信息
 *     tags: [Merchants]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: merchantId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 商户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 商户名称
 *               type:
 *                 type: string
 *                 enum: [individual, company]
 *                 description: 商户类型
 *               contact_person:
 *                 type: string
 *                 description: 联系人
 *               contact_phone:
 *                 type: string
 *                 description: 联系电话
 *               email:
 *                 type: string
 *                 description: 邮箱
 *               address:
 *                 type: string
 *                 description: 地址
 *               description:
 *                 type: string
 *                 description: 描述
 *               status:
 *                 type: string
 *                 enum: [active, inactive, banned]
 *                 description: 状态
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/Merchant'
 *                 message:
 *                   type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 商户不存在
 *       500:
 *         description: 服务器内部错误
 */
router.put('/:merchantId',
  authenticateUser,
  requireRole(['admin', 'super_admin']),
  [
    param('merchantId').isInt({ min: 1 }).withMessage('商户ID必须是正整数'),
    body('name').optional().notEmpty().withMessage('商户名称不能为空'),
    body('type').optional().isIn(['individual', 'company']).withMessage('商户类型必须是 individual 或 company'),
    body('contact_person').optional().notEmpty().withMessage('联系人不能为空'),
    body('contact_phone').optional().notEmpty().withMessage('联系电话不能为空'),
    body('email').optional().isEmail().withMessage('邮箱格式无效'),
    body('status').optional().isIn(['active', 'inactive', 'banned']).withMessage('状态值无效')
  ],
  MerchantController.updateMerchant
);

/**
 * @swagger
 * /merchants/{merchantId}:
 *   delete:
 *     summary: 删除商户
 *     tags: [Merchants]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: merchantId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 商户ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *       400:
 *         description: 商户ID无效
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 商户不存在
 *       500:
 *         description: 服务器内部错误
 */
router.delete('/:merchantId',
  authenticateUser,
  requireRole(['admin', 'super_admin']),
  [
    param('merchantId').isInt({ min: 1 }).withMessage('商户ID必须是正整数')
  ],
  MerchantController.deleteMerchant
);

/**
 * @swagger
 * /merchants/statistics:
 *   get:
 *     summary: 获取商户统计信息
 *     tags: [Merchants]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 *                       description: 总商户数
 *                     active:
 *                       type: integer
 *                       description: 活跃商户数
 *                     inactive:
 *                       type: integer
 *                       description: 非活跃商户数
 *                     banned:
 *                       type: integer
 *                       description: 被禁用商户数
 *                     individual:
 *                       type: integer
 *                       description: 个人商户数
 *                     company:
 *                       type: integer
 *                       description: 企业商户数
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.get('/statistics',
  authenticateUser,
  requireRole(['admin', 'super_admin']),
  MerchantController.getMerchantStatistics
);

module.exports = router;