#!/usr/bin/env python3
"""Promotion / equivalence decision driver (PLAN-next-gpu-v4). Explicit score files, paired by seed index.

  promote.py --cand C_s1.json [C_s2.json] --ref R_s1.json [R_s2.json] [--mode promote|equivalence] [--n 4000]
            [--patch-meta-dir DIR] [--out decision.json]

Score files are s1_score.py outputs (scores_window.json for window fits, scores_trim.json for whole-scroll fits).
Statistics (all differences are cand - ref, in percentage points):
- f025 and censored f025: per-line n025 / scored (or / points), pooled over lines; paired block bootstrap over the
  51 lines in 10 z-blocks (lines sorted by mean z; 9x5 + 1x6, identical resamples for every arm/seed); sensitivity:
  5 blocks (4x10 + 1x11) and leave-one-block-out.
- held fiber-off: equal-fiber rate (mean over evaluated fibers of off/evaluated); bootstrap over WHOLE fibers.
- held patches: sum satisfied_area / sum frozen_area; bootstrap over spatial z-blocks of patches (bbox from meta.json).
Routes (mode promote; every seed pair must pass; CI = two-sided 90%, bound = one-sided 95%):
  geometry: f025 >= +1.0 each seed AND CI(f025) > 0 AND upper bound(held_off deterioration) < +1.0
  topology: held_off <= -2.0 each seed AND CI(held_off) < 0 AND lower bound(f025) > -0.5 (non-inferior)
  both routes also: held_patches point estimate > -2.0 AND lower bound > -2.0
Mode equivalence (B4): CI(f025) inside [-0.5, +0.5] AND held_patches >= 0 on each seed AND upper bound(held_off) < +1.0.
Whole-winding run comparisons and Jacobian probes are reported separately (jump map); borderline = INCONCLUSIVE."""
import argparse, json, os, sys
import numpy as np
sys.path.insert(0, '/media/djosey/nvme4/spiral_0332/work')
D = '/media/djosey/nvme4/spiral_0332'
ap = argparse.ArgumentParser()
ap.add_argument('--cand', nargs='+', required=True); ap.add_argument('--ref', nargs='+', required=True)
ap.add_argument('--mode', choices=['promote', 'equivalence'], default='promote'); ap.add_argument('--n', type=int, default=4000)
ap.add_argument('--patch-meta-dir', default=f'{D}/dataset/verified_patches_hub1030'); ap.add_argument('--out', help='decision json (default: <cand run dir>/promote_vs_<ref run>.json)')
a = ap.parse_args()
assert len(a.cand) == len(a.ref), 'cand and ref must pair by seed index'
from slice_eval import load_lines
lines = load_lines(f'/media/djosey/nvme4/fibers/PHerc0332_vcslices.volpkg.json', step=8, verbose=False)
SEQ = [s if isinstance(s, str) else f'{int(s):06d}' for s, _ in sorted(lines, key=lambda t: t[1][:, 0].mean())]
assert len(SEQ) == 51
rng = np.random.default_rng(0)

def load(p):
    s = json.load(open(p)); w = s['slice']['windows']['all']; pl = {e['seq']: e for e in w['per_line']}
    n025 = np.array([pl[q]['n025'] for q in SEQ], float); sc = np.array([pl[q]['scored'] for q in SEQ], float); pt = np.array([pl[q]['points'] for q in SEQ], float)
    hf = s['anchors_held']['fibers']; fib = {k: (v['off'], v['evaluated']) for k, v in hf.items() if v['evaluated'] > 0}
    hp = {k: ((v['satisfied_area'] if v.get('present', True) else 0.0), v['frozen_area']) for k, v in s['held_patches']['patches'].items()}
    return dict(n025=n025, sc=sc, pt=pt, fib=fib, hp=hp, f025=w['f025'] * 100, held_off=s['anchors_held']['equal_fiber_off_rate'] * 100,
                held_patches=s['held_patches']['fraction'] * 100)

