"""
TARGET D, part 2 — fold the REAL joint growth+lensing sector into the #1 ln B.

Baseline #1 (laplace_evidence_v2 / dfd_likelihoods):
  chi2_min: DFD 1993.2  LCDM 1987.7  (LCDM raw-better by 5.5)
  agnostic ln B = -1.92 ; +SH0ES = +12.58 ; +profiled SH0ES = +9.95
  That baseline used a 1-dof lensing PROXY: DFD 0.04 vs LCDM 0.83 (DFD +0.79 chi2 credit).

Here we REPLACE the proxy with the real native joint growth+lensing sector
(phiphi 9-bin + shear-S8 1 + fsigma8 8) computed in joint_growth_lensing_synthesis,
and report how the #1 ln B moves -- window-only vs +linear-corr.

Delta(chi2) entering ln B  =  chi2_growthlensing(model)  -  proxy(model)
ln B shift = -0.5 * [ Delta_chi2_DFD - Delta_chi2_LCDM ].
"""
import numpy as np
import joint_growth_lensing_synthesis as J

# baseline numbers (frozen, from laplace_evidence_v2 + proxy calc above)
LNB_AGN_BASE   = -1.92      # agnostic prior, CMB+BAO+SNe+lensing-PROXY
LNB_SH0ES_BASE = +12.58     # + SH0ES (naive)
LNB_PROF_BASE  = +9.95      # + profiled SH0ES (referee floor)
PROXY_DFD  = 0.040
PROXY_LCDM = 0.830


def main():
    out = J.main()
    print("\n" + "#" * 84)
    print("# TARGET D part 2 — fold real joint growth+lensing into #1 ln B")
    print("#" * 84)

    for tag, key in [("WINDOW-only phiphi", "phi_win"), ("+LINEAR-corr phiphi", "phi_lin")]:
        gl_dfd  = out["DFD"][key]  + out["DFD"]["c_s8"]  + out["DFD"]["c_fs8"]
        gl_lcdm = out["LCDM"][key] + out["LCDM"]["c_s8"] + out["LCDM"]["c_fs8"]
        # replace proxy (1 dof) with the real 18-dof growth+lensing block
        dchi_dfd  = gl_dfd  - PROXY_DFD
        dchi_lcdm = gl_lcdm - PROXY_LCDM
        # net chi2 change in the joint fit (DFD relative to LCDM)
        net_rel = dchi_dfd - dchi_lcdm
        dlnB = -0.5 * net_rel
        print(f"\n=== {tag} ===")
        print(f"  real growth+lensing chi2:  DFD {gl_dfd:7.2f}   LCDM {gl_lcdm:7.2f}  (net DFD-LCDM {gl_dfd-gl_lcdm:+.2f})")
        print(f"  was (1-dof proxy):         DFD {PROXY_DFD:7.2f}   LCDM {PROXY_LCDM:7.2f}")
        print(f"  Delta chi2 vs proxy:       DFD {dchi_dfd:+7.2f}   LCDM {dchi_lcdm:+7.2f}")
        print(f"  net relative chi2 shift (DFD-LCDM) = {net_rel:+.2f}")
        print(f"  => ln B shift = {dlnB:+.2f}")
        print(f"  NEW ln B (agnostic)        : {LNB_AGN_BASE:+.2f} -> {LNB_AGN_BASE+dlnB:+.2f}")
        print(f"  NEW ln B (+SH0ES naive)    : {LNB_SH0ES_BASE:+.2f} -> {LNB_SH0ES_BASE+dlnB:+.2f}")
        print(f"  NEW ln B (+profiled SH0ES) : {LNB_PROF_BASE:+.2f} -> {LNB_PROF_BASE+dlnB:+.2f}")
        verdict = "NET-HELPS DFD" if dlnB > 0.2 else ("NET-HURTS DFD" if dlnB < -0.2 else "neutral")
        print(f"  => growth+lensing sector {verdict}")


if __name__ == "__main__":
    main()
