const express = require('express');
const router = express.Router();
const orderController = require('../controllers/orderController');

/**
 * @swagger
 * tags:
 *   name: Orders
 *   description: 订单管理
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     Order:
 *       type: object
 *       required:
 *         - id
 *         - userId
 *         - totalAmount
 *         - status
 *       properties:
 *         id:
 *           type: integer
 *           description: 订单ID
 *         userId:
 *           type: integer
 *           description: 用户ID
 *         totalAmount:
 *           type: number
 *           format: float
 *           description: 订单总金额
 *         status:
 *           type: string
 *           description: 订单状态
 *           enum: [pending, paid, shipped, delivered, cancelled]
 *       example:
 *         id: 1
 *         userId: 2
 *         totalAmount: 199.98
 *         status: "paid"
 */

/**
 * @swagger
 * /api/orders:
 *   get:
 *     summary: 获取所有订单
 *     tags: [Orders]
 *     responses:
 *       200:
 *         description: 订单列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Order'
 *       500:
 *         description: 服务器错误
 */

// 获取所有订单
router.get('/', orderController.getAllOrders);

// 根据用户名搜索订单
router.get('/search', orderController.searchOrdersByUsername);

/**
 * @swagger
 * /api/orders/{id}:
 *   get:
 *     summary: 根据ID获取订单
 *     tags: [Orders]
 *     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/Order'
 *       404:
 *         description: 订单未找到
 *       500:
 *         description: 服务器错误
 */

// 根据ID获取订单
router.get('/:id', orderController.getOrderById);

// 创建订单
router.post('/', orderController.createOrder);

// 更新订单
router.put('/:id', orderController.updateOrder);

// 删除订单
router.delete('/:id', orderController.deleteOrder);

// 获取用户的订单列表
router.get('/user/:userId', orderController.getOrdersByUserId);

module.exports = router;