#!/usr/bin/env tsx
import { createDb } from "./schema.js";
import { scanTypeScript } from "./scanners/ts-scanner.js";
import { scanGo } from "./scanners/go-scanner.js";
import { scanPython } from "./scanners/py-scanner.js";
import { scanProtobuf } from "./scanners/proto-scanner.js";
import path from "node:path";

const ROOT = process.cwd();

function main(): void {
  const db = createDb(path.join(ROOT, "arch.db"));

  // 清空旧数据（避免重跑产生重复符号）
  db.exec(
    "DELETE FROM calls; DELETE FROM dependencies; DELETE FROM symbols; DELETE FROM contracts; DELETE FROM events; DELETE FROM modules;",
  );

  console.log("🔍 arch-scan: 开始多语言扫描...");

  // 串行扫描（并行会导致 FOREIGN KEY 错误）
  const tsStats = scanTypeScript(db, ROOT);
  console.log(`  TypeScript: ${tsStats.modules} 模块, ${tsStats.symbols} 符号`);

  const goStats = scanGo(db, ROOT);
  console.log(`  Go: ${goStats.modules} 模块, ${goStats.symbols} 符号`);

  const pyStats = scanPython(db, ROOT);
  console.log(`  Python: ${pyStats.modules} 模块, ${pyStats.symbols} 符号`);

  const protoStats = scanProtobuf(db, ROOT);
  console.log(`  Protobuf: ${protoStats.contracts} 契约`);

  console.log("✅ arch-scan 完成");
  db.close();
}

main();
