#!/usr/bin/env node
/** Post-build bundle sanity check for beauty module chunks. */
import { existsSync, readdirSync, statSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";

const ROOT = join(fileURLToPath(new URL(".", import.meta.url)), "..");
const NEXT_APP = join(ROOT, ".next", "server", "app", "beauty");

console.log("Beauty Bundle Validation\n");

if (!existsSync(join(ROOT, ".next"))) {
  console.error("❌ Run `npm run build` first — .next directory not found");
  process.exit(1);
}

if (!existsSync(NEXT_APP)) {
  console.error("❌ No compiled beauty routes under .next/server/app/beauty");
  process.exit(1);
}

function countRoutes(dir) {
  let count = 0;
  for (const entry of readdirSync(dir)) {
    const full = join(dir, entry);
    if (statSync(full).isDirectory()) count += countRoutes(full);
    else if (entry === "page.js" || entry === "page.js.nft.json") count++;
  }
  return count;
}

const routeCount = countRoutes(NEXT_APP);
console.log(`✅ Compiled beauty route segments: ${routeCount}`);

if (routeCount < 50) {
  console.error(`❌ Expected at least 50 compiled beauty pages, got ${routeCount}`);
  process.exit(1);
}

console.log("✅ Beauty module bundled successfully");
process.exit(0);
