import { OpenAPIHono } from '@hono/zod-openapi';
import { createCrudRoutes } from '@d8d/shared-crud';
import { DeliveryAddressMt } from '../entities';
import {
  AdminDeliveryAddressSchema,
  CreateAdminDeliveryAddressDto,
  UpdateAdminDeliveryAddressDto
} from '../schemas/admin-delivery-address.mt.schema';
import adminCustomRoutes from './admin-custom.routes.mt';
import { authMiddleware } from '@d8d/auth-module-mt';

// 创建通用CRUD路由配置（只读模式）
const adminCrudRoutes = createCrudRoutes({
  entity: DeliveryAddressMt,
  createSchema: CreateAdminDeliveryAddressDto,
  updateSchema: UpdateAdminDeliveryAddressDto,
  getSchema: AdminDeliveryAddressSchema,
  listSchema: AdminDeliveryAddressSchema,
  searchFields: ['name', 'phone', 'address'],
  relations: ['user', 'province', 'city', 'district', 'town'],
  middleware: [authMiddleware],
  userTracking: {
    createdByField: 'createdBy',
    updatedByField: 'updatedBy'
  },
  tenantOptions: {
    enabled: true,
    tenantIdField: 'tenantId'
  },
  readOnly: true // 创建/更新/删除使用自定义路由
  // 注意：管理员路由不配置 dataPermission，保持完整CRUD功能
});

// 创建混合路由应用
const app = new OpenAPIHono()
  .route('/', adminCustomRoutes)      // 管理员自定义业务路由（创建/更新/删除，包含地区验证）
  .route('/', adminCrudRoutes);  // 通用CRUD路由（列表查询和获取详情）

export default app;