"""WS3 analysis. Reads frozen claim_types.json plus labels and existing model outputs. No API calls.
Writes per_claim.json and tables.json. Run from out/exp/ws3/."""
import json, math, collections as C
import numpy as np
from scipy.stats import binomtest, fisher_exact

B = '../data/'
FAILSET = {'dim_disagreement_major', 'contradicted_fact', 'fabricated_or_unsupported_claim'}
VALID_FLAGS = {'none', 'claim_overspecification_minor', 'claim_unsourced_minor', 'dim_disagreement_minor'} | FAILSET
THR = 0.8
SEED = 20260921
NBOOT = 2000

claims = {c['id']: c for c in json.load(open(B + 'claims_200.json', encoding='utf-8'))['claims']}
types = json.load(open('claim_types.json', encoding='utf-8'))['claims']
prov = json.load(open(B + 'label_provenance.json', encoding='utf-8'))


def load(prefix):
    rows = []
    for s in ('dev', 'test'):
        rows += json.load(open(B + f'{prefix}_{s}.json', encoding='utf-8'))['rows']
    by = C.defaultdict(list)
    for r in rows:
        by[r['id']].append(r)
    return by


jev = load('results'); mv = load('step03_vocab'); mp = load('step03_prod')


def prod_group(p):
    return {'claude_opus_validator': 'opus_validator', 'deterministic_ground_py': 'ground_py'}.get(p, 'other')


per = {}
for i, c in claims.items():
    jr = [r for r in jev[i] if not r.get('error') and r['flag'] in VALID_FLAGS]
    j_err = len(jev[i]) - len(jr)
    rec = {
        'id': i, 'vendor': c['vendor'], 'split': c['split'], 'cohort': c['cohort'], 'section': c['section'],
        'verbatim': bool(c['evidence_is_verbatim']), 'label': c['internal_verdict'], 'key_flag': c.get('internal_flag'),
        'producer': prov[i]['producer'], 'producer_group': prod_group(prov[i]['producer']),
        'type': types[i]['type'], 'type_method': types[i]['method'], 'has_number': types[i]['has_number'],
        'jev_valid_reps': len(jr), 'jev_err_reps': j_err,
        'jev_flags': [r['flag'] for r in jr], 'jev_conf': [r['confidence'] for r in jr],
    }
    if jr:
        rec['jev_checker_fail'] = any(r['flag'] in FAILSET for r in jr)
        rec['jev_accept'] = all(r['flag'] == 'none' and float(r['confidence']) >= THR for r in jr)
    rec['jev_accept_failclosed'] = (j_err == 0) and rec.get('jev_accept', False)
    for tag, src in (('mv', mv), ('mp', mp)):
        vr = [r for r in src[i] if not r.get('error') and r.get('verdict') not in (None, '', 'error')]
        rec[f'{tag}_valid_reps'] = len(vr); rec[f'{tag}_err_reps'] = len(src[i]) - len(vr)
        rec[f'{tag}_verdicts'] = [r['verdict'] for r in vr]
        if vr:
            rec[f'{tag}_flagged'] = any(r['verdict'] != 'pass' for r in vr)
    per[i] = rec

json.dump(per, open('per_claim.json', 'w', encoding='utf-8'), indent=0)


def wilson(k, n, z=1.96):
    if n == 0:
        return (None, None)
    p = k / n; d = 1 + z * z / n
    c = (p + z * z / (2 * n)) / d; h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / d
    return (round(max(0, c - h), 3), round(min(1, c + h), 3))


def rate(recs, pred):
    k = sum(1 for r in recs if pred(r)); n = len(recs)
    lo, hi = wilson(k, n)
    return {'k': k, 'n': n, 'rate': round(k / n, 3) if n else None, 'lo': lo, 'hi': hi, 'small': n < 10}


