// src/commands/graph/scan.ts
import { Command } from "commander";
import * as path from "path";
async function runScan(projectPath) {
  const { GraphStore, CodeIngestor, TopologicalLinker, KnowledgeIngestor, GitIngestor } = await import("./dist-PGQ32ZI7.js");
  const store = new GraphStore();
  const start = Date.now();
  await new CodeIngestor(store).ingest(projectPath);
  new TopologicalLinker(store).link();
  const knowledgeIngestor = new KnowledgeIngestor(store);
  await knowledgeIngestor.ingestAll(projectPath);
  try {
    await new GitIngestor(store).ingest(projectPath);
  } catch {
  }
  const graphDir = path.join(projectPath, ".harness", "graph");
  await store.save(graphDir);
  return { nodeCount: store.nodeCount, edgeCount: store.edgeCount, durationMs: Date.now() - start };
}
function createScanCommand() {
  return new Command("scan").description("Scan project and build knowledge graph").argument("[path]", "Project root path", ".").action(async (inputPath, _opts, cmd) => {
    const projectPath = path.resolve(inputPath);
    const globalOpts = cmd.optsWithGlobals();
    try {
      const result = await runScan(projectPath);
      if (globalOpts.json) {
        console.log(JSON.stringify(result));
      } else {
        console.log(
          `Graph built: ${result.nodeCount} nodes, ${result.edgeCount} edges (${result.durationMs}ms)`
        );
      }
    } catch (err) {
      console.error("Scan failed:", err instanceof Error ? err.message : err);
      process.exit(2);
    }
  });
}

export {
  runScan,
  createScanCommand
};
