import { OpenAPIHono } from '@hono/zod-openapi';
import { createCrudRoutes } from '@d8d/shared-crud';
import { UserEntityMt } from '../entities/user.entity.mt';
import { UserSchemaMt, CreateUserDtoMt, UpdateUserDtoMt } from '../schemas/user.schema.mt';
import customRoutesMt from './custom.routes.mt';
import { authMiddleware } from '@d8d/auth-module-mt';

// 创建多租户通用CRUD路由配置
const userCrudRoutesMt = createCrudRoutes({
  entity: UserEntityMt,
  createSchema: CreateUserDtoMt,
  updateSchema: UpdateUserDtoMt,
  getSchema: UserSchemaMt,
  listSchema: UserSchemaMt,
  searchFields: ['username', 'nickname', 'phone', 'email'],
  relations: ['roles', 'avatarFile'],
  middleware: [authMiddleware],
  readOnly: true, // 创建/更新/删除使用自定义路由
  tenantOptions: {
    enabled: true,
    tenantIdField: 'tenantId',
    autoExtractFromContext: true
  }
});

// 创建多租户混合路由应用
const app = new OpenAPIHono()
  .route('/', customRoutesMt)   // 多租户自定义业务路由
  .route('/', userCrudRoutesMt); // 多租户通用CRUD路由

export default app;