import { OpenAPIHono } from '@hono/zod-openapi';
import { createCrudRoutes } from '@d8d/shared-crud';
import { SystemConfigMt } from '../entities/system-config.entity.mt';
import { SystemConfigSchema, CreateSystemConfigSchema, UpdateSystemConfigSchema } from '../schemas/system-config.schema.mt';
import { authMiddleware } from '../../../auth-module-mt/src/middleware/index.mt';
import createSystemConfigRoutes from './custom/create-system-config.mt';
import updateSystemConfigRoutes from './custom/update-system-config.mt';
import deleteSystemConfigRoutes from './custom/delete-system-config.mt';

const systemConfigCrudRoutes = createCrudRoutes({
  entity: SystemConfigMt,
  createSchema: CreateSystemConfigSchema,
  updateSchema: UpdateSystemConfigSchema,
  getSchema: SystemConfigSchema,
  listSchema: SystemConfigSchema,
  searchFields: ['configKey', 'description'],
  middleware: [authMiddleware],
  readOnly: true, // 设为只读，创建、更新、删除操作通过自定义路由处理
  tenantOptions: {
    enabled: true,
    tenantIdField: 'tenantId',
    autoExtractFromContext: true
  }
});

// 创建路由实例 - 聚合自定义路由和CRUD路由
const systemConfigRoutesMt = new OpenAPIHono()
  .route('/', createSystemConfigRoutes)
  .route('/', updateSystemConfigRoutes)
  .route('/', deleteSystemConfigRoutes)
  .route('/', systemConfigCrudRoutes);

export { systemConfigRoutesMt };
export default systemConfigRoutesMt;