import json, sys, io

# Use UTF-8 output
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

with open('C:/allgifted/mathapi11v2/_work_barmodels/audit_final/af_slice.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

flagged_ids = {14914,14916,14923,14926,14930,14938,14940,14951,14953,14958,14965,14966,14970,14977,14980,14986,14988,14990,14992,14998,15002,15003,15011,15014,15015,15017,15030,15031,15035,15036,15038,15042,15049,15052,15057,15058,15061,15072,15075,15076,15081,15083,15093,15094,15102,15103,15106,15109,15112}

lines = []
for entry in data:
    qid = entry['id']
    if qid not in flagged_ids:
        continue
    sol = entry.get('solution','')
    q_text = entry['question'][:200]
    ans = entry['answers']
    ci = entry['correct_index']
    t = entry['type']
    if t == 1 and ci is not None and ci < len(ans):
        exp = ans[ci]
    else:
        exp = ans[0] if ans[0] is not None else 'N/A'

    lines.append('=== QID:' + str(qid) + ' type:' + str(t) + ' ci:' + str(ci) + ' ===')
    lines.append('Expected: ' + str(exp))
    lines.append('Q: ' + q_text)
    lines.append('S: ' + sol)
    lines.append('')

with open('C:/allgifted/mathapi11v2/_work_barmodels/audit_final/flagged_review.txt', 'w', encoding='utf-8') as f:
    f.write('\n'.join(lines))
print('Wrote flagged_review.txt with ' + str(len(flagged_ids)) + ' entries')
