import { type EntityMetadata, type Platform, defineConfig } from '@mikro-orm/mysql';
import { EntityGenerator } from '@mikro-orm/entity-generator';
import dotenv from 'dotenv';
import path from 'path';

// 加载 .env.development 配置文件 (从 src 目录返回根目录)
dotenv.config({ path: path.resolve(__dirname, '../.env.development') });

export default defineConfig({
  dbName: process.env.DB_NAME || 'scs',
  user: process.env.DB_USER || 'user',
  password: process.env.DB_PASSWORD || '',
  host: process.env.DB_HOST || 'localhost',
  port: Number(process.env.DB_PORT) || 3306,
  charset: 'utf8mb4',
  multipleStatements: true,
  extensions: [EntityGenerator],
  // 生成实体时的配置
  entityGenerator: {
    // 在元数据处理阶段修改类属性类型，但不影响 Schema
    onProcessedMetadata: (metadata: EntityMetadata[], _platform: Platform) => {
      metadata.forEach((meta) => {
        Object.values(meta.properties).forEach((prop) => {
          // 只修改 bigint 类型的 classType，不影响 columnType
          if (prop.columnTypes?.[0] === 'bigint') {
            // 设置运行时类型为 number
            prop.runtimeType = 'number';
          }
        });
      });
    },
  },
});
