"""Print cases (by case id) with R facts, candidates and the AI verdict — for manual review."""
import json, glob, sys
R = '/var/www/html/peta/storage/app/propertylab-catalogue-match/review'
cases = {}
for fn in glob.glob(f'{R}/*batch_*_in.jsonl'):
    for l in open(fn):
        c = json.loads(l); cases[c['case']] = c
ai = {}
for fn in glob.glob(f'{R}/*batch_*_out.jsonl'):
    for l in open(fn):
        if l.strip():
            v = json.loads(l); ai[v['case']] = v
ids = sys.argv[1:] if sys.argv[1:] != ['-'] else [l.strip() for l in sys.stdin if l.strip()]
for cid in ids:
    c = cases.get(cid)
    if not c: print(cid, 'not found'); continue
    r = c['realtycheck']
    print(f"\n[{cid}] R: {r['name']} | {r['category']} | {r['district'] or '-'} / {r['state'] or '-'} | mukim {r['mukim'] or '-'} | {r['coord_precision']} | median {r['median_price_rm']} psf {r['psf']}")
    for x in c['catalogue_candidates']:
        tw = f"  (also listed as: {', '.join(x['same_listing_also_as'][:3])})" if x.get('same_listing_also_as') else ''
        print(f"   {x['k']}: {x['name']} | {x['type'] or '-'} | {x['area'] or '-'}, {x['state'] or '-'} | {x['dist_m']} m | median {x['price_median']} psf {x['psf_median']} | {x['sale_status'] or ''} {x['completion_year'] or ''}{tw}")
    v = ai.get(cid)
    if v: print(f"   AI: {v['verdict']} cand={v.get('cand')} {v.get('confidence')} web={v.get('web')} — {v.get('reason')}")
