"""
s2_after_hold.py -- [2026-10-21] the user: "why didn't the bot buy the winners after a positive confirmation?"
For every base buy S2 held on 2 years (s2v2_test.jsonl), replay that stock-day with S2 ON and record EVERY minute from the
hold to +30 min: is S2 confirmed (all closes since the spike >= its middle AND buyers since >= 55%), what v2 said, and
minutes v2 was not even asked (holding / re-entry rule "only above the last losing buy" / after 15:15).
    nice -n 19 python3 s2_after_hold.py   -> s2_after_hold.log
"""
import gzip
import json
import sys
from collections import Counter, defaultdict
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE)); sys.path.insert(0, "/var/www/screener/trade")
import v2_bar_replay as R  # noqa: E402
import entry_situations as ES  # noqa: E402
import logging  # noqa: E402
logging.getLogger("trade_bot.entry_situations").setLevel(logging.WARNING)
from flush_winners_study import cat  # noqa: E402

CFG = {"S2_spike_confirm": {"enabled": True, "record_only": False}}
CTX = {}
_orig = R.entry_v2.evaluate


def confirmed(view, a2, fm):
    done = view[:-1]
    after = [b for b in done if b["t"] > a2["t"]]
    if not after:
        return False, "no bar after the spike yet", None
    held_mid = min(b["c"] for b in after) >= a2["mid"]
    fl = [fm.get(ES._mod(b["t"])) for b in after]
    bsum, ssum = sum(x[0] for x in fl if x), sum(x[1] for x in fl if x)
    bs = bsum / (bsum + ssum) if bsum + ssum else None
    ok = held_mid and bs is not None and bs >= 0.55
    return ok, ("held the middle" if held_mid else "FELL under the middle") + f", buyers {bs:.0%}" if bs is not None else "", bs


def wrapped(sym, view, ref, f15, pers, as_of=None, cfg=None, quote=None):
    d = _orig(sym, view, ref, f15, pers, as_of=as_of, cfg=cfg, quote=quote)
    m = R.minute(as_of) - 1; day = CTX["day"]
    fb, fs = CTX["flow"]["buy"], CTX["flow"]["sell"]
    fm = {k: (fb[k], fs[k]) for k in range(0, m + 1)}
    ES.record(day, sym, m, d.metrics, passed=bool(d.should_enter))
    v2ok = bool(d.should_enter); s2held = False
    if v2ok:
        tags, det = ES.tag(day, sym, m, view, ref, d.metrics, cfg=CFG, flow_by_min=fm)
        det["_flowm"] = fm
        if ES.check(day, sym, m, view, ref, d.metrics, tags, det, cfg=CFG):
            d.should_enter = False; s2held = True
    a2 = ES._s2.get((day, sym)) or CTX.get("last_a2")
    if ES._s2.get((day, sym)):
        CTX["last_a2"] = ES._s2[(day, sym)]
    conf = confirmed(view, a2, fm) if a2 else (None, "", None)
    CTX["log"][m] = {"v2": v2ok, "s2held": s2held, "conf": conf[0], "conf_txt": conf[1],
                     "reasons": [cat(x) for x in (d.reasons_against or []) if not x.startswith("warning")]}
    return d


R.entry_v2.evaluate = wrapped


