"""Exploratory (not pre-registered): does the Jev x Mistral miss correlation survive removing recorded fails whose
cause code says the label or the supplied evidence is the problem? Same OR / Haldane / vendor-clustered bootstrap
(2,000 resamples, seed 20260921) as analyze.py."""
import json, math
import numpy as np

per = json.load(open('per_claim.json', encoding='utf-8'))
codes = {r['id']: r['code'] for r in json.load(open('cause_codes.json', encoding='utf-8'))['rows']}
F = [r for r in per.values() if r['label'] == 'fail']


def cells(s):
    a = sum(1 for r in s if not r['jev_checker_fail'] and not r['mv_flagged'])
    b = sum(1 for r in s if not r['jev_checker_fail'] and r['mv_flagged'])
    c = sum(1 for r in s if r['jev_checker_fail'] and not r['mv_flagged'])
    d = sum(1 for r in s if r['jev_checker_fail'] and r['mv_flagged'])
    return a, b, c, d


def orr(a, b, c, d):
    if 0 in (a, b, c, d):
        a, b, c, d = a + .5, b + .5, c + .5, d + .5
    return a * d / (b * c)


def boot(s):
    vend = sorted({r['vendor'] for r in s}); byv = {v: [r for r in s if r['vendor'] == v] for v in vend}
    rng = np.random.default_rng(20260921); vals = []
    for _ in range(2000):
        pick = rng.integers(0, len(vend), len(vend))
        vals.append(orr(*cells([r for i in pick for r in byv[vend[i]]])))
    return round(float(np.percentile(vals, 2.5)), 2), round(float(np.percentile(vals, 97.5)), 2)


out = {}
for name, drop in (('all_fails', set()), ('drop_QLABEL_NOTVIS', {'QLABEL', 'NOTVIS'}),
                   ('drop_QLABEL_NOTVIS_PROV', {'QLABEL', 'NOTVIS', 'PROV'})):
    s = [r for r in F if codes.get(r['id']) not in drop]
    a, b, c, d = cells(s)
    out[name] = {'n': len(s), 'both': a, 'jev_only': b, 'mistral_only': c, 'neither': d,
                 'expected_both': round((a + b) * (a + c) / len(s), 2), 'OR': round(orr(a, b, c, d), 2), 'boot95': boot(s)}
json.dump(out, open('explor_or_robustness.json', 'w'), indent=1)
print(json.dumps(out))
