"""Each uncertain rule reading, run through the exact chains at the deployed cost share (random side, swap charged, 3.3526% of risk).
Reports P(pass), P(any payout) at the re-picked funded risk (plateau rule, 0.05% grid), cash per $38 fee and the breakeven honour rate 1/cash_fee."""
import numpy as np
from pa_imp import *
g=PA.cost_share(34.791728647093755)+0.004783927673690548
def thr_factory(cap=0.35,m1_scale=1.0):
    def thr(r,rr,B,split,min_net=100.0,cap_=None):
        return max(m1_scale*min_net/split/B, rr*r/cap)
    return thr
def case(tag,ra=0.03,cu=True,cd=True,wait=7,cap=0.35,m1_scale=1.0,split=0.7):
    e=PA.eval_chain(ra,g*ra,rr=1.0,clip_up=cu,clip_dn=cd)
    best=None; rows=[]
    for r in np.arange(0.008,0.0301,0.0005):
        m=max(m1_scale*100.0/split/3000, r/cap)
        f=PA.funded_chain(r,g*r,m,rr=1.0,clip_dn=cd,wait=wait)
        rows.append({"risk":float(r),"p_any":f["p_any"],"e_W":f["e_W"],"med_first_pay":f["med_first_pay"],"e_life":f["e_life"]})
    plat=PA.plateau(rows); pk=min(plat,key=lambda x:(x['med_first_pay'],-x['e_W']))
    cash=split*3000*e['p_pass']*pk['e_W']
    print(f"{tag:70s} pass {e['p_pass']:.4f}  pick {pk['risk']:.4%} any {pk['p_any']:.4f}  cash/fee {cash/38:.3f}  breakeven honour {38/cash:.1%}",flush=True)
case("baseline")
case("floor NOT enforced on the tick (no stop clip, both stages)",cd=False)
case("target not clipped (win lands past 9%)",cu=False)
case("35% cap read against the NET request (day <= 0.35 x 0.7 x equity)",cap=0.245)
case("35% cap absent in the funded stage",cap=1e9)
case("$100 minimum read before the split (3.33% of bankroll)",m1_scale=0.7)
case("payout clock from assessment opening (wait=1 trade)",wait=1)
case("payout clock 14 trades (the broker map's clock)",wait=14)
case("assessment risk 4% (35% rule absent in the assessment)",ra=0.04)
case("assessment risk 3.15% (the cap)",ra=0.0315)
