import { DataSource } from 'typeorm';
import { InitialSchema1784520727860 } from './migrations/1784520727860-InitialSchema';
import { AddExamManagement1784600000000 } from './migrations/1784600000000-AddExamManagement';
import { AddRoomInspections1784680000000 } from './migrations/1784680000000-AddRoomInspections';
import { AddJinshujuMatchRules1784700000000 } from './migrations/1784700000000-AddJinshujuMatchRules';
import { AddAiChat1784780000000 } from './migrations/1784780000000-AddAiChat';
import { EnhanceAiChatForAntDesignX1784860000000 } from './migrations/1784860000000-EnhanceAiChatForAntDesignX';
import { AddA2UiForms1784870000000 } from './migrations/1784870000000-AddA2UiForms';
import { AddA2UiReviews1784880000000 } from './migrations/1784880000000-AddA2UiReviews';
import { EnlargeAiReviewSections1784900000000 } from './migrations/1784900000000-EnlargeAiReviewSections';
import { AddImportRuns1784910000000 } from './migrations/1784910000000-AddImportRuns';
import { DropAiMessageFeedback1784920000000 } from './migrations/1784920000000-DropAiMessageFeedback';
import { AddImportRunSettings1784930000000 } from './migrations/1784930000000-AddImportRunSettings';
import { config } from 'dotenv';

config();

export async function runMigrationsOnStartup(): Promise<void> {
  const ds = new DataSource({
    type: 'mysql',
    host: process.env.DB_HOST || 'localhost',
    port: Number(process.env.DB_PORT || 3306),
    username: process.env.DB_USERNAME || 'root',
    password: process.env.DB_PASSWORD || '',
    database: process.env.DB_DATABASE || 'dorm_billing',
    charset: 'utf8mb4',
    migrations: [
      InitialSchema1784520727860,
      AddExamManagement1784600000000,
      AddRoomInspections1784680000000,
      AddJinshujuMatchRules1784700000000,
      AddAiChat1784780000000,
      EnhanceAiChatForAntDesignX1784860000000,
      AddA2UiForms1784870000000,
      AddA2UiReviews1784880000000,
      EnlargeAiReviewSections1784900000000,
      AddImportRuns1784910000000,
      DropAiMessageFeedback1784920000000,
      AddImportRunSettings1784930000000,
    ],
  });

  await ds.initialize();
  await ds.runMigrations();
  await ds.destroy();
}
