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

/**
 * @swagger
 * tags:
 *   name: Products
 *   description: 产品管理
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     Product:
 *       type: object
 *       required:
 *         - id
 *         - name
 *         - price
 *         - stock
 *       properties:
 *         id:
 *           type: integer
 *           description: 产品ID
 *         name:
 *           type: string
 *           description: 产品名称
 *         description:
 *           type: string
 *           description: 产品描述
 *         price:
 *           type: number
 *           format: float
 *           description: 产品价格
 *         stock:
 *           type: integer
 *           description: 产品库存
 *         status:
 *           type: string
 *           enum: [active, inactive]
 *           description: 产品状态
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 *       example:
 *         id: 1
 *         name: "示例产品1"
 *         description: "这是一个示例产品"
 *         price: 99.99
 *         stock: 100
 *         status: "active"
 */

/**
 * @swagger
 * /api/products:
 *   get:
 *     summary: 获取所有产品
 *     tags: [Products]
 *     responses:
 *       200:
 *         description: 产品列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Product'
 *       500:
 *         description: 服务器错误
 */
router.get('/', productController.getAllProducts);

// 根据产品名称搜索产品
router.get('/search', productController.searchProductsByName);

/**
 * @swagger
 * /api/products:
 *   post:
 *     summary: 创建新产品
 *     tags: [Products]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - price
 *               - stock
 *             properties:
 *               name:
 *                 type: string
 *               description:
 *                 type: string
 *               price:
 *                 type: number
 *               stock:
 *                 type: integer
 *               status:
 *                 type: string
 *                 enum: [active, inactive]
 *     responses:
 *       201:
 *         description: 产品创建成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 */
router.post('/', verifyToken, productController.createProduct);



/**
 * @swagger
 * /api/products/{id}:
 *   get:
 *     summary: 根据ID获取产品
 *     tags: [Products]
 *     parameters:
 *       - in: path
 *         name: id
 *         schema:
 *           type: integer
 *         required: true
 *         description: 产品ID
 *     responses:
 *       200:
 *         description: 产品信息
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/Product'
 *       404:
 *         description: 产品未找到
 *       500:
 *         description: 服务器错误
 */
router.get('/:id', productController.getProductById);

/**
 * @swagger
 * /api/products/{id}:
 *   put:
 *     summary: 更新产品
 *     tags: [Products]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         schema:
 *           type: integer
 *         required: true
 *         description: 产品ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               description:
 *                 type: string
 *               price:
 *                 type: number
 *               stock:
 *                 type: integer
 *               status:
 *                 type: string
 *                 enum: [active, inactive]
 *     responses:
 *       200:
 *         description: 产品更新成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 产品未找到
 */
router.put('/:id', verifyToken, productController.updateProduct);

/**
 * @swagger
 * /api/products/{id}:
 *   delete:
 *     summary: 删除产品
 *     tags: [Products]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         schema:
 *           type: integer
 *         required: true
 *         description: 产品ID
 *     responses:
 *       200:
 *         description: 产品删除成功
 *       401:
 *         description: 未授权
 *       404:
 *         description: 产品未找到
 */
router.delete('/:id', verifyToken, productController.deleteProduct);

module.exports = router;