import { createCrudRoutes } from '../../utils/generic-crud.routes';
import { OpenAPIHono } from '@hono/zod-openapi';
import { Goods } from '../../modules/goods/goods.entity';
import { GoodsSchema, CreateGoodsDto, UpdateGoodsDto } from '../../modules/goods/goods.schema';
import { authMiddleware } from '../../middleware/auth.middleware';
import { File } from '../../modules/files/file.entity';
import randomRoute from './random';

// 创建基础CRUD路由
const goodsRoutes = createCrudRoutes({
  entity: Goods,
  createSchema: CreateGoodsDto,
  updateSchema: UpdateGoodsDto,
  getSchema: GoodsSchema,
  listSchema: GoodsSchema,
  searchFields: ['name', 'instructions'],
  relations: ['category1', 'category2', 'category3', 'supplier', 'imageFile.uploadUser', 'slideImages.uploadUser'],
  middleware: [authMiddleware],
  userTracking: {
    createdByField: 'created_by',
    updatedByField: 'updated_by'
  },
  relationFields: {
    slideImageIds: {
      relationName: 'slideImages',
      targetEntity: File,
      joinTableName: 'goods_slide_images'
    }
  }
});

// 聚合基础CRUD路由和扩展路由
const app = new OpenAPIHono()
  .route('/random', randomRoute)  // 随机商品列表路由
  .route('/', goodsRoutes)          // 基础CRUD路由

export default app;