#!/usr/bin/env node
/**
 * Bulk-update Beauty module import paths after migration to modules/beauty.
 */
import { readFileSync, writeFileSync, readdirSync, statSync } from "node:fs";
import { join, relative } from "node:path";
import { fileURLToPath } from "node:url";

const ROOT = join(fileURLToPath(new URL(".", import.meta.url)), "..");
const EXT = new Set([".ts", ".tsx"]);
const SKIP = new Set(["node_modules", ".next"]);

const REPLACEMENTS = [
  ["@/components/beauty/design-system", "@/modules/beauty/design-system"],
  ["@/components/beauty/pages/", "@/modules/beauty/pages/"],
  ["@/components/beauty/BeautyPortalShell", "@/modules/beauty/components/BeautyPortalShell"],
  ["@/components/beauty/PublicBeautyLayout", "@/modules/beauty/components/PublicBeautyLayout"],
  ["@/components/beauty/createPortalLayout", "@/modules/beauty/components/createPortalLayout"],
  ["@/components/beauty/BeautyBusinessShell", "@/modules/beauty/components/BeautyBusinessShell"],
  ["@/lib/beauty-business-api", "@/modules/beauty/services/beauty-business-api"],
  ["@/lib/beauty-portals", "@/modules/beauty/constants/portals"],
  ["@/hooks/useBeautyLookups", "@/modules/beauty/hooks/useBeautyLookups"],
];

function walk(dir, files = []) {
  for (const entry of readdirSync(dir)) {
    if (SKIP.has(entry)) continue;
    const full = join(dir, entry);
    if (statSync(full).isDirectory()) walk(full, files);
    else if (EXT.has(entry.slice(entry.lastIndexOf(".")))) files.push(full);
  }
  return files;
}

let changed = 0;
for (const file of walk(ROOT)) {
  const rel = relative(ROOT, file);
  if (rel.startsWith("scripts/migrate-beauty-imports.mjs")) continue;
  let content = readFileSync(file, "utf8");
  const original = content;
  for (const [from, to] of REPLACEMENTS) {
    content = content.split(from).join(to);
  }
  if (content !== original) {
    writeFileSync(file, content, "utf8");
    changed++;
  }
}

console.log(`Updated imports in ${changed} files.`);
