#!/usr/bin/env node
// CLI wrapper: `node scripts/migrate.js [latest|rollback|status|dry-run]`
// Used by the Docker entrypoint and by operators running migrations by hand.

const { runMigrations, rollbackMigrations, migrationStatus } = require('../src/database/migrations/runner');

const cmd = process.argv[2] || 'latest';
const dryRun = process.argv.includes('--dry-run');

(async () => {
  try {
    if (cmd === 'latest') {
      if (dryRun) {
        console.log('[migrate] DRY RUN — not actually applying anything');
        await migrationStatus();
      } else {
        await runMigrations();
      }
    } else if (cmd === 'rollback') {
      await rollbackMigrations();
    } else if (cmd === 'status') {
      await migrationStatus();
    } else {
      console.error(`Unknown command: ${cmd}. Use: latest | rollback | status [--dry-run]`);
      process.exit(1);
    }
  } catch (err) {
    console.error('[migrate] failed:', err.message);
    process.exit(1);
  }
})();
