import { createCrudRoutes } from '@d8d/shared-crud';
import { authMiddleware } from '@d8d/auth-module';
import { Goods } from '../entities/goods.entity.js';
import { UserGoodsSchema, UserCreateGoodsDto, UserUpdateGoodsDto } from '../schemas/user-goods.schema.js';
import { File } from '@d8d/file-module';

export const userGoodsRoutes = createCrudRoutes({
  entity: Goods,
  createSchema: UserCreateGoodsDto,
  updateSchema: UserUpdateGoodsDto,
  getSchema: UserGoodsSchema,
  listSchema: UserGoodsSchema,
  searchFields: ['name', 'instructions'],
  relations: ['category1', 'category2', 'category3', 'supplier', 'imageFile.uploadUser', 'slideImages.uploadUser'],
  middleware: [authMiddleware],
  userTracking: {
    createdByField: 'createdBy',
    updatedByField: 'updatedBy'
  },
  relationFields: {
    slideImageIds: {
      relationName: 'slideImages',
      targetEntity: File,
      joinTableName: 'goods_slide_images'
    }
  },
  dataPermission: {
    enabled: true,
    userIdField: 'createdBy'
  }
});