import { createCrudRoutes } from '../../../utils/generic-crud.routes';
import { authMiddleware } from '../../../middleware/auth.middleware';
import { adminMiddleware } from '../../../middleware/admin.middleware';
import { Order } from '../../../modules/orders/order.entity';
import {
  OrderCreateSchema,
  OrderUpdateSchema,
  OrderGetSchema,
  OrderResponseSchema,
  OrderListSchema
} from '../../../modules/orders/order.schema';
import { OpenAPIHono } from '@hono/zod-openapi';
import statsRoutes from './stats';

// 使用通用CRUD路由创建订单管理API
const orderRoutes = createCrudRoutes({
  entity: Order,
  createSchema: OrderCreateSchema,
  updateSchema: OrderUpdateSchema,
  getSchema: OrderResponseSchema,
  listSchema: OrderResponseSchema,
  searchFields: ['id', 'user.username', 'user.phone'],
  relations: ['user', 'route'],
  middleware: [authMiddleware, adminMiddleware],
  userTracking: {
    createdByField: 'createdBy',
    updatedByField: 'updatedBy'
  }
})

export default new OpenAPIHono()
  // 合并统计路由
  .route('/stats', statsRoutes)
  .route('/', orderRoutes);