import { Hono } from 'hono';
import { createCrudRoutes } from '@d8d/shared-crud';
import { Goods } from '../entities/goods.entity.js';
import { PublicGoodsSchema, PublicGoodsQueryDto } from '../schemas/public-goods.schema.js';
import { File } from '@d8d/file-module';

// 创建公开商品路由 - 只读查询，无需认证
// 默认只返回可用状态的商品

export const publicGoodsRoutes = createCrudRoutes({
  entity: Goods,
  createSchema: undefined as any, // 禁用创建操作
  updateSchema: undefined as any, // 禁用更新操作
  getSchema: PublicGoodsSchema,
  listSchema: PublicGoodsSchema,
  searchFields: ['name', 'instructions'],
  relations: ['category1', 'category2', 'category3', 'supplier', 'merchant', 'imageFile', 'slideImages'],
  // 公开路由无需认证中间件
  middleware: [],
  // 公开路由不跟踪用户操作
  userTracking: undefined,
  relationFields: {
    slideImageIds: {
      relationName: 'slideImages',
      targetEntity: File,
      joinTableName: 'goods_slide_images'
    }
  },
  // 公开路由不使用数据权限控制
  dataPermission: undefined,
  // 设置为只读模式
  readOnly: true,
  // 默认只返回可用状态的商品
  defaultFilters: { state: 1 }
});