/**
 * Prebuild script: runs before `astro build` via the `prebuild` npm script.
 *
 * 1. Generates terminal-data.ts from terminal-script.md
 * 2. Counts Swift @Test and @Suite annotations to produce project-stats.json
 *    (consumed by astro.config.mjs as environment variables)
 */

import { parseTerminalScript } from "../src/lib/terminal-parser";
import { execSync } from "child_process";
import fs from "fs";
import path from "path";

// Terminal Data

const script = parseTerminalScript();
const terminalOutputPath = path.join(process.cwd(), "src", "components", "react", "terminal-data.ts");

const tsContent = `/**
 * Auto-generated from terminal-script.md
 * DO NOT EDIT THIS FILE DIRECTLY - Edit terminal-script.md instead
 */

import type { TerminalScript } from "../../lib/terminal-parser";

export const TERMINAL_SCRIPT: TerminalScript = ${JSON.stringify(script, null, 2)};
`;

fs.writeFileSync(terminalOutputPath, tsContent);
console.log("✓ Generated terminal-data.ts from terminal-script.md");

// Project Stats

const projectRoot = path.resolve(process.cwd(), "..");
const testsDir = path.join(projectRoot, "Tests");

/**
 * Count occurrences of a pattern in all .swift files under a directory.
 * Falls back to 0 if the directory doesn't exist (e.g. CI without Swift source).
 */
function countPattern(directory: string, pattern: string): number {
  if (!fs.existsSync(directory)) return 0;
  try {
    const output = execSync(
      `grep -r '${pattern}' --include='*.swift' "${directory}" | wc -l`,
      { encoding: "utf-8" }
    );
    return parseInt(output.trim(), 10) || 0;
  } catch {
    return 0;
  }
}

const testCount = countPattern(testsDir, "@Test\\b");
const suiteCount = countPattern(testsDir, "@Suite\\b");

const statsPath = path.join(process.cwd(), "project-stats.json");
fs.writeFileSync(statsPath, JSON.stringify({ testCount, suiteCount }, null, 2));
console.log(`✓ Generated project-stats.json (${testCount} tests, ${suiteCount} suites)`);