C = [load(p) for p in a.cand]; R = [load(p) for p in a.ref]
# shared index sets
fibs = sorted(set.intersection(*[set(x['fib']) for x in C + R])); pats = sorted(set.intersection(*[set(x['hp']) for x in C + R]))
assert all(len(x['hp']) == len(pats) for x in C + R), 'held-patch populations differ between score files (frozen eval set expected)'
pz = []
for p in pats:
    try: b = json.load(open(f'{a.patch_meta_dir}/{p}/meta.json'))['bbox']; pz.append((b[0][2] + b[1][2]) / 2)
    except Exception as e: raise SystemExit(f'held patch {p}: no bbox metadata in {a.patch_meta_dir} ({e}); spatial blocks need every patch placed')
pz = np.array(pz); order = np.argsort(pz); pblocks = np.array_split(order, 10)

def line_blocks(k):
    if k == 10: return [list(range(i * 5, i * 5 + 5)) for i in range(9)] + [list(range(45, 51))]
    return [list(range(i * 10, i * 10 + 10)) for i in range(4)] + [list(range(40, 51))]

def boot_lines(num, den, blocks, n):
    draws = rng.integers(0, len(blocks), size=(n, len(blocks))); out = np.empty(n)
    for i, d in enumerate(draws):
        sel = np.concatenate([blocks[b] for b in d]); out[i] = np.mean([(nu[sel].sum() / de[sel].sum()) for nu, de in zip(num, den)]) * 100
    return out

def diff_lines(key_num, key_den, blocks, n):
    c = boot_lines([x[key_num] for x in C], [x[key_den] for x in C], blocks, n); rng.bit_generator.state = STATE
    r = boot_lines([x[key_num] for x in R], [x[key_den] for x in R], blocks, n); return c - r

def point_lines(key_num, key_den, sel=None):
    sel = slice(None) if sel is None else sel
    return [(x[key_num][sel].sum() / x[key_den][sel].sum() - y[key_num][sel].sum() / y[key_den][sel].sum()) * 100 for x, y in zip(C, R)]

def fiber_rate(x, idx): return np.mean([x['fib'][fibs[i]][0] / x['fib'][fibs[i]][1] for i in idx]) * 100
def patch_rate(x, idx): s = sum(x['hp'][pats[i]][0] for i in idx); f = sum(x['hp'][pats[i]][1] for i in idx); return s / f * 100

res = {'cand': a.cand, 'ref': a.ref, 'mode': a.mode, 'n_fibers': len(fibs), 'n_patches': len(pats)}
STATE = rng.bit_generator.state
# lines
d = diff_lines('n025', 'sc', line_blocks(10), a.n); res['f025'] = dict(per_seed=point_lines('n025', 'sc'), mean=float(np.mean(point_lines('n025', 'sc'))),
    ci90=[float(np.percentile(d, 5)), float(np.percentile(d, 95))], lower95=float(np.percentile(d, 5)))
rng.bit_generator.state = STATE; d5 = diff_lines('n025', 'sc', line_blocks(5), a.n); res['f025']['ci90_5blocks'] = [float(np.percentile(d5, 5)), float(np.percentile(d5, 95))]
loo = []
for b in line_blocks(10):
    keep = np.array([i for i in range(51) if i not in b]); loo.append(float(np.mean(point_lines('n025', 'sc', keep))))
res['f025']['leave_one_block_out_range'] = [min(loo), max(loo)]
rng.bit_generator.state = STATE; dc = diff_lines('n025', 'pt', line_blocks(10), a.n)
rng.bit_generator.state = STATE; dc5 = diff_lines('n025', 'pt', line_blocks(5), a.n)
looc = [float(np.mean(point_lines('n025', 'pt', np.array([i for i in range(51) if i not in b])))) for b in line_blocks(10)]
res['f025_cens'] = dict(mean=float(np.mean(point_lines('n025', 'pt'))), ci90=[float(np.percentile(dc, 5)), float(np.percentile(dc, 95))],
    ci90_5blocks=[float(np.percentile(dc5, 5)), float(np.percentile(dc5, 95))], leave_one_block_out_range=[min(looc), max(looc)])
