#!/usr/bin/env python3
"""Original cardinality-bound analysis; never imports or runs another agent's code.

Inputs are unique-patient counts from FAME-3 5y appendix Table S9, stable angina.
This concerns reported counts, not censoring-adjusted five-year treatment risks.
"""
import csv
import hashlib
import itertools
import json
import math
from pathlib import Path

import numpy as np
from scipy.optimize import Bounds, LinearConstraint, milp
from scipy.stats import beta

ROOT = Path(__file__).resolve().parents[1]
HERE = ROOT / 'data'
OUT = ROOT / 'results'
OUT.mkdir(exist_ok=True)
# Bit order: death, all MI, stroke, spontaneous MI. S must be a subset of M.
CELLS = [b for b in itertools.product((0, 1), repeat=4) if b[3] <= b[1]]
MAT = np.array([[1] * len(CELLS)] + [[b[i] for b in CELLS] for i in range(4)] +
               [[int(any(b[:3])) for b in CELLS]], dtype=float)
OBJ = np.array([int(b[0] or b[3]) for b in CELLS], dtype=float)


def solve(row, sense, target=None):
    rhs = np.array([row[k] for k in ('N', 'D', 'M', 'T', 'S', 'U')], dtype=float)
    a, y = MAT, rhs
    if target is not None:
        a = np.vstack([a, OBJ]); y = np.r_[y, target]
    r = milp(OBJ * sense, integrality=np.ones(len(CELLS)),
             bounds=Bounds(np.zeros(len(CELLS)), np.full(len(CELLS), row['N'])),
             constraints=LinearConstraint(a, y, y),
             options={'time_limit': 20, 'mip_rel_gap': 0})
    if not r.success:
        raise RuntimeError(r.message)
    x = np.rint(r.x).astype(int)
    assert np.array_equal(a @ x, y), (row, x)
    assert np.all(x >= 0)
    return int(OBJ @ x), x


def analytic(row):
    n, d, m, t, s, u = [row[k] for k in ('N', 'D', 'M', 'T', 'S', 'U')]
    assert 0 <= s <= m <= n and max(d, m, t) <= u <= min(n, d + m + t)
    return max(d, s, u - (m - s) - t), min(d + s, u)


def compositions(n, k):
    """All integer allocations of n anonymous participants to k membership cells."""
    if k == 1:
        yield (n,)
    else:
        for i in range(n + 1):
            for rest in compositions(n - i, k - 1):
                yield (i,) + rest


def exhaustive_small_n(max_n=6):
    """Independent exhaustive enumeration checks endpoints, not MILP internals."""
    groups = {}
    allocations = 0
    for n in range(max_n + 1):
        for cells in compositions(n, len(CELLS)):
            x = np.array(cells, dtype=int)
            signature = tuple((MAT @ x).astype(int))
            v = int(OBJ @ x)
            if signature not in groups:
                groups[signature] = [v, v]
            else:
                groups[signature][0] = min(groups[signature][0], v)
                groups[signature][1] = max(groups[signature][1], v)
            allocations += 1
    for signature, extrema in groups.items():
        row = dict(zip(('N', 'D', 'M', 'T', 'S', 'U'), signature))
        assert tuple(extrema) == analytic(row), (row, extrema, analytic(row))
    return {'max_population': max_n, 'allocations': allocations,
            'distinct_feasible_marginals': len(groups), 'all_extrema_match': True}


def clopper_pearson(k, n, level=0.975):
    # Each arm 97.5% => Bonferroni joint coverage at least 95%.
    alpha = 1 - level
    lower = 0.0 if k == 0 else float(beta.ppf(alpha / 2, k, n - k + 1))
    upper = 1.0 if k == n else float(beta.ppf(1 - alpha / 2, k + 1, n - k))
    return lower, upper


