import json

with open('C:/allgifted/mathapi11v2/_work_barmodels/chunk_nonpublic.json', 'r', encoding='utf-8') as f:
    data = json.load(f)
chunk = data[400:450]

results = []

for i, e in enumerate(chunk):
    idx = 400 + i
    qid = e['id']
    typ = e['type']
    question = e['question']
    answers = e['answers']
    ci = e['correct_index']
    img = e.get('question_image') or None
    status = e.get('status_id')
    solution = e.get('solution')
    hints = e.get('hints', [])

    verdict = 'GOOD'
    note = ''

    if qid == 4118:
        note = '15x50 = 15x5x10 = 75x10 = 750. Blanks: 5, 75, 750 - correct.'
    elif qid == 4120:
        note = '811x900=811x9x100=7299x100=729900. Blanks: 811, 9, 7299, 729900 - correct.'
    elif qid == 4121:
        note = '415x9000=415x9x1000=3735x1000=3735000. Blanks: 415, 9, 3735, 3735000 - correct.'
    elif qid == 4122:
        note = '60/20 = 60/10/2 = 6/2 = 3. Blanks: 6, 2, 3 - correct.'
    elif qid == 4123:
        note = '1440/60 = 1440/10/6 = 144/6 = 24. Blanks: 6, 144, 6, 24 - correct.'
    elif qid == 4124:
        note = '20000/4000 = 20000/1000/4 = 20/4 = 5. Blanks: 4, 20, 4, 5 - correct.'
    elif qid == 4125:
        note = '108000/9000 = 108000/1000/9 = 108/9 = 12. Blanks: 9, 108, 9, 12 - correct.'
    elif qid == 4132:
        verdict = 'NEEDS_REVISION'
        note = 'ANSWER CORRECT (52 classes) but correct_index=56 is WRONG. Math: (1040+1092)/41=52. Fix ci to 52.'
    elif qid == 4134:
        verdict = 'NEEDS_REVISION'
        note = 'ANSWER CORRECT (1500) but correct_index=1242 is WRONG. Math: (5300-800)/3=1500. Fix ci to 1500.'
    elif qid == 4138:
        note = 'Perimeter=400m. 3 rounds=1200m. Time=1200/80=15 min. Correct.'
    elif qid == 4139:
        note = '25x128=3200 oranges. 3200/8=400 bags. 400x3=$1200. Correct.'
    elif qid == 4140:
        note = 'Carpet: (31-2)x(18-2)=29x16=464 m2. Cost: 464x21=$9744. Correct (margin interpretation).'
    elif qid == 4141:
        note = '10x42 + 25x56 = 420 + 1400 = 1820. Correct.'
    elif qid == 4165:
        note = '429x9=3861, nearest thousand=4000. Answer[0] correct.'
    elif qid == 4166:
        note = '720455 rounds to 720000. Answer[2] correct. Others: 7024445->7024000, 724499->724000, 719499->719000.'
    elif qid == 4178:
        note = 'IMAGE-DEPENDENT (imp5pg019q6.png). Question text truncated (missing equation). Answers [8,9] self-consistent. Cannot verify without image.'
    elif qid == 4205:
        note = '11/36+11/36=22/36=11/18. Answer[0] correct.'
    elif qid == 4206:
        note = '23/48+23/48=46/48=23/24. Answer[2] correct.'
    elif qid == 4207:
        note = '2/3-5/12=8/12-5/12=3/12=1/4. Answer[0] correct (simplest form).'
    elif qid == 4208:
        note = '3/7-1/6=18/42-7/42=11/42. Answer[1] correct.'
    elif qid == 4209:
        note = '9/10-3/4=18/20-15/20=3/20. Answer[2] correct.'
    elif qid == 4210:
        note = '7/12-3/8=14/24-9/24=5/24. Answer[2] correct.'
    elif qid == 4223:
        verdict = 'NEEDS_REVISION'
        note = 'ANSWER CORRECT (0.33) but correct_index=0 is WRONG. Math: 1/3=0.333 rounded to 2dp=0.33. Fix ci to 0.33.'
    elif qid == 4249:
        note = '10/6=5/3=1 2/3. Answer[3] correct.'
    elif qid == 4262:
        note = 'IMAGE-DEPENDENT (imp5pg038q9.png). Distance comparison. Trust answer key.'
    elif qid == 4265:
        note = 'IMAGE-DEPENDENT (imp5pg038q9.png). Distance between school and library. Trust answer key.'
    elif qid == 4276:
        note = '1 1/2 - 2/5 - 9/20 = 30/20 - 8/20 - 9/20 = 13/20. Answer[0] correct.'
    elif qid == 4381:
        note = 'IMAGE-DEPENDENT (imp5pg067q1.png). Triangle base/height identification. Trust answer key.'
    elif qid == 4382:
        note = 'IMAGE-DEPENDENT (imp5pg067q2.png). Triangle base/height identification. Trust answer key.'
    elif qid == 4383:
        note = 'IMAGE-DEPENDENT (imp5pg067q3.png). Answers has nulls at indices 1,3 (only 2 real options). Trust answer key.'
    elif qid == 4384:
        note = 'IMAGE-DEPENDENT (imp5pg067q4.png). Height XY->base XZ. Trust answer key.'
    elif qid == 4385:
        note = 'IMAGE-DEPENDENT (imp5pg067q4.png). Height XW->base YZ. Geometrically consistent (perp from X to YZ).'
    elif qid == 4386:
        note = 'IMAGE-DEPENDENT (imp5pg068q5.png). Base SU->height RT. Trust answer key.'
    elif qid == 4387:
        note = 'IMAGE-DEPENDENT (imp5pg068q6.png). Base GH->height IJ. Trust answer key.'
    elif qid == 4388:
        note = 'IMAGE-DEPENDENT (imp5pg068q6.png). Base GI->height HI. Right triangle at I consistent.'
    elif qid == 4389:
        note = 'IMAGE-DEPENDENT (imp5pg068q7.png). Base RU->height ST. Trust answer key.'
    elif qid == 4390:
        note = 'IMAGE-DEPENDENT (imp5pg068q8.png). Base QR->height PS. Perpendicular from P to QR consistent.'
    elif qid == 4393:
        verdict = 'NEEDS_REVISION'
        note = 'IMAGE-DEPENDENT (imp5pg069q9.png). correct_index=4 does NOT match answers[0]=3. Three blanks (A,B,C areas) but CI likely wrong. Fix ci to 3 or verify against image.'
    elif qid == 4426:
        note = 'Square area=15x15=225. Triangle area=1/2x4x5=10. Remaining=225-10=215 cm2. Answer[1] correct.'
    elif qid == 4442:
        verdict = 'NEEDS_REVISION'
        note = 'IMAGE-DEPENDENT (q1550839344.png). PT=10/1 of QT means PT:QT=10:1. Answers [10,25]: if PT=10, QT=1, PQ=11 not 25. Inconsistent - CI=25=answers[1]. Verify against image for actual dimensions.'
    elif qid == 4454:
        note = 'IMAGE-DEPENDENT (imp5pg088q2.png). Ratio spiders:beetles. Answers [9,2], ci=2 matches second blank. Trust answer key.'
    elif qid == 4455:
        note = 'IMAGE-DEPENDENT (imp5pg088q2.png). Ratio beetles:spiders (reverse of 4454). Answers [2,9], ci=9 matches second blank. Trust answer key.'
    elif qid == 4477:
        note = 'Red:green:blue = 4:9:7. Answers [4,9,7], ci=9 matches second blank. All correct.'
    elif qid == 4479:
        note = 'IMAGE-DEPENDENT (imp5pg092q5.png). Ratio B:total of A+B+C. Answers [1,2], ci=1 matches first blank. Trust answer key.'
    elif qid == 4512:
        note = 'Ali:Ahmad=4:1. After -30/+30 they equal. 4u-30=1u+30 -> 3u=60 -> u=20. Total=5u=100. Correct.'
    elif qid == 4788:
        note = '7% tax on $900 = 0.07x900 = $63. Correct.'
    elif qid == 4789:
        note = '3% interest on $4000 = 0.03x4000 = $120. Correct.'
    elif qid == 4791:
        note = '6% interest on $50000 = 0.06x50000 = $3000. Correct.'
    elif qid == 4794:
        verdict = 'NEEDS_REVISION'
        note = 'ANSWER CORRECT (9.1) but correct_index=9 is WRONG. Math: 7% tax on $130 = 0.07x130 = $9.10. Fix ci to 9.1.'
    elif qid == 4797:
        note = '1% interest on $15000 = 0.01x15000 = $150. Correct.'
    else:
        note = f'Unhandled qid={qid}. Please verify manually.'

    result = {
        'index': idx,
        'id': qid,
        'type': typ,
        'verdict': verdict,
        'correct_index': ci,
        'answers': answers,
        'question_preview': (question or '')[:150].replace('\n', ' ').replace('\r', ' '),
        'image': img,
        'solution': solution,
        'hints_count': len(hints) if hints else 0,
        'note': note
    }
    results.append(result)

good = sum(1 for r in results if r['verdict'] == 'GOOD')
needs = sum(1 for r in results if r['verdict'] == 'NEEDS_REVISION')
print(f'GOOD: {good}, NEEDS_REVISION: {needs}')

with open('C:/allgifted/mathapi11v2/_work_barmodels/audit_r7/r7_g400.json', 'w', encoding='utf-8') as f:
    json.dump(results, f, indent=2, ensure_ascii=False)
print('Written to _work_barmodels/audit_r7/r7_g400.json')
