/**
 * Fallback body generators for wiki pages.
 *
 * These produce readable prose (not raw data dumps) when the AI-powered
 * narrative generation fails or is unavailable.
 */

import type { ModuleEvidence, ModuleNode, RootEvidence } from "../types";
import { humanTitle } from "../utils";

export function buildModuleFallbackBody(
  module: ModuleNode,
  evidence: ModuleEvidence,
  agentError: string | null,
): string {
  const title = humanTitle(module.path || module.name);
  const lines: string[] = [];

  lines.push(`# ${title}`);
  lines.push("");
  lines.push(
    `The \`${module.path || module.name}\` module contains ${evidence.fileCount} source files ` +
      `with ${evidence.classCount} classes/interfaces and ${evidence.methodCount} methods.`,
  );

  if (module.children.length > 0) {
    lines.push("");
    lines.push(
      `This module is organized into ${module.children.length} sub-modules: ` +
        `${module.children.map((c) => `**${c.name}**`).join(", ")}.`,
    );
  }

  lines.push("");
  lines.push("## Key Classes");
  lines.push("");

  const classLines = evidence.classReferences.split("\n").slice(0, 12);
  if (
    classLines.length > 0 &&
    !classLines[0].includes("No class-level symbols")
  ) {
    for (const line of classLines) {
      lines.push(line);
    }
  } else {
    lines.push(
      "No classes were indexed for this module. See child modules for details.",
    );
  }

  const depLines = evidence.dependencySummary.split("\n");
  if (depLines.length > 0 && !depLines[0].includes("No package dependencies")) {
    lines.push("");
    lines.push("## Dependencies");
    lines.push("");
    lines.push(
      `This module depends on the following packages:`,
    );
    for (const line of depLines) {
      lines.push(line);
    }
  }

  if (agentError) {
    lines.push("");
    lines.push(
      "> **Note:** AI-powered narrative generation encountered an error, so this page shows " +
        "deterministic content extracted from the code index. The error was: " +
        agentError,
    );
  }

  return lines.join("\n");
}

export function buildRootFallbackBody(
  evidence: RootEvidence,
  agentError: string | null,
): string {
  const lines: string[] = [];

  lines.push("# Repository Overview");
  lines.push("");
  lines.push(
    "This documentation was automatically generated from static code analysis. " +
      "It provides an overview of the repository structure, modules, and technology stack.",
  );
  lines.push("");
  lines.push("## Repository at a Glance");
  lines.push("");
  for (const line of evidence.repoStats.split("\n")) {
    lines.push(line);
  }

  lines.push("");
  lines.push("## Technology Stack");
  lines.push("");
  for (const line of evidence.technologyStack.split("\n")) {
    lines.push(line);
  }

  lines.push("");
  lines.push("## Main Modules");
  lines.push("");
  for (const line of evidence.topLevelSummary.split("\n")) {
    lines.push(line);
  }

  if (agentError) {
    lines.push("");
    lines.push(
      "> **Note:** AI-powered narrative generation encountered an error, so this page shows " +
        "deterministic content from the code index. The error was: " +
        agentError,
    );
  }

  return lines.join("\n");
}