J_MISS = lambda r: not r['jev_checker_fail']          # on recorded fails: checker view miss
J_ESC = lambda r: r['jev_accept']                     # on recorded fails: escalation view escape
M_MISS = lambda r: not r['mv_flagged']
P_MISS = lambda r: not r['mp_flagged']
J_OVER = lambda r: r['jev_checker_fail']              # on recorded passes
J_UNNEED = lambda r: not r['jev_accept']
M_UNNEED = lambda r: r['mv_flagged']


def subset(split, label):
    return [r for r in per.values() if r['label'] == label and (split == 'pooled' or r['split'] == split)
            and 'jev_checker_fail' in r]


def block(F, P):
    return {'fails': {'jev_checker_miss': rate(F, J_MISS), 'jev_escape': rate(F, J_ESC),
                      'mistral_vocab_miss': rate([r for r in F if 'mv_flagged' in r], M_MISS),
                      'mistral_prod_miss': rate([r for r in F if 'mp_flagged' in r], P_MISS)},
            'passes': {'jev_checker_overflag': rate(P, J_OVER), 'jev_unneeded_escalation': rate(P, J_UNNEED),
                       'mistral_vocab_unneeded': rate([r for r in P if 'mv_flagged' in r], M_UNNEED)}}


DIMS = ['type', 'section', 'cohort', 'verbatim', 'producer_group', 'has_number']
tables = {'excluded': {m: [i for i, r in per.items() if k not in r] for m, k in
                       (('jev', 'jev_checker_fail'), ('mistral_vocab', 'mv_flagged'), ('mistral_prod', 'mp_flagged'))}}
t31 = {}
for split in ('pooled', 'dev', 'test'):
    F = subset(split, 'fail'); P = subset(split, 'pass')
    t31[split] = {'overall': block(F, P)}
    for d in DIMS:
        t31[split][d] = {val: block([r for r in F if str(r[d]) == val], [r for r in P if str(r[d]) == val])
                         for val in sorted({str(r[d]) for r in per.values()})}
tables['q31'] = t31


def cluster_boot(recs, stat, seed=SEED, nboot=NBOOT):
    vend = sorted({r['vendor'] for r in recs})
    byv = {v: [r for r in recs if r['vendor'] == v] for v in vend}
    rng = np.random.default_rng(seed)
    vals, undefined = [], 0
    for _ in range(nboot):
        pick = rng.integers(0, len(vend), len(vend))
        sample = [r for idx in pick for r in byv[vend[idx]]]
        s = stat(sample) if sample else None
        if s is None or (isinstance(s, float) and (math.isnan(s) or math.isinf(s))):
            undefined += 1
        else:
            vals.append(s)
    if not vals:
        return {'lo': None, 'hi': None, 'undefined': undefined, 'n_vendors': len(vend)}
    return {'lo': round(float(np.percentile(vals, 2.5)), 3), 'hi': round(float(np.percentile(vals, 97.5)), 3),
            'undefined': undefined, 'n_vendors': len(vend)}


def cells(recs, a_miss, b_miss):
    a = sum(1 for r in recs if a_miss(r) and b_miss(r)); b = sum(1 for r in recs if a_miss(r) and not b_miss(r))
    c = sum(1 for r in recs if not a_miss(r) and b_miss(r)); d = sum(1 for r in recs if not a_miss(r) and not b_miss(r))
    return a, b, c, d


