import { createCrudRoutes } from '@d8d/shared-crud';
import { OrderMt } from '../../entities/order.mt.entity';
import { OrderSchema, OrderListSchema } from '../../schemas/order.mt.schema';
import { UserCreateOrderDto, UserUpdateOrderDto } from '../../schemas/user-order.mt.schema';
import { authMiddleware } from '@d8d/auth-module-mt';
import { OpenAPIHono } from '@hono/zod-openapi';
import createOrderRoutes from './create-order.mt';
import cancelOrderRoutes from './cancel-order.mt';

// 多租户用户订单路由 - 有数据权限限制，只能访问自己的订单
const userOrderCrudRoutes = createCrudRoutes({
  entity: OrderMt,
  createSchema: UserCreateOrderDto,
  updateSchema: UserUpdateOrderDto,
  getSchema: OrderSchema,
  listSchema: OrderSchema,
  searchFields: ['orderNo', 'userPhone', 'recevierName'],
  relations: ['user', 'merchant', 'supplier', 'deliveryAddress', 'orderGoods.imageFile'],
  middleware: [authMiddleware],
  readOnly: true,
  userTracking: {
    createdByField: 'createdBy',
    updatedByField: 'updatedBy'
  },
  dataPermission: {
    enabled: true,
    userIdField: 'userId'
  },
  tenantOptions: {
    enabled: true,
    tenantIdField: 'tenantId'
  }
});
const userOrderRoutes = new OpenAPIHono()
  .route('/', createOrderRoutes)
  .route('/', cancelOrderRoutes)
  .route('/', userOrderCrudRoutes)

export default userOrderRoutes;