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);

// Check for the EXACT patterns my fix targets
const pattern1 = '{number":';
const pattern2 = '{"number":';
console.log('Pattern "{number\\":":', aiResponse.includes('{number":'));
console.log('Pattern "{\\"number\\":":', aiResponse.includes('{"number":'));

// Let's see what character sequence is at position 1624
console.log('\nExact chars at position 1624:');
for (let i = 1624; i < 1635; i++) {
  console.log('  pos', i, ': U+' + aiResponse.charCodeAt(i).toString(16).padStart(4, '0'), JSON.stringify(aiResponse[i]));
}

// Check if position 1624 actually starts with { followed by "
console.log('\nAt 1624, char is "{":', aiResponse[1624] === '{');
console.log('At 1625, char is "\"":', aiResponse[1625] === '"');
console.log('At 1626, char is "n":', aiResponse[1626] === 'n');

// So it's },{\"number\":3 at positions 1620-1634
// 1620: " (closing quote of mustNotRepeat array)
// 1621: ] (closing bracket of mustNotRepeat array)
// 1622: } (closing brace of writingInstructions)
// 1623: , (comma separator)
// 1624: { (OPENING NEW OBJECT)
// 1625: " (opening quote of property name)
// 1626: n, 1627: u, 1628: m, 1629: b, 1630: e, 1631: r (number)
// 1632: " (closing quote)
// 1633: : (colon)
// 1634: 3 (value)

// So the JSON is CORRECT! The issue must be something else.
// The error message says "Expected double-quoted property name" at position 1624
// But at position 1624, we have "{" which is NOT a property name!

// Wait - maybe the issue is that my fix function is NOT being applied to the right string!
// Let me check - the function fixMissingQuotes takes a JSON string and applies replacements
// But the issue is the fix does .replace(/\{number":/g, '{"number":')
// This replaces "{number": with "{\"number\":"
// Let me verify this replacement works on the actual string

const test1 = '{\"number":3}';
const fixed1 = test1.replace(/\{number":/g, '{"number":');
console.log('\nFix test:');
console.log('Input:', JSON.stringify(test1));
console.log('Output:', JSON.stringify(fixed1));

// The actual string at 1624 is NOT {number": but just {"
// Let me trace what my fix would do
// My fix: .replace(/\{number":/g, '{"number":')
// This replaces the LITERAL string "{number":" with '{"number":'

// But in the actual JSON, the sequence is:
// ...]},{"number":3,"title":"
//          ^-- at 1620
// So at 1624 we have { followed by "number":
// The string literal in my regex is: "{number":
// Let's verify this is present in the JSON
const idx = aiResponse.indexOf('{number":');
console.log('\nIndex of "{number\\":":', idx);

// Also check what's actually before position 1624
console.log('\nChar before position 1624 (at 1623):', JSON.stringify(aiResponse[1623]), 'code:', aiResponse.charCodeAt(1623));

// I think the issue might be that I'm looking at the WRONG error
// Let me re-examine - the JSON has 39 more opening braces than closing braces
// This means the JSON is TRUNCATED - some objects were never closed
// And my fix function is working correctly, but the JSON is still truncated

// Let me check: can I find where the JSON SHOULD end, and try to fix it there?
console.log('\n=== Checking for truncation point ===');
console.log('Expected closing braces:', 101 - 39); // 62 should be the target
console.log('Actual closing braces:', 62);

// Find where the JSON stops making sense
let inString = false, escapeNext = false;
let braceLevel = 0;
let lastValidPos = 0;

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) continue;
  if (ch === '{') braceLevel++;
  else if (ch === '}') braceLevel--;

  // When braceLevel goes below 0, that's where we have extra closing
  if (braceLevel < 0) {
    console.log('Brace level goes negative at position', i, '- char:', JSON.stringify(ch));
    break;
  }

  // Track last position where we had balanced braces at top level
  if (braceLevel === 0 && ch === '}') {
    // Try parsing
    try {
      const sub = aiResponse.substring(0, i + 1);
      JSON.parse(sub);
      lastValidPos = i + 1;
    } catch(e) {}
  }
}

console.log('Last valid parse position:', lastValidPos);
if (lastValidPos > 0) {
  console.log('JSON is truncated at position', lastValidPos, 'of', aiResponse.length);
  console.log('We have', lastValidPos, 'valid chars but need', aiResponse.length);
}