/**
 * Eager tenant migration script.
 * Run: pnpm --filter @horux/api db:migrate-tenants
 * Or:  pnpm db:migrate-tenants (from monorepo root via Turborepo)
 *
 * Applies pending SQL migrations to all active tenant databases.
 */
import { migrateAll } from '../src/config/tenant-migrations.js';

async function main() {
  console.log('=== Tenant Schema Migration (Eager) ===\n');

  const start = Date.now();
  const result = await migrateAll();
  const elapsed = ((Date.now() - start) / 1000).toFixed(1);

  console.log(`\n=== Done in ${elapsed}s ===`);
  console.log(`  Migrated: ${result.success}`);
  console.log(`  Up-to-date: ${result.skipped}`);
  console.log(`  Failed: ${result.failed}`);

  if (result.failed > 0) {
    console.error('\nSome tenants failed migration. Check logs above.');
    process.exit(1);
  }

  process.exit(0);
}

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