const db = require("./config/database");

async function updatePageIdsToReadable() {
  try {
    console.log("Updating page IDs to readable format...\n");

    // Get all pages
    const pages = await db.query("SELECT id, slug FROM pages");

    for (const page of pages.rows) {
      const newId = `page-${page.slug}`;

      if (page.id !== newId) {
        console.log(`Updating: ${page.slug}`);
        console.log(`  Old ID: ${page.id}`);
        console.log(`  New ID: ${newId}`);

        await db.query("UPDATE pages SET id = $1 WHERE slug = $2", [
          newId,
          page.slug,
        ]);
      }
    }

    console.log("\n✅ All page IDs updated to readable format!");
    console.log("\nVerifying updates...");

    const updated = await db.query(
      "SELECT id, slug, title FROM pages ORDER BY slug"
    );
    console.log("\nCurrent page IDs:");
    updated.rows.forEach((p) => {
      console.log(`  ${p.id.padEnd(25)} → ${p.title}`);
    });

    process.exit(0);
  } catch (error) {
    console.error("Error updating page IDs:", error);
    process.exit(1);
  }
}

updatePageIdsToReadable();