def sign_class(ci): return 'positive' if ci[0] > 0 else 'negative' if ci[1] < 0 else 'crossing'
# fibers (whole-fiber bootstrap, paired)
nf = len(fibs); draws = rng.integers(0, nf, size=(a.n, nf))
df = np.array([np.mean([fiber_rate(x, dd) - fiber_rate(y, dd) for x, y in zip(C, R)]) for dd in draws])
res['held_off'] = dict(per_seed=[x['held_off'] - y['held_off'] for x, y in zip(C, R)], per_seed_shared=[fiber_rate(x, range(nf)) - fiber_rate(y, range(nf)) for x, y in zip(C, R)],
    mean=float(np.mean([fiber_rate(x, range(nf)) - fiber_rate(y, range(nf)) for x, y in zip(C, R)])), ci90=[float(np.percentile(df, 5)), float(np.percentile(df, 95))],
    upper95=float(np.percentile(df, 95)), per_fiber=[{ 'fiber': f, 'cand': [x['fib'][f][0] / x['fib'][f][1] * 100 for x in C], 'ref': [y['fib'][f][0] / y['fib'][f][1] * 100 for y in R]} for f in fibs])
# patches (z-block bootstrap, paired)
draws = rng.integers(0, 10, size=(a.n, 10))
dp = np.array([np.mean([patch_rate(x, np.concatenate([pblocks[b] for b in dd])) - patch_rate(y, np.concatenate([pblocks[b] for b in dd])) for x, y in zip(C, R)]) for dd in draws])
res['held_patches'] = dict(per_seed=[x['held_patches'] - y['held_patches'] for x, y in zip(C, R)], mean=float(np.mean([patch_rate(x, range(len(pats))) - patch_rate(y, range(len(pats))) for x, y in zip(C, R)])),
    ci90=[float(np.percentile(dp, 5)), float(np.percentile(dp, 95))], lower95=float(np.percentile(dp, 5)))
# decision
f = res['f025']; h = res['held_off']; p = res['held_patches']
patches_ok = p['mean'] > -2.0 and p['lower95'] > -2.0
if a.mode == 'promote':
    geometry = all(v >= 1.0 for v in f['per_seed']) and f['ci90'][0] > 0 and h['upper95'] < 1.0
    topology = all(v <= -2.0 for v in h['per_seed_shared']) and h['ci90'][1] < 0 and f['lower95'] > -0.5
    stable = sign_class(f['ci90_5blocks']) == sign_class(f['ci90']) and sign_class(f['ci90_5blocks']) != 'crossing'
    res['f025']['sign_10blocks'] = sign_class(f['ci90']); res['f025']['sign_5blocks'] = sign_class(f['ci90_5blocks'])
    verdict = 'PROMOTE (geometry route)' if geometry and patches_ok and stable else 'PROMOTE (topology route)' if topology and patches_ok and stable else 'NOT PROMOTED'
    if verdict == 'NOT PROMOTED' and (geometry or topology): verdict = 'INCONCLUSIVE (spatially unstable or patch guard)'
else:
    eq = -0.5 <= f['ci90'][0] and f['ci90'][1] <= 0.5 and all(v >= 0 for v in p['per_seed']) and h['upper95'] < 1.0
    eq = eq and sign_class(f['ci90_5blocks']) == sign_class(f['ci90'])
    res['f025']['sign_10blocks'] = sign_class(f['ci90']); res['f025']['sign_5blocks'] = sign_class(f['ci90_5blocks'])
    verdict = 'EQUIVALENT (adopt simplification)' if eq else 'NOT EQUIVALENT'
res['verdict'] = verdict
print(json.dumps({k: v for k, v in res.items() if k != 'held_off'} | {'held_off': {k: v for k, v in h.items() if k != 'per_fiber'}}, indent=1))
print('per-fiber held off-rate (cand seeds | ref seeds), worst change first:')
for e in sorted(h['per_fiber'], key=lambda e: -(np.mean(e['cand']) - np.mean(e['ref']))):
    print('  %-40s %s | %s  d=%+.1f' % (e['fiber'], ' '.join('%5.1f' % v for v in e['cand']), ' '.join('%5.1f' % v for v in e['ref']), np.mean(e['cand']) - np.mean(e['ref'])))
print('VERDICT', verdict)
out = a.out or os.path.join(os.path.dirname(a.cand[0]), 'promote_vs_' + os.path.basename(os.path.dirname(a.ref[0])) + '.json')
json.dump(res, open(out, 'w'), indent=1); print('written', out)