def main():
    A = [json.loads(l) for l in open(HERE / "s2v2_test.jsonl")]
    T = [t for t in A if t.get("kind") != "held"]
    k = lambda t: (t["day"], t["sym"], t["buy_m"])
    B = {k(t): t for t in T if t["run"] == "BASE"}; S = {k(t): t for t in T if t["run"] == "S2_ON"}
    gone = sorted([B[x] for x in B if x not in S], key=k)
    newb = defaultdict(list)
    for x in S:
        if x not in B:
            newb[(x[0], x[1])].append(S[x])
    cfg = R.get_config(); ecfg = {**(cfg.get("entry_v2") or {}), "confirm_checks": 1}; xcfg = {**(cfg.get("exit_v2") or {}), "enabled": True}
    rows = []
    byday = defaultdict(list)
    for t in gone:
        byday[t["day"]].append(t)
    for day in sorted(byday):
        d = json.load(gzip.open(R.OW / "bars" / f"bars_{day}.json.gz", "rt")); bench = R.bench_pct(day)
        for t in byday[day]:
            s = t["sym"]; ref = R.REF.load(day, s)
            flow = json.loads((R.OW / "flow" / day / f"{s}.json").read_text())
            CTX.update(day=day, flow=flow, log={}, last_a2=None)
            for z in (ES._hist, ES._pending, ES._streak, ES._c3ep, ES._s1, ES._s2):
                z.clear()
            R.run_stock(day, s, d["symbols"][s]["bars"], flow, ref, bench, ecfg, xcfg)
            lg = CTX["log"]; h0 = t["buy_m"] - 1                  # the minute S2 first held
            win = range(h0, min(h0 + 31, 346))
            conf_m = [m for m in win if lg.get(m) and lg[m]["conf"]]
            conf10 = [m for m in conf_m if m - h0 <= 10]
            later = [u for u in newb.get((day, s), []) if u["buy_m"] > t["buy_m"]]
            first_conf = conf10[0] if conf10 else None
            if later and later[0]["buy_m"] - 1 - h0 <= 30:
                case = "BOUGHT later (within 30 min)"
            elif first_conf is None:
                case = "never confirmed within the 10-min hold"
            else:
                case = "confirmed, but v2 said NO"
            v2_after = Counter(r for m in range(first_conf, min(first_conf + 11, 346)) if first_conf is not None and lg.get(m) and not lg[m]["v2"]
                               for r in lg[m]["reasons"]) if first_conf is not None else Counter()
            not_asked = sum(1 for m in win if m not in lg)
            rows.append({"t": t, "case": case, "first_conf": first_conf, "v2_after": v2_after, "not_asked": not_asked,
                         "conf_txt": lg.get(h0 + 2, {}).get("conf_txt", ""),
                         "later_pl": later[0]["pl_gross"] - 0.2 if later else None, "later_m": later[0]["buy_m"] if later else None})
        R.REF._CACHE.clear(); del d
        print(day, flush=True)
    pl = lambda X: np.array([r["t"]["pl_gross"] - 0.2 for r in X])
    print(f"\n{len(rows)} base buys S2 held (2 years). Base result = what the held buy would have made.\n")
    for lab, sel in (("ALL held", lambda r: True), ("held WINNERS", lambda r: r["t"]["pl_gross"] - 0.2 > 0), ("held LOSERS", lambda r: r["t"]["pl_gross"] - 0.2 <= 0)):
        X = [r for r in rows if sel(r)]
        print(f"{lab}: {len(X)}  (base {pl(X).mean():+.3f}%/trade)")
        for c in ("BOUGHT later (within 30 min)", "never confirmed within the 10-min hold", "confirmed, but v2 said NO"):
            Y = [r for r in X if r["case"] == c]
            if Y:
                lp = [r["later_pl"] for r in Y if r["later_pl"] is not None]
                print(f"   {c:42} {len(Y):3}  base {pl(Y).mean():+.3f}%" + (f"  | the later buy {np.mean(lp):+.3f}%" if lp else ""))
        Z = [r for r in X if r["case"] == "confirmed, but v2 said NO"]
        if Z:
            cc = Counter()
            for r in Z:
                cc.update(set(r["v2_after"]))
            print("      v2 checks that said no in the 10 min after the confirmation (stocks):", ", ".join(f"{a} {b}" for a, b in cc.most_common(8)))
        print()
    print("THE WINNERS one by one:")
    for r in sorted([r for r in rows if r["t"]["pl_gross"] - 0.2 > 0], key=lambda r: -r["t"]["pl_gross"]):
        t = r["t"]; m = t["buy_m"]
        extra = (f"confirmed at {9 + (30 + r['first_conf'] + 1) // 60}:{(30 + r['first_conf'] + 1) % 60:02d}" if r["first_conf"] is not None else f"not confirmed ({r['conf_txt']})")
        v2 = ", ".join(f"{a} {b}m" for a, b in r["v2_after"].most_common(3))
        lb = f" | bought later {9 + (30 + r['later_m']) // 60}:{(30 + r['later_m']) % 60:02d} {r['later_pl']:+.2f}%" if r["later_pl"] is not None else ""
        print(f"   {t['day']} {t['sym']:6} {9 + (30 + m) // 60}:{(30 + m) % 60:02d} base {t['pl_gross'] - 0.2:+.2f}% | {r['case']} | {extra}" + (f" | v2 no: {v2}" if v2 else "") + lb)
    print("S2AFTER_DONE")


if __name__ == "__main__":
    main()