def odds_ratio(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 twobytwo(recs, a_miss, b_miss, label_a, label_b):
    a, b, c, d = cells(recs, a_miss, b_miss); n = a + b + c + d
    orr = odds_ratio(a, b, c, d)
    boot = cluster_boot(recs, lambda s: odds_ratio(*cells(s, a_miss, b_miss)))
    pmj = rate([r for r in recs if a_miss(r)], b_miss); pmc = rate([r for r in recs if not a_miss(r)], b_miss)

    def diffstat(s):
        x = [r for r in s if a_miss(r)]; y = [r for r in s if not a_miss(r)]
        if not x or not y:
            return None
        return sum(map(b_miss, x)) / len(x) - sum(map(b_miss, y)) / len(y)
    mc = binomtest(min(b, c), b + c, 0.5).pvalue if b + c else 1.0
    fe = fisher_exact([[a, b], [c, d]])
    return {'rows': label_a, 'cols': label_b, 'n': n,
            'both': a, 'row_only': b, 'col_only': c, 'neither': d,
            'expected_both_if_independent': round((a + b) * (a + c) / n, 2) if n else None,
            'odds_ratio': round(orr, 3), 'haldane_applied': 0 in (a, b, c, d), 'or_boot95': boot,
            'P_col_given_row': pmj, 'P_col_given_not_row': pmc,
            'diff_conditional_boot95': cluster_boot(recs, diffstat),
            'rate_row': round((a + b) / n, 3), 'rate_col': round((a + c) / n, 3),
            'diff_col_minus_row_boot95': cluster_boot(recs, lambda s: (sum(map(b_miss, s)) - sum(map(a_miss, s))) / len(s)),
            'mcnemar_exact_p': round(mc, 4), 'fisher_p_exploratory': round(fe.pvalue, 4)}


q32 = {}
for split in ('pooled', 'dev', 'test'):
    F = [r for r in subset(split, 'fail') if 'mv_flagged' in r and 'mp_flagged' in r]
    P = [r for r in subset(split, 'pass') if 'mv_flagged' in r and 'mp_flagged' in r]
    q32[split] = {
        'fails_jevchecker_x_vocab': twobytwo(F, J_MISS, M_MISS, 'Jev checker miss', 'Mistral vocab miss'),
        'fails_jevchecker_x_prod': twobytwo(F, J_MISS, P_MISS, 'Jev checker miss', 'Mistral prod miss'),
        'fails_jevescape_x_vocab': twobytwo(F, J_ESC, M_MISS, 'Jev escape (escalation view)', 'Mistral vocab miss'),
        'passes_jevunneeded_x_vocab': twobytwo(P, J_UNNEED, M_UNNEED, 'Jev unneeded escalation', 'Mistral vocab flags pass'),
        'passes_jevoverflag_x_vocab': twobytwo(P, J_OVER, M_UNNEED, 'Jev checker over-flag', 'Mistral vocab flags pass'),
    }
tables['q32'] = q32

F = subset('pooled', 'fail')


def grp_diff(key, a, b):
    def f(s):
        x = [r for r in s if r[key] == a]; y = [r for r in s if r[key] == b]
        if not x or not y:
            return None
        return sum(map(J_MISS, x)) / len(x) - sum(map(J_MISS, y)) / len(y)
    p = f(F)
    return {'point': None if p is None else round(p, 3), 'boot95': None if p is None else cluster_boot(F, f)}


tables['explor_paraphrase_minus_verbatim_jev_checker_miss'] = grp_diff('verbatim', False, True)
tables['explor_opus_minus_groundpy_jev_checker_miss'] = grp_diff('producer_group', 'opus_validator', 'ground_py')
tables['jev_error_reps_total'] = sum(r['jev_err_reps'] for r in per.values())
tables['mistral_vocab_error_reps_total'] = sum(r['mv_err_reps'] for r in per.values())
tables['mistral_prod_error_reps_total'] = sum(r['mp_err_reps'] for r in per.values())
tables['failclosed_differs_claims'] = sum(1 for r in per.values() if r.get('jev_accept') != r['jev_accept_failclosed'])
json.dump(tables, open('tables.json', 'w', encoding='utf-8'), indent=1)
print('ok', {k: len(v) for k, v in tables['excluded'].items()}, tables['jev_error_reps_total'],
      tables['mistral_vocab_error_reps_total'], tables['mistral_prod_error_reps_total'], tables['failclosed_differs_claims'])
