import { ACTIVE_SQL_MIGRATION_PATHS, RETIRED_SQL_MIGRATION_PATHS } from "./sql-migration-manifest.js";
import { applyMigrationFile, withMigrationClient } from "./sql-migration-runtime.js";

async function main() {
  const args = new Set(process.argv.slice(2));
  const dryRun = args.has("--dry-run");
  const listRetired = args.has("--include-retired");

  console.log("Active SQL migrations:");
  for (const entry of ACTIVE_SQL_MIGRATION_PATHS) {
    console.log(`- ${entry.fileName}: ${entry.reason}`);
  }

  if (listRetired) {
    console.log("\nRetired SQL migrations:");
    for (const entry of RETIRED_SQL_MIGRATION_PATHS) {
      console.log(`- ${entry.fileName}: ${entry.reason}`);
    }
  }

  if (dryRun) {
    console.log("\nDry run only. No SQL was applied.");
    return;
  }

  await withMigrationClient(async (client) => {
    for (const entry of ACTIVE_SQL_MIGRATION_PATHS) {
      console.log(`Applying ${entry.fileName}`);
      await applyMigrationFile(client, entry.path);
    }
  });

  console.log("Active SQL migrations applied successfully.");
}

main().catch((err) => {
  console.error(err);
  process.exitCode = 1;
});
