// Developer-only scripts moved out of infrastructure: DOM exporter for local HTML dumps.
// NOT for production automation; intended as a developer utility to generate compact DOM exports
// for manual inspection and to aid writing Playwright automations.

import { chromium } from "playwright";
import { promises as fs } from "fs";
import path from "path";
import { extractDom, ExportedElement } from "./domExtractor";

const INPUT_DIR = path.join(process.cwd(), "html-dumps");
const OUTPUT_DIR = path.join(process.cwd(), "html-dumps-optimized");

async function ensureDir(dir: string): Promise<void> {
  try {
    await fs.mkdir(dir, { recursive: true });
  } catch {
    // ignore
  }
}

async function exportAll(): Promise<void> {
  await ensureDir(OUTPUT_DIR);

  async function collectHtmlFiles(dir: string): Promise<string[]> {
    const entries = await fs.readdir(dir, { withFileTypes: true });
    const results: string[] = [];
    for (const ent of entries) {
      const p = path.join(dir, ent.name);
      if (ent.isDirectory()) {
        results.push(...(await collectHtmlFiles(p)));
      } else if (ent.isFile() && ent.name.endsWith(".html")) {
        results.push(path.relative(INPUT_DIR, p));
      }
    }
    return results;
  }

  let htmlFiles: string[] = [];
  try {
    htmlFiles = await collectHtmlFiles(INPUT_DIR);
  } catch (err) {
    console.error(
      "Could not read input directory recursively:",
      INPUT_DIR,
      err
    );
    process.exit(1);
    return;
  }

  if (htmlFiles.length === 0) {
    console.log("No .html files found in", INPUT_DIR);
    return;
  }

  const browser = await chromium.launch({ headless: true });
  try {
    for (const file of htmlFiles) {
      const abs = path.join(INPUT_DIR, file);
      const url = "file://" + abs;
      const page = await browser.newPage();
      try {
        await page.addInitScript({
          content: "window.__name = window.__name || (fn => fn);",
        });
        await page.goto(url, {
          waitUntil: "domcontentloaded",
          timeout: 10000,
        });
        const items = (await page.evaluate(
          extractDom as () => ExportedElement[]
        )) as unknown as ExportedElement[];
        const outPath = path.join(
          OUTPUT_DIR,
          file.replace(/\.html$/, ".json")
        );
        await fs.mkdir(path.dirname(outPath), { recursive: true });
        await fs.writeFile(
          outPath,
          JSON.stringify(items, null, 2),
          "utf8"
        );
        console.log(
          "exported " +
            file +
            " -> " +
            path.relative(process.cwd(), outPath) +
            " (elements: " +
            (Array.isArray(items) ? items.length : 0) +
            ")"
        );
      } catch (e) {
        console.error("Failed processing", file, e);
      } finally {
        await page.close();
      }
    }
  } finally {
    await browser.close();
  }
}

if (typeof require !== "undefined" && require.main === module) {
  exportAll().catch((err) => {
    console.error(err);
    process.exit(1);
  });
}
