"""Reproduce displayed count-based RR analyses and prespecified sensitivities.

The target inputs are not assumed to define a common clinical estimand.
No claim of efficacy meta-analysis or reconstructed patient-level data.
"""
from pathlib import Path
import json
import math
import numpy as np
from scipy.optimize import minimize_scalar
from scipy.stats import t, norm, chi2

ROOT=Path(__file__).resolve().parents[1]


def synthesize(rows):
    names=list(rows)
    a,n1,c,n0=np.asarray(list(rows.values()),float).T
    assert np.all(a>0) and np.all(c>0)
    y=np.log((a/n1)/(c/n0))
    v=1/a-1/n1+1/c-1/n0
    k=len(y)
    def objective(tau2):
        w=1/(v+tau2);mu=np.dot(w,y)/sum(w)
        return np.log(v+tau2).sum()+math.log(w.sum())+np.dot(w,(y-mu)**2)
    fit=minimize_scalar(objective,bounds=(0,10),method='bounded',options={'xatol':1e-13})
    tau2=fit.x if objective(fit.x)<objective(0) else 0.
    w=1/(v+tau2);mu=np.dot(w,y)/sum(w)
    q_re=np.dot(w,(y-mu)**2)/(k-1)
    se_hk=math.sqrt(q_re/sum(w));se_wald=math.sqrt(1/sum(w))
    w_fe=1/v;mu_fe=np.dot(w_fe,y)/sum(w_fe)
    q=np.dot(w_fe,(y-mu_fe)**2)
    def interval(se,critical):return np.exp([mu-critical*se,mu+critical*se]).tolist()
    return {
      'k':k,'n_tavr':int(sum(n1)),'n_savr':int(sum(n0)),
      'risk_ratio':math.exp(mu),'ci_hk':interval(se_hk,t.ppf(.975,k-1)),
      'p_hk':2*t.sf(abs(mu/se_hk),k-1),'tau2_reml':tau2,
      'hk_scale':q_re,'ci_modified_hk':interval(max(se_hk,se_wald),t.ppf(.975,k-1)),
      'ci_wald':interval(se_wald,norm.ppf(.975)),
      'q':q,'i2_q_pct':max(0,(q-k+1)/q)*100,'q_pvalue':chi2.sf(q,k-1),
      'fixed_effect_rr':math.exp(mu_fe),
      'trial_estimates':{name:{'rr':math.exp(yi),'variance_log_rr':vi,'weight_pct':100*wi/sum(w)} for name,yi,vi,wi in zip(names,y,v,w)},
    }


def main():
    d=json.loads((ROOT/'data/target-forest-inputs.json').read_text())
    results={'description':'Count-based arithmetic reproduction and audit sensitivities; not an updated clinical survival meta-analysis','reproduction':{},'sensitivities':{}}
    for outcome in ['mortality','labelled_death_disabling_stroke']:
        results['reproduction'][outcome]={}
        for panel,rows in d[outcome].items():
            r=synthesize(rows)
            target=d['target_reported_pooled'][outcome][panel]
            r['target_rr_ci']=target
            r['matches_target_at_2dp']=list(map(lambda x:round(x,2),[r['risk_ratio']]+r['ci_hk']))==target
            results['reproduction'][outcome][panel]=r
    for panel in ['1_year','longest']:
        original=d['labelled_death_disabling_stroke'][panel]
        repaired={k:list(v) for k,v in original.items()}
        repaired['DEDICATE']=d['confirmed_endpoint_substitutions']['DEDICATE']['source_death_disabling_stroke']
        repaired['UKTAVI']=d['confirmed_endpoint_substitutions']['UKTAVI']['source_death_disabling_nonfatal_stroke']
        results['sensitivities'][panel+'_two_endpoint_substitutions']=synthesize(repaired)
        r3={k:list(v) for k,v in repaired.items()}
        r3['Evolut']=d['additional_count_discrepancy']['observed_source_1_year'] if panel=='1_year' else r3['Evolut']
        if panel=='1_year':
            results['sensitivities']['1_year_add_observed_evolut_counts_arithmetic_only']=synthesize(r3)
            results['sensitivities']['1_year_two_substitutions_omit_evolut']=synthesize({k:v for k,v in repaired.items() if k!='Evolut'})
        results['sensitivities'][panel+'_original_leave_one_out']={k:synthesize({j:v for j,v in original.items() if j!=k}) for k in original}
        results['sensitivities'][panel+'_two_substitutions_leave_one_out']={k:synthesize({j:v for j,v in repaired.items() if j!=k}) for k in repaired}
    for outcome in ['mortality','labelled_death_disabling_stroke']:
        rows={k:v for k,v in d[outcome]['longest'].items() if d['longest_horizon_years'][k]>=5}
        results['sensitivities'][outcome+'_at_least_5_years_descriptive']=synthesize(rows)
    # Source audit counts and weight sums refer to the ORIGINAL displayed fit.
    results['endpoint_audit']={
        'confirmed_substituted_trial_rows':2,
        'unique_extra_any_stroke_events_tavr':(37-26)+(39-30),
        'unique_extra_any_stroke_events_savr':(68-57)+(40-35),
        'published_displayed_weight_1_year_pct':30.6+28.3,
        'calculated_weight_1_year_pct':sum(results['reproduction']['labelled_death_disabling_stroke']['1_year']['trial_estimates'][k]['weight_pct'] for k in ['DEDICATE','UKTAVI']),
        'longest_includes_1_year_trials':[k for k,v in d['longest_horizon_years'].items() if v==1],
        'longest_horizon_min_years':min(d['longest_horizon_years'].values()),
        'longest_horizon_max_years':max(d['longest_horizon_years'].values()),
    }
    (ROOT/'results').mkdir(exist_ok=True)
    (ROOT/'results/R1.json').write_text(json.dumps(results,indent=2)+'\n')
    for outcome,panels in results['reproduction'].items():
        for name,r in panels.items():print(outcome,name,r['risk_ratio'],r['ci_hk'],r['p_hk'],r['tau2_reml'],r['matches_target_at_2dp'])
    for name,r in results['sensitivities'].items():
        if 'leave_one_out' not in name:print(name,r['risk_ratio'],r['ci_hk'],r['p_hk'])


if __name__=='__main__':main()
