import json

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

items_raw = data[1088:1152]

items_out = []
good_count = 0
needs_revision_count = 0

for i, item in enumerate(items_raw):
    idx = 1088 + i
    qid = item['id']
    qtype = item['type']
    img = item.get('question_image', '')
    question = item.get('question', '')
    answers = item.get('answers', [])
    correct_index = item.get('correct_index')
    solution = item.get('solution')
    hints = item.get('hints', [])

    verdict = 'GOOD'
    notes = ''

    # --- Analysis per item ---

    if idx == 1088:  # id=4007
        verdict = 'NEEDS_REVISION'
        notes = 'correct_index=945 does not match answer="94"; stray digit "5". CI should be 94.'

    elif idx == 1089:  # id=4008
        notes = 'Perimeter FIB. ci=76 matches answer. Answer requires figure to verify.'

    elif idx == 1090:  # id=4013
        notes = 'Compound figure area FIB. ci=208 matches answer. Answer requires figure to verify.'

    elif idx == 1091:  # id=4014
        notes = 'Compound figure area FIB. ci=260 matches answer. Answer requires figure to verify.'

    elif idx == 1092:  # id=4019
        verdict = 'NEEDS_REVISION'
        notes = 'correct_index=3 does not match answer="70". CI should be 70 (perimeter value). Same figure as id=4020.'

    elif idx == 1093:  # id=4020
        notes = 'Area FIB. ci=0 (fallback convention, unlike sibling questions using ci=answer_value). answers[0]="216". Same figure as id=4019.'

    elif idx == 1094:  # id=4025
        notes = 'Path area around square field. ci=96 matches answer. Answer requires figure to verify.'

    elif idx == 1095:  # id=4026
        notes = 'Path area around rectangular field (1.5m wide). ci=123 matches answer. Answer requires figure to verify.'

    elif idx == 1096:  # id=4027
        notes = 'Remaining cardboard after cutting square from 25cm square. ci=336 matches. 25^2=625, inner=289=17^2, 625-289=336. Plausible.'

    elif idx == 1097:  # id=4028
        notes = 'Towel border 100x70 with 6cm border. 100*70 - (100-12)*(70-12) = 7000 - 88*58 = 7000 - 5104 = 1896. CORRECT.'

    elif idx == 1098:  # id=4029
        notes = 'Remaining pool area after cordon. ci=700 matches answer. Answer requires figure to verify cordon dimensions.'

    elif idx == 1099:  # id=4030
        notes = 'Remaining paper after cutting 7cm square corner. ci=179 matches answer. Answer requires figure for original dimensions.'

    elif idx == 1100:  # id=4031
        notes = 'Remaining paper from 30cm square after cutting rectangle corner. 30^2=900, 900-198=702. Plausible (cutout=198).'

    elif idx == 1101:  # id=4032
        notes = 'Remaining field area 80x65 after cementing portion. 80*65=5200, 5200-cemented=3920, cemented=1280. Plausible.'

    elif idx == 1102:  # id=4033
        notes = 'Perimeter of remaining field (same figure as id=4032). ci=290 matches answer. Answer requires figure.'

    elif idx == 1103:  # id=4034
        notes = 'Area of square paper after corner fold. ci=121 matches. If square, side=11. Answer requires figure.'

    elif idx == 1104:  # id=4035
        notes = 'Area of rectangular paper after corner fold. ci=126 matches answer. Answer requires figure.'

    elif idx == 1105:  # id=4036
        notes = 'Area of entire sheet folded along side. ci=320 matches answer. Answer requires figure.'

    elif idx == 1106:  # id=4037
        verdict = 'NEEDS_REVISION'
        notes = 'correct_index=327 does not match answer="324". Off by 3. Square paper folded in half. Verify which value is correct from answer key.'

    elif idx == 1107:  # id=4048 type=1
        notes = 'MCQ: Is letter F symmetric? ci=1 (No). F has no line of symmetry. CORRECT.'

    elif idx == 1108:  # id=4049 type=1
        notes = 'MCQ: Is letter X symmetric? ci=0 (Yes). X has 4 lines of symmetry. CORRECT.'

    elif idx == 1109:  # id=4050 type=1
        notes = 'MCQ: Is letter H symmetric? ci=0 (Yes). H has 2 lines of symmetry. CORRECT.'

    elif idx == 1110:  # id=4051 type=1
        notes = 'MCQ: Is letter Q symmetric? ci=1 (No). Q tail breaks symmetry. CORRECT.'

    elif idx == 1111:  # id=4052 type=1
        notes = 'MCQ: Is letter G symmetric? ci=1 (No). G has no line of symmetry. CORRECT.'

    elif idx == 1112:  # id=4053 type=1
        notes = 'MCQ: Is letter M symmetric? ci=0 (Yes). M has 1 vertical line of symmetry. CORRECT.'

    elif idx == 1113:  # id=4054 type=1
        notes = 'MCQ: Is letter S symmetric? ci=1 (No). S has no line of symmetry. CORRECT.'

    elif idx == 1114:  # id=4055 type=1
        notes = 'MCQ: Is letter N symmetric? ci=1 (No). N has no line of symmetry. CORRECT.'

    elif idx == 1115:  # id=4056 type=1
        notes = 'MCQ: Is figure symmetric? ci=0 (Yes). Answer requires image to verify.'

    elif idx == 1116:  # id=4057 type=1
        notes = 'MCQ: Is figure symmetric? ci=0 (Yes). Answer requires image to verify.'

    elif idx == 1117:  # id=4058 type=1
        notes = 'MCQ: Is figure symmetric? ci=1 (No). Answer requires image to verify.'

    elif idx == 1118:  # id=4059 type=1
        notes = 'MCQ: Is figure symmetric? ci=0 (Yes). Answer requires image to verify.'

    elif idx == 1119:  # id=4060 type=1
        notes = 'MCQ: Is figure symmetric? ci=0 (Yes). Answer requires image to verify.'

    elif idx == 1120:  # id=4061 type=1
        notes = 'MCQ: Is figure symmetric? ci=1 (No). Answer requires image to verify.'

    elif idx == 1121:  # id=4062 type=1
        notes = 'MCQ: Is figure symmetric? ci=1 (No). Answer requires image to verify.'

    elif idx == 1122:  # id=4063 type=1
        notes = 'MCQ: Is figure symmetric? ci=1 (No). Answer requires image to verify.'

    elif idx == 1123:  # id=4064 type=1
        notes = 'MCQ: Is figure symmetric? ci=0 (Yes). Answer requires image to verify.'

    elif idx == 1124:  # id=4066 type=1
        notes = 'MCQ: Is Line AB a line of symmetry? ci=0 (Yes). Answer requires image.'

    elif idx == 1125:  # id=4067 type=1
        notes = 'MCQ: Halves do not fit/fit? ci=0 (do not fit). Paired with id=4068 (line CD is not symmetry). Consistent.'

    elif idx == 1126:  # id=4068 type=1
        notes = 'MCQ: Line CD is/is not symmetry? ci=1 (is not). Paired with id=4067 (halves do not fit). Consistent.'

    elif idx == 1127:  # id=4069 type=1
        notes = 'MCQ: Halves do not fit/fit? ci=1 (fit). Paired with id=4070 (line XY is symmetry). Consistent.'

    elif idx == 1128:  # id=4070 type=1
        notes = 'MCQ: Line XY is/is not symmetry? ci=0 (is). Paired with id=4069 (halves fit). Consistent.'

    elif idx == 1129:  # id=4071 type=1
        notes = 'MCQ: Halves do not fit/fit? ci=0 (do not fit). Paired with id=4072 (line ST is not symmetry). Consistent.'

    elif idx == 1130:  # id=4072 type=1
        notes = 'MCQ: Line ST is/is not symmetry? ci=1 (is not). Paired with id=4071 (halves do not fit). Consistent.'

    elif idx == 1131:  # id=4073 type=1
        notes = 'MCQ: Is this the line of symmetry? ci=0 (Yes). Answer requires image.'

    elif idx == 1132:  # id=4074 type=1
        notes = 'MCQ: Is this the line of symmetry? ci=1 (No). Answer requires image.'

    elif idx == 1133:  # id=4075 type=1
        notes = 'MCQ: Is this the line of symmetry? ci=0 (Yes). Answer requires image.'

    elif idx == 1134:  # id=4076 type=1
        notes = 'MCQ: Is this the line of symmetry? ci=0 (Yes). Answer requires image.'

    elif idx == 1135:  # id=4078 type=1
        notes = 'MCQ: Min squares to colour for symmetry? ci=1 (2 squares). Answer requires image.'

    elif idx == 1136:  # id=4083 type=1
        notes = 'MCQ: Which square box to colour for symmetry? ci=0 (box 1). Answer requires image.'

    elif idx == 1137:  # id=4084 type=1
        notes = 'MCQ: Which 2 squares for symmetry? ci=2 (B and D). Answer requires image.'

    elif idx == 1138:  # id=4085 type=1
        notes = 'MCQ: Which 2 squares for symmetry? ci=0 (B and D). Answer requires image.'

    elif idx == 1139:  # id=4135 type=2
        notes = 'Cruise pricing FIB. ci=3000 matches answer. Answer requires rate table image to verify.'

    elif idx == 1140:  # id=4173 type=1
        notes = 'MCQ: Pattern missing number. ci=3 (answer 4). Answer requires pattern image to verify.'

    elif idx == 1141:  # id=4200 type=2
        notes = 'Multi-blank: chairs around joined tables. answers=[26,92]; ci=26 matches first blank. Pattern question; answer requires image.'

    elif idx == 1142:  # id=4201 type=2
        notes = 'Multi-blank: squares in pattern figures. answers=[15,7,105]; ci=15 matches first blank. Answer requires image.'

    elif idx == 1143:  # id=4202 type=2
        notes = 'Multi-blank: squares in pattern figures. answers=[17,10,59]; ci=17 matches first blank. Answer requires image.'

    elif idx == 1144:  # id=4290 type=1
        notes = 'MCQ: Perimeter of square STUV. ci=1 (24 4/5). Answer requires figure for side length.'

    elif idx == 1145:  # id=4291 type=1
        verdict = 'NEEDS_REVISION'
        notes = 'NAMING INCONSISTENCY: question names square "ABCD" then refers to "square ABDC". ABDC is not standard vertex order for a square. ci=3 (1/4). Answer requires figure to verify.'

    elif idx == 1146:  # id=4292 type=1
        notes = 'MCQ: Perimeter of rectangle EFGH. ci=2 (2 2/3). Answer requires figure for dimensions.'

    elif idx == 1147:  # id=4293 type=1
        notes = 'MCQ: Perimeter of rectangle JKLM. ci=1 (15 1/10). Answer requires figure for dimensions.'

    elif idx == 1148:  # id=4392 type=2
        notes = 'FIB: Difference between largest and smallest triangle areas. ci=3 matches answer. Answer requires figure.'

    elif idx == 1149:  # id=4394 type=2
        verdict = 'NEEDS_REVISION'
        notes = 'FORMULA ERROR: question text displays triangle area formula as 2/1 (twice) instead of 1/2. Answers [2,5,5] for base=2,height=5,area=5. Math is correct (1/2*2*5=5) but the displayed formula is wrong.'

    elif idx == 1150:  # id=4395 type=2
        verdict = 'NEEDS_REVISION'
        notes = 'FORMULA ERROR: question text displays triangle area formula as 2/1 instead of 1/2. Answers [2,3,3] for base=2,height=3,area=3. Math is correct (1/2*2*3=3) but the displayed formula is wrong.'

    elif idx == 1151:  # id=4396 type=2
        verdict = 'NEEDS_REVISION'
        notes = 'HTML CORRUPTION: question text contains "&lt; hr&gt;" (double-encoded <hr> tag). Should be "<hr>". Also likely shares the 2/1 formula error from id=4394/4395.'

    else:
        notes = 'No issues detected from text inspection.'

    items_out.append({
        'index': idx,
        'id': qid,
        'type': qtype,
        'image': img,
        'question': question,
        'answers': answers,
        'correct_index': correct_index,
        'verdict': verdict,
        'notes': notes
    })

    if verdict == 'GOOD':
        good_count += 1
    else:
        needs_revision_count += 1

output = {
    'range': '1088-1151',
    'total': len(items_out),
    'good': good_count,
    'needs_revision': needs_revision_count,
    'items': items_out
}

out_path = 'C:/allgifted/mathapi11v2/_work_barmodels/audit_r6/r6_g1088.json'
with open(out_path, 'w', encoding='utf-8') as f:
    json.dump(output, f, indent=2, ensure_ascii=False)

print(f'Written {len(items_out)} items to r6_g1088.json')
print(f'GOOD: {good_count}, NEEDS_REVISION: {needs_revision_count}')
for item in items_out:
    if item['verdict'] == 'NEEDS_REVISION':
        print(f'  NEEDS_REVISION idx={item["index"]} id={item["id"]}: {item["notes"][:120]}')
