const fs = require('fs');
const content = fs.readFileSync('temp/rich-outline-fail_57_1779015509994.txt', 'utf-8');
const marker = '========== AI 原始响应 ';
const markerIdx = content.indexOf(marker);
const headerEndIdx = content.indexOf(' ==========', markerIdx);
const aiStart = content.indexOf('\n', headerEndIdx) + 1;
const aiEnd = content.indexOf('\n\n', aiStart);
const aiResponse = content.substring(aiStart, aiEnd);

console.log('AI response length:', aiResponse.length);

// The error is at position 1624 which is the "{" in "{\"number\":3"
// JSON says: Expected double-quoted property name at position 1624
// This means when we hit this "{", we should be seeing a property name, but we're not
// This suggests we might be INSIDE a string when we encounter this "{"

// Let's trace back from position 1624 to find where the string state changes
let inString = false;
let escapeNext = false;
let lastNonSpaceBefore = -1;

for (let i = 0; i < 1624; i++) {
  const ch = aiResponse[i];
  if (escapeNext) {
    escapeNext = false;
    lastNonSpaceBefore = i;
    continue;
  }
  if (ch === '\\') {
    escapeNext = true;
    lastNonSpaceBefore = i;
    continue;
  }
  if (ch === '"') {
    inString = !inString;
    lastNonSpaceBefore = i;
    continue;
  }
  if (!inString && ch !== ' ' && ch !== '\n' && ch !== '\r' && ch !== '\t') {
    lastNonSpaceBefore = i;
  }
}

console.log('At position 1624: inString =', inString);
console.log('Last non-space char before 1624: pos', lastNonSpaceBefore, '=', JSON.stringify(aiResponse[lastNonSpaceBefore]));

// Find the nearest opening quote before position 1624 that might be the start of the problematic string
// Look at the last 200 chars before position 1624
console.log('\nLast 200 chars before position 1624:');
console.log(JSON.stringify(aiResponse.substring(1424, 1624)));

// Check for unescaped newlines or special chars in strings
let hasProblem = false;
for (let i = 0; i < 1624; i++) {
  const code = aiResponse.charCodeAt(i);
  if (code < 32 && code !== 9 && code !== 10 && code !== 13) {
    console.log('Problematic char at', i, ': code', code, 'char', JSON.stringify(aiResponse[i]));
    hasProblem = true;
  }
}
if (!hasProblem) {
  console.log('No control characters found before position 1624');
}

// Maybe it's the "numbered" property names issue again but at a nested level
// Let's check all occurrences of problematic patterns
const problemPatterns = [
  [',{number":', 'comma opening brace number colon quote'],
  [',{title":', 'comma opening brace title colon quote'],
  [',{summary":', 'comma opening brace summary colon quote'],
  [',{"number":', 'comma opening brace QUOTE number colon quote (correct)'],
];

for (const [pattern, desc] of problemPatterns) {
  let count = 0;
  let idx = aiResponse.indexOf(pattern);
  while (idx !== -1 && idx < 1624) { count++; idx = aiResponse.indexOf(pattern, idx + 1); }
  if (count > 0) console.log(pattern, '->', count, 'times (before pos 1624)');
}

// Let's try a different approach - find all "},{" occurrences and see if any are malformed
const badCommaBrace = /},\{"[^"]+":/g;
let badMatches = [];
let m;
while ((m = badCommaBrace.exec(aiResponse)) !== null) {
  if (m.index < 1700) {
    badMatches.push({pos: m.index, match: m[0]});
  }
}
console.log('\nAll },{" patterns near the error:');
badMatches.forEach(b => console.log('  pos', b.pos, ':', JSON.stringify(b.match.substring(0, 50))));

// Check the actual structure by counting nesting levels
let braceLevel = 0;
let bracketLevel = 0;
inString = false;
escapeNext = false;
for (let i = 0; i < aiResponse.length; i++) {
  const ch = aiResponse[i];
  if (escapeNext) { escapeNext = false; continue; }
  if (ch === '\\') { escapeNext = true; continue; }
  if (ch === '"') { inString = !inString; continue; }
  if (!inString) {
    if (ch === '{') braceLevel++;
    else if (ch === '}') braceLevel--;
    else if (ch === '[') bracketLevel++;
    else if (ch === ']') bracketLevel--;
  }
  if (i === 1624) {
    console.log('\nAt error position 1624: braceLevel =', braceLevel, 'bracketLevel =', bracketLevel, 'inString =', inString);
  }
}