import { createCrudRoutes } from '@d8d/shared-crud';
import { DeliveryAddress } from '../entities';
import {
  UserDeliveryAddressSchema,
  CreateUserDeliveryAddressDto,
  UpdateUserDeliveryAddressDto
} from '../schemas/user-delivery-address.schema';
import { authMiddleware } from '@d8d/auth-module';

// 用户专用路由 - 仅限当前用户使用
const userDeliveryAddressRoutes = createCrudRoutes({
  entity: DeliveryAddress,
  createSchema: CreateUserDeliveryAddressDto,
  updateSchema: UpdateUserDeliveryAddressDto,
  getSchema: UserDeliveryAddressSchema,
  listSchema: UserDeliveryAddressSchema,
  searchFields: ['name', 'phone', 'address'],
  relations: ['user', 'province', 'city', 'district', 'town'],
  middleware: [authMiddleware],
  userTracking: {
    createdByField: 'createdBy',
    updatedByField: 'updatedBy'
  },
  // 配置数据权限控制，确保用户只能操作自己的数据
  dataPermission: {
    enabled: true,
    userIdField: 'userId',
    // 自动注入当前登录用户ID到查询条件
    // 用户只能访问和操作自己创建的配送地址
  }
});

export default userDeliveryAddressRoutes;