def main():
    with (HERE / 'counts.csv').open(newline='') as f:
        rows = [{k: (v if k == 'arm' else int(v)) for k, v in r.items()}
                for r in csv.DictReader(f)]
    arms, witnesses = {}, []
    for row in rows:
        lo, hi = analytic(row)
        got_lo, xlo = solve(row, 1)
        got_hi, xhi = solve(row, -1)
        assert (lo, hi) == (got_lo, got_hi)
        # Every integer in the interval must actually be feasible, not just extrema.
        for target in range(lo, hi + 1):
            got, _ = solve(row, 1, target)
            assert got == target
        arms[row['arm']] = {'counts': row, 'union_lower': lo, 'union_upper': hi,
                            'fraction_lower': lo / row['N'],
                            'fraction_upper': hi / row['N'],
                            'every_integer_feasible': True}
        for label, x in [('minimum', xlo), ('maximum', xhi)]:
            for b, count in zip(CELLS, x):
                witnesses.append(dict(arm=row['arm'], extremum=label,
                                      death=b[0], all_mi=b[1], stroke=b[2],
                                      spontaneous_mi=b[3], participants=int(count)))
    p, c = arms['PCI'], arms['CABG']
    # Cartesian combinations are possible because the randomized arms do not share people.
    pairs = list(itertools.product(range(p['union_lower'], p['union_upper'] + 1),
                                   range(c['union_lower'], c['union_upper'] + 1)))
    npci, ncabg = p['counts']['N'], c['counts']['N']
    diffs = [a / npci - b / ncabg for a, b in pairs]
    cis = []
    for a, b in pairs:
        lp, up = clopper_pearson(a, npci)
        lc, uc = clopper_pearson(b, ncabg)
        cis.append((lp - uc, up - lc))
    # Deliberately extreme sensitivity: global losses could all be stable and have
    # unreported endpoint events. Not a missingness estimate or a sharp IPD bound.
    missing_pci, missing_cabg = 33, 47
    missing_rd = [p['union_lower'] / npci - (c['union_upper'] + missing_cabg) / ncabg,
                  (p['union_upper'] + missing_pci) / npci - c['union_lower'] / ncabg]
    result = {
        'analysis': 'Exploratory reported-count identification bounds; not a clinical risk estimator',
        'source': 'Fearon et al., Lancet 2025, doi:10.1016/S0140-6736(25)00505-7, appendix Table S9',
        'population': 'Stable-angina subgroup; non-left-main multivessel disease',
        'notation': {'N': 'randomized denominator', 'D': 'death', 'M': 'all MI',
                     'T': 'stroke', 'S': 'spontaneous MI, subset of M',
                     'U': 'death or all MI or stroke'},
        'target': 'number of reported participants with death or spontaneous MI',
        'bound': {'lower': 'max(D,S,U-(M-S)-T)', 'upper': 'min(D+S,U)'},
        'arms': arms,
        'crude_fraction_difference_pci_minus_cabg_pp': [100 * min(diffs), 100 * max(diffs)],
        'feasible_count_pairs': len(pairs),
        'hypothetical_complete_followup_binomial_95pct_confidence_envelope_pp':
            [100 * min(t[0] for t in cis), 100 * max(t[1] for t in cis)],
        'binomial_envelope_warning': 'Conditional methods illustration only: assumes complete comparable follow-up and independent Bernoulli participants; not a survival interval or a correction for actual missing outcomes; no multiplicity adjustment for exploratory selection.',
        'extreme_missingness_sensitivity_pp': [100 * t for t in missing_rd],
        'extreme_missingness_warning': 'Uses all global trial withdrawals/losses (33 PCI,47 CABG) as potentially stable with unknown union outcomes; deliberately permissive, not an estimated or sharp stable-specific bound.',
        'validation': {'integer_programming': 'scipy.optimize.milp, all constraints checked exactly after integer rounding',
                       'small_n_exhaustive': exhaustive_small_n()},
        'clinical_limits': ['Counts are source-reported crude fractions; timing, overlap identities, censoring and stable-specific missingness are unavailable.',
                            'Bounds do not identify an observed composite, hazard ratio, five-year treatment risk, significance test or mortality benefit.',
                            'No clinical equivalence or noninferiority margin is specified.',
                            'This exploratory analysis is a separate follow-on to the earlier evidence synthesis.']}
    (OUT / 'bounds.json').write_text(json.dumps(result, indent=2) + '\n')
    with (OUT / 'integer-witnesses.csv').open('w', newline='') as f:
        writer = csv.DictWriter(f, fieldnames=list(witnesses[0]))
        writer.writeheader(); writer.writerows(witnesses)
    for arm, info in arms.items():
        print(arm, info['union_lower'], info['union_upper'])
    print('RD bounds pp', result['crude_fraction_difference_pci_minus_cabg_pp'])
    print('Conditional binomial envelope pp', result['hypothetical_complete_followup_binomial_95pct_confidence_envelope_pp'])
    print('Extreme missingness pp', result['extreme_missingness_sensitivity_pp'])
    print('Exhaustive small-n validation', result['validation']['small_n_exhaustive'])


def emit_declared_context():
    x = json.loads((OUT / 'bounds.json').read_text())
    p, c = x['arms']['PCI'], x['arms']['CABG']
    d = {
      'ci_level':95, 'qol_12m_label':'12 months',
      'pci_lower':p['union_lower'], 'pci_upper':p['union_upper'],
      'cabg_lower':c['union_lower'], 'cabg_upper':c['union_upper'],
      'pci_n':p['counts']['N'], 'cabg_n':c['counts']['N'],
      'pci_count_range':f"{p['union_lower']} to {p['union_upper']}",
      'cabg_count_range':f"{c['union_lower']} to {c['union_upper']}",
      'rd_pp':[round(v,2) for v in x['crude_fraction_difference_pci_minus_cabg_pp']],
      'rd_range_pp':' to '.join(f'{v:+.2f}' for v in x['crude_fraction_difference_pci_minus_cabg_pp']),
      'binomial_envelope_pp':[round(v,2) for v in x['hypothetical_complete_followup_binomial_95pct_confidence_envelope_pp']],
      'binomial_range_pp':' to '.join(f'{v:+.2f}' for v in x['hypothetical_complete_followup_binomial_95pct_confidence_envelope_pp']),
      'missing_pp':[round(v,2) for v in x['extreme_missingness_sensitivity_pp']],
      'missing_range_pp':' to '.join(f'{v:+.2f}' for v in x['extreme_missingness_sensitivity_pp']),
      'feasible_pairs':x['feasible_count_pairs'],
      'allocations':x['validation']['small_n_exhaustive']['allocations'],
      'marginal_sets':x['validation']['small_n_exhaustive']['distinct_feasible_marginals'],
      'small_n_max':x['validation']['small_n_exhaustive']['max_population'],
      'all_intermediate_integers_feasible':p['every_integer_feasible'] and c['every_integer_feasible']}
    with (HERE/'stable-qol.csv').open(newline='') as f:q=list(csv.DictReader(f))
    d['qol']=[{k:(float(v) if k in ['PCI_mean','CABG_mean','adjusted_difference','lower_95','upper_95'] else v) for k,v in row.items()} for row in q]
    d['economic']=json.loads((HERE/'clinical-context.json').read_text())
    d['registry']=json.loads((HERE/'registry-snapshot.json').read_text())
    d['registry_n']=len(d['registry'])
    d['scope_audit']=json.loads((HERE/'scope-clarifications.json').read_text())
    (OUT/'R1.json').write_text(json.dumps(d,indent=2)+'\n')

if __name__ == '__main__':
    main()
    emit_declared_context()
