const {generateEntityDoc} = require('./docgen.js');
const GraphStore = require('./graph.js');
const fs = require('fs');
const g = GraphStore.loadSnapshot('snapshots/openclaw-full.json');

async function run() {
  const targets = [
    {id: 'gateway/session-utils.ts', file: '/app/src/gateway/session-utils.ts'},
    {id: 'pairing/pairing-store.ts', file: '/app/src/pairing/pairing-store.ts'},
    {id: 'infra/state-migrations.ts', file: '/app/src/infra/state-migrations.ts'},
  ];

  for (const t of targets) {
    const entity = g.nodes.get(t.id);
    if (!entity) continue;

    let source = '';
    try { source = fs.readFileSync(t.file, 'utf8'); } catch {}

    const funcs = [];
    const fileIds = g.fileIndex.get(t.file);
    if (fileIds) {
      for (const fid of fileIds) {
        const fe = g.nodes.get(fid);
        if (fe && fe.type === 'Function' && fe.visibility === 'public') {
          funcs.push(fe.name);
        }
      }
    }

    const imports = g.edges.filter(e => e.type === 'IMPORTS' && e.source === t.id).map(e => e.target.replace('dep:', ''));

    console.log(`\n### Module: \`${t.id}\``);
    try {
      // Use Kiro backend instead of local Ollama for faster/more reliable generation
      process.env.LLM_BACKEND = 'openai';
      const doc = await generateEntityDoc(t.id, g, source);
      console.log(doc);
    } catch (e) {
      console.log('*Doc generation failed:* ' + e.message);
    }
    
    if (funcs.length > 0) console.log(`\n**Public Exports:** \`${funcs.slice(0, 8).join('`, `')}${funcs.length > 8 ? '` (+' + (funcs.length-8) + ' more)' : '`'}`);
    if (imports.length > 0) console.log(`**Key Dependencies:** \`${imports.slice(0, 6).join('`, `')}${imports.length > 6 ? '` (+' + (imports.length-6) + ' more)' : '`'}`);
  }
}
run();
