import { initializeContainers } from '../lib/cosmos-init.js';
import { runMigrations, rollbackMigration } from '../lib/migrations.js';
import migrations from '../migrations/index.js';

async function main() {
  const command = process.argv[2];
  const migrationName = process.argv[3];

  await initializeContainers();

  if (command === 'up') {
    console.log('Running migrations...');
    await runMigrations(migrations);
    console.log('Migrations completed successfully');
  } else if (command === 'down' && migrationName) {
    console.log(`Rolling back migration: ${migrationName}`);
    await rollbackMigration(migrationName, migrations);
    console.log('Rollback completed successfully');
  } else {
    console.log('Usage: tsx src/scripts/run-migrations.ts [up|down] [migration-name]');
    console.log('  up              - Run all pending migrations');
    console.log('  down <name>     - Rollback a specific migration');
    process.exit(1);
  }
}

main().catch((error) => {
  console.error('Migration failed:', error);
  process.exit(1);
});
