import { prisma } from "../db/prisma.js";
import { pruneOrphanPlaceholders } from "../modules/admin/admin-catalog.service.js";

/**
 * Deletes every placeholder `Ingredient` row (`Ingredient.isPlaceholder`)
 * that no recipe references any more — the debris left behind when a recipe
 * edit drops a placeholder line (the `RecipeIngredient` row goes, the
 * `Ingredient` row stays). The admin catalog view has a button for this
 * too; this script is the same operation for a cron / one-off cleanup:
 *
 *   pnpm --filter api exec tsx src/scripts/prune-orphan-placeholders.ts
 *
 * Delegates to `admin-catalog.service.ts` so the "what counts as an orphan"
 * rule lives in exactly one place.
 */
async function main(): Promise<void> {
  const { deleted } = await pruneOrphanPlaceholders();
  console.info(`Pruned ${deleted} orphan placeholder ingredient(s).`);
}

main()
  .then(() => prisma.$disconnect())
  .catch(async (err) => {
    console.error(err);
    await prisma.$disconnect();
    process.exit(1);
  });
