import json, sys
sys.path.insert(0, '/tmp/claude-1000/-var-www-html-peta/310517d2-8f28-442b-9907-0e3560add2cb/scratchpad')
from r2b06_decisions import D
from r2b06_updates import U
D = dict(D); D.update(U)
inp = '/var/www/html/peta/storage/app/propertylab-catalogue-match/review/r2_batch_06_in.jsonl'
out = '/var/www/html/peta/storage/app/propertylab-catalogue-match/review/r2_batch_06_out.jsonl'
cases = [json.loads(l) for l in open(inp)]
problems = []
lines = []
seen = set()
for c in cases:
    cid = c['case']
    if cid in seen:
        problems.append(f'dup input {cid}')
    seen.add(cid)
    if cid not in D:
        problems.append(f'missing {cid}'); continue
    v, k, conf, reason, web = D[cid]
    ks = [x['k'] for x in c['catalogue_candidates']]
    if v not in ('SAME', 'PART_OF', 'DIFFERENT', 'UNSURE'): problems.append(f'bad verdict {cid}')
    if conf not in ('high', 'medium', 'low'): problems.append(f'bad conf {cid}')
    if v == 'DIFFERENT' and k is not None: problems.append(f'DIFFERENT with cand {cid}')
    if v in ('SAME', 'PART_OF') and k is None: problems.append(f'{v} without cand {cid}')
    if k is not None and k not in ks: problems.append(f'cand {k} not in candidates {cid} {ks}')
    nw = len(reason.split())
    if nw > 20: problems.append(f'reason {nw} words {cid}')
    lines.append(json.dumps({"case": cid, "verdict": v, "cand": k, "confidence": conf, "reason": reason, "web": web}, ensure_ascii=False))
extra = set(D) - seen
if extra: problems.append(f'extra keys {sorted(extra)}')
print('problems:', problems)
if not problems:
    with open(out, 'w') as f:
        f.write('\n'.join(lines) + '\n')
    print('wrote', len(lines), 'lines')
