const fs = require('fs');
const path = require('path');

// Try all available dumps
const bridgePath = path.join(process.env.USERPROFILE, '.gemini/antigravity/bridge');
const dumpFiles = ['dump_html_1.json', 'dump_html_2.json', 'dump_html_3.json', 'dump_html_4.json', 'dump_html_5.json', 'deep-inspect-result.json', 'deep-inspect-manual.json'];

function printTree(node, indent, maxDepth) {
  if (!node || indent > maxDepth) return;
  const tag = (node.tag || node.tagName || '?').toLowerCase();
  const clsArr = (node.cls || node.className || '').split(' ').filter(c => c.length > 0);
  const text = (node.text || node.textContent || '').substring(0, 50).replace(/[\n\r]+/g, ' ');
  const childCount = (node.children || []).length;
  let line = '  '.repeat(indent) + tag;
  if (clsArr.length > 0) line += '.' + clsArr[0];
  if (childCount) line += ' [' + childCount + ']';
  if (text && childCount === 0) line += ' = "' + text + '"';
  console.log(line);
  if (node.children) {
    for (const c of node.children) printTree(c, indent + 1, maxDepth);
  }
}

// Find the conversation area in each dump
function findConvo(node, depth) {
  if (!node || depth > 20) return null;
  const cls = node.cls || node.className || '';
  if (cls.includes('bg-agent-convo-background') || cls.includes('agent-convo')) return node;
  if (node.children) {
    for (const c of node.children) {
      const r = findConvo(c, depth + 1);
      if (r) return r;
    }
  }
  return null;
}

// Find buttons (Run, Allow, Always run, Accept)
function findButtons(node, depth, results) {
  if (!node || depth > 25) return;
  const tag = (node.tag || node.tagName || '').toLowerCase();
  const text = (node.text || node.textContent || '');
  if (tag === 'button' && /Run|Allow|Accept|Always/i.test(text) && text.length < 50) {
    results.push({ text: text.trim(), depth });
  }
  if (node.children) {
    for (const c of node.children) findButtons(c, depth + 1, results);
  }
}

// Find pre/code blocks near buttons
function findCodeBlocks(node, depth, results) {
  if (!node || depth > 25) return;
  const tag = (node.tag || node.tagName || '').toLowerCase();
  const cls = node.cls || node.className || '';
  if ((tag === 'pre' || tag === 'code') && cls.includes('font-mono')) {
    const text = (node.text || node.textContent || '').substring(0, 80);
    results.push({ tag, cls: cls.substring(0, 50), text, depth });
  }
  if (node.children) {
    for (const c of node.children) findCodeBlocks(c, depth + 1, results);
  }
}

for (const df of dumpFiles) {
  const fp = path.join(bridgePath, df);
  if (!fs.existsSync(fp)) continue;
  try {
    const dump = JSON.parse(fs.readFileSync(fp, 'utf8'));
    const root = dump.bodyTree || dump.body || dump;
    console.log('\n=== ' + df + ' ===');
    
    // Find conversation area
    const convo = findConvo(root, 0);
    if (convo) {
      console.log('>> Conversation area found, tree (depth 12):');
      printTree(convo, 0, 12);
    }
    
    // Find buttons
    const btns = [];
    findButtons(root, 0, btns);
    if (btns.length > 0) {
      console.log('>> Buttons found:');
      for (const b of btns) console.log('  d' + b.depth + ': ' + b.text);
    }
    
    // Find code blocks
    const codes = [];
    findCodeBlocks(root, 0, codes);
    if (codes.length > 0) {
      console.log('>> Code blocks (font-mono):');
      for (const c of codes) console.log('  d' + c.depth + ': <' + c.tag + '> cls=' + c.cls + ' text="' + c.text + '"');
    }
  } catch (e) {
    console.log('ERROR reading ' + df + ': ' + e.message);
  }
}
