const fs = require('fs');
const content = fs.readFileSync('temp/rich-outline-fail_57_1779022210168.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);

// Find chapters array - need to find the correct start
const chaptersArrayStart = aiResponse.indexOf('"chapters":[');
console.log('Chapters array starts at:', chaptersArrayStart);

// Check the raw characters around position 376
console.log('\nChars around position 376:');
for (let i = 370; i < 390; i++) {
  console.log('  pos', i, ':', JSON.stringify(aiResponse[i]), 'code:', aiResponse.charCodeAt(i));
}

// Check chars around position 5000
console.log('\nChars around position 5000:');
for (let i = 4995; i < 5010; i++) {
  console.log('  pos', i, ':', JSON.stringify(aiResponse[i]), 'code:', aiResponse.charCodeAt(i));
}

// The issue is the findArrayEnd function must be finding the WRONG closing bracket
// Let's trace through it manually
console.log('\n=== Manual trace of bracket counting ===');
let bracketDepth = 0;
let inString = false;
let escapeNext = false;
let lastCloseBracket = -1;
let lastCloseBracketContext = '';

for (let i = chaptersArrayStart + 12; i < chaptersArrayStart + 500; i++) {
  const ch = aiResponse[i];
  if (escapeNext) { escapeNext = false; continue; }
  if (ch === '\\') { escapeNext = true; continue; }
  if (ch === '"') { inString = !inString; continue; }
  if (inString) continue;

  if (ch === '[') { bracketDepth++; console.log('  +bracket at', i, 'now depth', bracketDepth); }
  else if (ch === ']') {
    bracketDepth--;
    lastCloseBracket = i;
    lastCloseBracketContext = aiResponse.substring(Math.max(0, i-30), i+30);
    console.log('  -bracket at', i, 'now depth', bracketDepth, 'context:', JSON.stringify(lastCloseBracketContext));
    if (bracketDepth === 0) { console.log('  Found array end at', i); break; }
  }
}

// So the first ] when depth becomes 0 is at position 376 - but that can't be right for 65 chapters
// The issue is we're starting to count from AFTER "chapters":[
// We should start counting from the [ itself

console.log('\n=== Re-trace starting from the opening [ ===');
bracketDepth = 0;
inString = false;
escapeNext = false;

for (let i = chaptersArrayStart + 11; i < chaptersArrayStart + 500; i++) {  // Start at 11 to include the [
  const ch = aiResponse[i];
  if (escapeNext) { escapeNext = false; continue; }
  if (ch === '\\') { escapeNext = true; continue; }
  if (ch === '"') { inString = !inString; continue; }
  if (inString) continue;

  if (ch === '[') { bracketDepth++; }
  else if (ch === ']') {
    bracketDepth--;
    if (bracketDepth === 0) {
      console.log('Array ends at position', i);
      console.log('Context:', JSON.stringify(aiResponse.substring(Math.max(0, i-50), i+50)));
      break;
    }
  }
}

// The correct approach: count from the [ at "chapters":[
console.log('\n=== Correct trace (starting from opening [) ===');
bracketDepth = 0;
inString = false;
escapeNext = false;
const chaptersOpenBracket = chaptersArrayStart + 11; // Position of [ after "chapters":

for (let i = chaptersOpenBracket; 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) continue;

  if (ch === '[') bracketDepth++;
  else if (ch === ']') {
    bracketDepth--;
    if (bracketDepth === 0) {
      console.log('Array actually ends at position', i);
      console.log('This is', i - chaptersOpenBracket, 'characters from the opening [');
      console.log('Context:', JSON.stringify(aiResponse.substring(i-100, i+50)));
      break;
    }
  }
}