"""Q3.3: join one-line cause codes (scratch/cause_*.txt) with who missed; tally with Wilson intervals.
Also exploratory: did Jev pick a minor (non-fail) flag on its checker misses; Mistral finding/verdict inconsistency."""
import json, glob, math, collections as C

per = json.load(open('per_claim.json', encoding='utf-8'))
B = '../data/'
mrows = {}
for s in ('dev', 'test'):
    for r in json.load(open(B + f'step03_vocab_{s}.json', encoding='utf-8'))['rows']:
        mrows.setdefault(r['id'], []).append(r)

codes = {}
for f in sorted(glob.glob('scratch/cause_*.txt')):
    for line in open(f, encoding='utf-8'):
        if line.strip():
            i, code, why = line.rstrip('\n').split('|', 2)
            assert i not in codes
            codes[i] = (code, why)

MINOR = {'claim_overspecification_minor', 'claim_unsourced_minor', 'dim_disagreement_minor'}


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))


rows = []
for i, r in per.items():
    if r['label'] != 'fail':
        continue
    jm, mm = not r['jev_checker_fail'], not r['mv_flagged']
    if not (jm or mm):
        continue
    who = 'both' if jm and mm else ('jev_only' if jm else 'mistral_only')
    code, why = codes[i]
    mfind = [x.get('finding') for x in mrows[i] if not x.get('error')]
    rows.append({'id': i, 'split': r['split'], 'type': r['type'], 'who_missed': who, 'code': code, 'reason': why,
                 'jev_flags': r['jev_flags'], 'jev_conf': r['jev_conf'], 'jev_escalated': not r['jev_accept'],
                 'jev_picked_minor_flag': any(f in MINOR for f in r['jev_flags']),
                 'mistral_verdicts': r['mv_verdicts'], 'mistral_findings': mfind,
                 'mistral_major_finding_but_pass': any(v == 'pass' and f in ('dim_disagreement_major', 'contradicted_fact', 'fabricated_or_unsupported_claim')
                                                       for v, f in zip([x.get('verdict') for x in mrows[i] if not x.get('error')], mfind)),
                 'key_flag': r['key_flag']})
assert set(codes) == {x['id'] for x in rows}, set(codes) ^ {x['id'] for x in rows}
n = len(rows)
tally = {'n_missed_by_either': n, 'by_code': {}, 'by_code_x_who': {}, 'by_code_x_split': {}}
cc = C.Counter(x['code'] for x in rows)
for code, k in cc.most_common():
    lo, hi = wilson(k, n)
    tally['by_code'][code] = {'k': k, 'n': n, 'share': round(k / n, 3), 'lo': lo, 'hi': hi}
for who in ('jev_only', 'both', 'mistral_only'):
    sub = [x for x in rows if x['who_missed'] == who]
    tally['by_code_x_who'][who] = {'n': len(sub), **dict(C.Counter(x['code'] for x in sub).most_common())}
for sp in ('dev', 'test'):
    sub = [x for x in rows if x['split'] == sp]
    tally['by_code_x_split'][sp] = {'n': len(sub), **dict(C.Counter(x['code'] for x in sub).most_common())}
notclear = sum(1 for x in rows if x['code'] in ('NOTVIS', 'QLABEL'))
tally['H6_NOTVIS_or_QLABEL'] = {'k': notclear, 'n': n, 'share': round(notclear / n, 3), 'ci': wilson(notclear, n)}
prov_like = sum(1 for x in rows if x['code'] in ('PROV', 'NOTVIS', 'QLABEL'))
tally['explor_PROV_NOTVIS_QLABEL'] = {'k': prov_like, 'n': n, 'share': round(prov_like / n, 3), 'ci': wilson(prov_like, n)}
jm = [x for x in rows if x['who_missed'] in ('jev_only', 'both')]
tally['explor_jev_checker_misses_with_minor_flag'] = {'k': sum(x['jev_picked_minor_flag'] for x in jm), 'n': len(jm)}
tally['explor_jev_checker_misses_escalated_anyway'] = {'k': sum(x['jev_escalated'] for x in jm), 'n': len(jm)}
mm = [x for x in rows if x['who_missed'] in ('mistral_only', 'both')]
tally['explor_mistral_misses_with_major_finding_but_pass'] = {'k': sum(x['mistral_major_finding_but_pass'] for x in mm), 'n': len(mm),
                                                               'ids': [x['id'] for x in mm if x['mistral_major_finding_but_pass']]}
json.dump({'rows': sorted(rows, key=lambda x: x['id']), 'tally': tally}, open('cause_codes.json', 'w', encoding='utf-8'), indent=1)
print(json.dumps(tally, indent=0))
