"""
entry_situations.py -- [2026-10-20] The user's design: at every v2 buy signal, first RECOGNIZE THE SITUATION the stock is
in, then apply the rule written for THAT situation (not one rule fits all). Catalog: reports/entry_situations.html.
  tag()    -> the situations the stock is in right now (several can apply). Logged on every v2 BUY (metrics
              "situations"); never changes a decision by itself.
  check()  -> the situation rules that are switched ON (config "entry_situations"); they can hold a buy back.
Situations tagged (measurable at the buy, finished bars only):
  S1 flush-leave flip  [2026-10-21, user: "that will be situation S1"] the HPK 10/20 case: v2 was REJECTING the minute
                   before, buy share up >= flip_bs (20 pts) or straightness up >= flip_eff (0.3) vs 1-2 min ago, AND a bar
                   that just left the 15-bar window had >= leave_x (3x) the window's average volume AND it was a SELL FLUSH
                   (down bar, sellers > buyers) -- the pass may come only from that bar leaving the window
  S2 weak spike    [2026-10-21, user: option 1] one of the last 2 finished bars moved > spike_x (2x) the normal 1-min range
                   (avg of the 15 before it) AND it was WEAK: a down bar, or sellers > 60% of it (buy share < weak_bs), or it
                   closed off its high (close in the lower 2/3 of the bar). 2-year study: 106 such buys -0.276% (y1 -0.39 /
                   y2 -0.17), 61% faded under the spike's middle within 2 bars; strong spikes -0.073% (left alone)
  S3 block_print   one bar holds > block_share (50%) of the last 15 minutes' volume (RILY 10/12: one 53,660-share bar
                   flipped vol15 1.37 -> 1.92x). 2 years: 50 buys -0.307% (halves -0.28 / -0.33) vs -0.129% the rest
  S6 extended      > 3% over the open          S7 stretched   > 2% over VWAP          S8 near_res  room to resistance < 5%
  S10 late_move    move from the open > 1.5x the normal move by now
  S11 flush_bounce a DOWN bar with >= flush_x (5x) normal volume in the last flush_window (30) minutes
  S16 lower_low_1030   S17 choppy (>= 3 Vs)   S20 calm (daily ATR < 8%)   S21 thin_or_wide (spread > 0.5% or median
                   shares/min < 1,000)
Rules (all OFF by default):
  S1 -> R1 [2026-10-21] "do the NEW bars pass on their own?": buy share from the flush bar to now (flush INCLUDED) >= bs_min
        (55%) OR price rising (>= +slope_pct over 5 bars WITH buyers >= bs_min over those 5 bars). If neither: noise -> hold. The hold lasts up to hold_max
        minutes after the flip and is re-checked every minute (new real buying outweighs the flush). record_only: log
        "[S1 FLIP NOISE] would hold" and never change the buy. (The old R1 "slope up + volume up" on every flip
        tested negative on 2 years and was removed.)
  S2 -> R2 [2026-10-21] "not now" on a weak spike: hold; every minute re-check -- confirmed when >= 1 bar finished after
        the spike, every close since stayed >= the spike's middle AND buyers of those bars >= bs_min (55%); up to hold_max
        minutes, then v2 decides alone. record_only: log "[S2 WEAK SPIKE] would hold" only. (The old R2 "wait 1-2 min on
        EVERY spike" tested worse on 2 years and was removed.)
  S3 -> R3 [2026-10-22, user] "not now" if the buy passes ONLY because of the block: v2's volume and buy-share checks are
        recomputed WITHOUT the block bar (RVOL so far >= min_rvol, buy share 15 min >= min_buy_share, shares/min 15 >=
        min_spm -- v2's own numbers); hold while any fails, re-checked every minute for up to hold_max (15 = until the
        block leaves the 15-min window), then v2 decides alone. record_only: log "[S3 BLOCK PRINT] would hold" only.
  S6 -> R6 [2026-10-22, user] "not now" while the price is extended over the open: mode "pct" = more than max_pct (3%)
        over the open; mode "atr" = more than max_atr_x (1.0) x the daily ATR % over the open. Re-checked every minute
        (no memory): once the price is back under the line and v2 still says BUY, the buy goes. (v2 lets these through
        only via its confirmed-breakout exception.) record_only: log "[S6 EXTENDED] would hold" only.
  S8 -> R8 [2026-10-22, user] "not now" while the price is close UNDER the 14-day resistance: mode "pct" = room < min_room_pct
        (5%); mode "atr" = room < min_room_atr_x x the daily ATR %. No limit once the price is above the 14-day high (v2
        drops the room check there) -- so the hold ends when the level breaks and v2 still says BUY. record_only: log
        "[S8 UNDER RESISTANCE] would hold" only.
  S11 -> R11 cool-down `cooldown` minutes after the flush AND a higher low (no low under the flush low since) AND the
        price back over the flush bar's high                                                      [T3]
State (per symbol, per day) is kept here: the v2 metrics of the last minutes and pending spike signals.
"""
import logging

log = logging.getLogger("trade_bot.entry_situations")

DEFAULTS = {
    "tag": True,
    "S1_window_flip": {"enabled": False, "record_only": True, "flip_bs": 0.20, "flip_eff": 0.30, "leave_x": 3.0,
                       "bs_min": 0.55, "slope_pct": 0.3, "hold_max": 15},
    "S2_spike_confirm": {"enabled": False, "record_only": True, "spike_x": 2.0, "weak_bs": 0.40, "weak_pos": 0.66,
                         "bs_min": 0.55, "hold_max": 10},
    "S3_block_print": {"enabled": False, "record_only": True, "block_share": 0.50, "min_rvol": 1.5, "min_buy_share": 0.55,
                       "min_spm": 2000, "hold_max": 15},
    "S6_extended": {"enabled": False, "record_only": True, "mode": "pct", "max_pct": 3.0, "max_atr_x": 1.0},
    "S8_resistance": {"enabled": False, "record_only": True, "mode": "pct", "min_room_pct": 5.0, "min_room_atr_x": 1.0},
    "S11_flush_cooldown": {"enabled": False, "flush_x": 5.0, "flush_window": 30, "cooldown": 15},
    # [2026-10-21] S1R: a BUY right after >= min_rejects minutes of REJECT, via a flip -> review price + volume history
    # level "strict" = all 4 checks, "medium" = history + volume + price, "light" = buyers now + price
    # [2026-10-21, user] C3: on EVERY buy signal confirm the last 3 FULL bars: price rising = mode "green3" (the user's
    # definition: each of the 3 bars closed above its own open; other modes: "closes" closes up, "closes_upbars"), buy share not falling (last 3 bars >= the 3 before - bs_tol), volume not falling
    # (last 3 bars >= vol_ratio x the 3 before)
    "C3_confirm": {"enabled": False, "price_mode": "green3", "bs_tol": 0.05, "vol_ratio": 0.9, "max_wait": 10},
    "S1R_flip_after_rejects": {"enabled": False, "min_rejects": 10, "flip_bs": 0.20, "flip_eff": 0.30, "level": "strict",
                               "bs30_min": 0.50, "bs5_min": 0.55},
}
_hist = {}       # (day, symbol) -> {minute: (buy_share15, eff15)}
_pending = {}    # (day, symbol) -> {"m": spike minute, "mid": midpoint}
_streak = {}     # (day, symbol) -> rejection streak (S1R)
_s3 = {}         # (day, symbol) -> active block print {"m": decision minute, "t": block bar time, "v": its volume}
_s2 = {}         # (day, symbol) -> active weak spike {"m": decision minute, "t": spike bar time, "mid": midpoint}
_s1 = {}         # (day, symbol) -> active S1 flip {"m": flip minute, "flush_t": bar time} (hold window)
_c3ep = {}       # (day, symbol) -> C3 "wait_green3" signal window {"start": first BUY minute, "skip": bool}, lives max_wait min


def _cfg(cfg):
    out = {}
    for k, v in DEFAULTS.items():
        out[k] = {**v, **((cfg or {}).get(k) or {})} if isinstance(v, dict) else (cfg or {}).get(k, v)
    return out


def record(day, symbol, minute, metrics, passed=None):
    """call on EVERY v2 evaluation (also REJECT): keeps the recent buy share / straightness and, [2026-10-21] the
    rejection streak (minutes in a row v2 did NOT pass) for S1R."""
    if metrics.get("buy_share15") is None:
        return
    h = _hist.setdefault((day, symbol), {})
    h[minute] = (metrics.get("buy_share15"), metrics.get("eff15"))
    for k in [k for k in h if isinstance(k, int) and k < minute - 5]:
        del h[k]
    if passed is not None:
        st = _streak.setdefault((day, symbol), {"last": None, "n": 0, "prev_n": 0})
        if st["last"] != minute:
            if passed:
                st["prev_n"] = st["n"] if st["n"] else st["prev_n"]
                st["n"] = 0
            else:
                st["n"] += 1; st["prev_n"] = 0
            st["last"] = minute


def _flush(done, ref, n, x):
    vpm = (ref or {}).get("vol_per_min")
    base = len(done) - 1
    best, best_x = None, 0.0
    for i in range(max(0, base - n + 1), base + 1):
        b = done[i]
        norm = vpm[i] if vpm and i < len(vpm) and vpm[i] else None
        if norm and b["c"] < b["o"] and b["v"] >= x * norm and b["v"] / norm > best_x:
            best, best_x = i, b["v"] / norm          # the BIGGEST flush of the window
    return best


def _mod(t):
    """minute of the day since 9:30 ET for a bar time (datetime or epoch seconds)"""
    from datetime import datetime, timezone
    from zoneinfo import ZoneInfo
    if not isinstance(t, datetime):
        t = datetime.fromtimestamp(t, timezone.utc)
    t = t.astimezone(ZoneInfo("America/New_York"))
    return (t.hour - 9) * 60 + t.minute - 30


def _bar_flow(bar, flow_by_min):
    """(buy, sell) of one bar from {minute since 9:30: (buy, sell)}; None if unknown"""
    try:
        return (flow_by_min or {}).get(_mod(bar["t"]))
    except Exception:
        return None


def tag(day, symbol, minute, bars, ref, metrics, cfg=None, quote=None, flow_by_min=None):
    """flow_by_min = {minute since 9:30: (buy, sell)} per-minute flow (S1 sell-flush test); optional"""
    """-> (list of situation names, details dict). `minute` = the decision bar (finished) index since 9:30."""
    c = _cfg(cfg)
    done = bars[:-1] if len(bars) > 1 else bars
    tags, d = [], {}
    try:
        if len(done) < 20:
            return tags, d
        ref = ref or {}
        O = done[0]["o"]; P = done[-1]["c"]
        rng = [b["h"] - b["l"] for b in done]
        avg15 = sum(rng[-17:-2]) / 15 if len(rng) >= 17 else None
        # S1 window flip
        h = _hist.get((day, symbol), {})
        bs, eff = metrics.get("buy_share15"), metrics.get("eff15")
        prev = [h[k] for k in (minute - 1, minute - 2) if k in h]
        if prev and bs is not None and eff is not None:
            dbs = max(bs - p[0] for p in prev if p[0] is not None) if any(p[0] is not None for p in prev) else 0
            deff = max(eff - p[1] for p in prev if p[1] is not None) if any(p[1] is not None for p in prev) else 0
            win = done[-15:]; avgv = sum(b["v"] for b in win) / 15
            edge = done[-17:-14] if len(done) >= 17 else []        # bars at the window edge (flow is clock-based)
            s1 = c["S1_window_flip"]
            was_rej = (_streak.get((day, symbol)) or {}).get("prev_n", 0) >= 1
            if edge and was_rej and (dbs >= s1["flip_bs"] or deff >= s1["flip_eff"]):
                fb = max(edge, key=lambda b: b["v"])
                fl = _bar_flow(fb, flow_by_min)
                sell_flush = fb["c"] < fb["o"] and (fl is None or fl[1] > fl[0])
                if fb["v"] >= s1["leave_x"] * max(avgv, 1) and sell_flush:
                    tags.append("S1")
                    d["S1"] = {"d_buy_share": round(dbs, 3), "d_straight": round(deff, 3), "left_bar_x": round(fb["v"] / max(avgv, 1), 1),
                               "flush_t": fb["t"]}
                    _s1[(day, symbol)] = {"m": minute, "flush_t": fb["t"]}
        # S1R flip after a long rejection
        st = _streak.get((day, symbol)) or {}
        r = c["S1R_flip_after_rejects"]
        if prev and bs is not None and eff is not None and st.get("prev_n", 0) >= r["min_rejects"]:
            dbs2 = max((bs - p[0]) for p in prev if p[0] is not None) if any(p[0] is not None for p in prev) else 0
            deff2 = max((eff - p[1]) for p in prev if p[1] is not None) if any(p[1] is not None for p in prev) else 0
            if dbs2 >= r["flip_bs"] or deff2 >= r["flip_eff"]:
                tags.append("S1R"); d["S1R"] = {"rejected_min": st.get("prev_n"), "d_buy_share": round(dbs2, 3), "d_straight": round(deff2, 3)}
        # S2 weak spike (the biggest qualifying bar of the last 2)
        s2 = c["S2_spike_confirm"]
        best = None
        for lag in (0, 1):
            i = len(done) - 1 - lag
            avg = sum(rng[i - 15:i]) / 15 if i >= 15 else None
            if avg and rng[i] > s2["spike_x"] * avg and (best is None or rng[i] / avg > best[1]):
                best = (i, rng[i] / avg)
        if best:
            i, x = best; b = done[i]
            fl = _bar_flow(b, flow_by_min)
            bbs = fl[0] / (fl[0] + fl[1]) if fl and fl[0] + fl[1] > 0 else None
            pos = (b["c"] - b["l"]) / (b["h"] - b["l"]) if b["h"] > b["l"] else 1.0
            weak = b["c"] < b["o"] or (bbs is not None and bbs < s2["weak_bs"]) or pos < s2["weak_pos"]
            if weak:
                tags.append("S2")
                d["S2"] = {"bar": i, "x": round(x, 1), "mid": (b["h"] + b["l"]) / 2, "t": b["t"],
                           "down": b["c"] < b["o"], "buy_share": None if bbs is None else round(bbs, 2), "close_pos": round(pos, 2)}
                if (_s2.get((day, symbol)) or {}).get("t") != b["t"]:      # a new weak spike (re)starts the hold
                    _s2[(day, symbol)] = {"m": minute, "t": b["t"], "mid": (b["h"] + b["l"]) / 2}
        # S3 single block print
        v15 = [b["v"] for b in done[-15:]]
        if sum(v15) and max(v15) > c["S3_block_print"]["block_share"] * sum(v15):
            bb = max(done[-15:], key=lambda b: b["v"])
            tags.append("S3"); d["S3"] = {"t": bb["t"], "v": bb["v"], "share": round(bb["v"] / sum(v15), 2)}
            if (_s3.get((day, symbol)) or {}).get("t") != bb["t"]:
                _s3[(day, symbol)] = {"m": minute, "t": bb["t"], "v": bb["v"]}
        # S6 / S7 / S8 / S10
        if (P / O - 1) * 100 > 3:
            tags.append("S6")
        if (metrics.get("vwap_pct") or 0) > 2:
            tags.append("S7")
        room = (metrics.get("sr14") or {}).get("dist_to_resistance_pct")
        if room is not None and room < 5:
            tags.append("S8")
            d["S8"] = {"room": round(room, 2), "touches": ((metrics.get("sr14") or {}).get("resistance") or {}).get("touches")}
        if (metrics.get("move_x") or 0) > 1.5:
            tags.append("S10")
        # S11 flush bounce
        s11 = c["S11_flush_cooldown"]
        fi = _flush(done, ref, int(s11["flush_window"]), s11["flush_x"])
        if fi is not None:
            tags.append("S11"); d["S11"] = {"flush_bar": fi, "low": done[fi]["l"], "high": done[fi]["h"], "ago": len(done) - 1 - fi}
        # S16 lower low by 10:30
        if len(done) >= 60 and min(b["l"] for b in done[30:60]) < min(b["l"] for b in done[:30]):
            tags.append("S16")
        # S17 choppy
        H = [b["h"] for b in done]; L = [b["l"] for b in done]; nv = 0
        for j in range(15, len(done) - 3):
            lo = L[j]
            if lo <= min(L[j - 3:j + 4]) and L[j - 1] != lo and max(H[max(0, j - 30):j]) / lo - 1 >= 0.01:
                nv += 1
        if nv >= 3:
            tags.append("S17")
        # S20 calm, S21 thin / wide
        if ref.get("atr14") and ref.get("prev_close") and ref["atr14"] / ref["prev_close"] * 100 < 8:
            tags.append("S20")
        med = sorted(v15)[7] if len(v15) == 15 else None
        spr = metrics.get("spread_pct")
        if (spr is not None and spr > 0.5) or (med is not None and med < 1000):
            tags.append("S21")
    except Exception as e:
        log.warning(f"[SITUATION] {symbol}: tag failed ({e})")
    return tags, d


def check(day, symbol, minute, bars, ref, metrics, tags, details, cfg=None):
    """-> list of 'situation rule: reason' that HOLD the buy back (only rules switched ON)."""
    c = _cfg(cfg)
    done = bars[:-1] if len(bars) > 1 else bars
    held = []
    try:
        s1 = c["S1_window_flip"]
        a1 = _s1.get((day, symbol))
        if a1 and minute - a1["m"] > s1["hold_max"]:
            _s1.pop((day, symbol), None); a1 = None
        if a1 and (s1["enabled"] or s1["record_only"]):
            # do the NEW bars pass on their own? buy share from the flush bar to now, flush INCLUDED
            fbm = (details or {}).get("_flowm") or {}
            since = [b for b in done if b["t"] >= a1["flush_t"]]
            fl = [fbm.get(_mod(b["t"])) for b in since]
            bsum, ssum = sum(x[0] for x in fl if x), sum(x[1] for x in fl if x)
            known = sum(1 for x in fl if x)
            bs_incl = bsum / (bsum + ssum) if bsum + ssum and known >= max(1, len(fl) / 2) else None
            cl = [b["c"] for b in done[-6:]]
            f5 = [fbm.get(_mod(b["t"])) for b in done[-5:]]
            b5, s5 = sum(x[0] for x in f5 if x), sum(x[1] for x in f5 if x)
            bs5 = b5 / (b5 + s5) if b5 + s5 else None
            # [2026-10-21, HPK] price rising counts only WITH buyers behind it (HPK 10:00-10:05: +0.45% drift, buyers 38%)
            rising = len(cl) == 6 and (cl[-1] / cl[0] - 1) * 100 >= s1["slope_pct"] and bs5 is not None and bs5 >= s1["bs_min"]
            real = (bs_incl is not None and bs_incl >= s1["bs_min"]) or rising
            if not real:
                why = (f"S1 flush-leave flip: the pass comes from the sell flush leaving the window -- buyers incl. the flush "
                       f"{'?' if bs_incl is None else f'{bs_incl:.0%}'} < {s1['bs_min']:.0%}, price not rising "
                       f"({(cl[-1] / cl[0] - 1) * 100 if len(cl) == 6 else 0:+.2f}% over 5 bars, buyers {'?' if bs5 is None else f'{bs5:.0%}'})")
                if s1["enabled"] and not s1["record_only"]:
                    held.append(why)
                else:
                    log.info(f"[S1 FLIP NOISE] {symbol}: would hold (record only) -- {why}")
                    if details is not None:
                        details["S1_would_hold"] = why
            else:
                _s1.pop((day, symbol), None)                 # confirmed by the new bars: S1 is over for this flip
        c3 = c["C3_confirm"]
        if c3["enabled"]:
            last3, prev3 = done[-3:], done[-6:-3]
            cl = [b["c"] for b in done[-4:]]
            closes_up = len(cl) == 4 and cl[1] > cl[0] and cl[2] > cl[1] and cl[3] > cl[2]
            up_bars = sum(1 for b in last3 if b["c"] > b["o"])
            if c3["price_mode"] == "wait_green3":
                # [2026-10-21, user OK] refined C3 from the 6-month test: at the FIRST buy of a v2 episode, if the last 3
                # bars are already 3 up bars -> the move is stretched: skip the whole episode. Otherwise wait (<= max_wait
                # min) and buy at the first minute whose last 3 full bars are 3 NEW up bars formed after the signal.
                key = (day, symbol)
                ep = _c3ep.get(key)
                green = len(last3) == 3 and up_bars == 3
                # the episode lives max_wait minutes from its first BUY, even if v2 flickers off and on (most v2 BUYs last 1-2 min)
                if ep is None or minute - ep["start"] > c3.get("max_wait", 10):
                    _c3ep[key] = ep = {"start": minute, "skip": green}
                    price_ok = False
                    held.append("C3 wait: " + ("3 up bars ALREADY at the signal -- stretched, skip this signal"
                                               if green else "waiting for 3 new up bars"))
                elif ep["skip"]:
                    price_ok = False
                    held.append("C3 wait: this signal was skipped (stretched at the start)")
                else:
                    price_ok = green and minute - ep["start"] >= 3     # 3 bars finished since the signal = all 3 are new
                    if not price_ok:
                        held.append("C3 wait: still waiting for 3 new up bars")
            elif c3["price_mode"] == "green3":
                # [2026-10-21, the user's definition] each of the last 3 full bars closed ABOVE ITS OWN OPEN (3 up bars)
                price_ok = len(last3) == 3 and up_bars == 3
            else:
                price_ok = closes_up and (c3["price_mode"] == "closes" or up_bars >= 2)
            f3 = (details or {}).get("_flow3")            # (buy last 3, sell last 3, buy prev 3, sell prev 3)
            bs_ok = True
            if f3 and (f3[0] + f3[1]) > 0 and (f3[2] + f3[3]) > 0:
                bs_ok = f3[0] / (f3[0] + f3[1]) >= f3[2] / (f3[2] + f3[3]) - c3["bs_tol"]
            v3, vp3 = sum(b["v"] for b in last3), sum(b["v"] for b in prev3)
            vol_ok = vp3 <= 0 or v3 >= c3["vol_ratio"] * vp3
            if c3["price_mode"] == "wait_green3" and not price_ok:
                pass                                       # already explained above
            elif not (price_ok and bs_ok and vol_ok):
                held.append(f"C3 last 3 bars: price {'rising' if price_ok else 'NOT rising'} (up bars {up_bars}/3), buy share "
                            f"{'stable' if bs_ok else 'FALLING'}, volume {'stable' if vol_ok else 'FALLING'}")
        r = c["S1R_flip_after_rejects"]
        if "S1R" in tags and r["enabled"]:
            fm = (details or {}).get("_flow")            # (buy per minute, sell per minute) up to the decision bar
            bs30, bs5 = (details or {}).get("_bs30"), (details or {}).get("_bs5")   # live: from the stream
            if fm:
                b30, s30 = sum(fm[0][-30:]), sum(fm[1][-30:]); b5, s5 = sum(fm[0][-5:]), sum(fm[1][-5:])
                bs30 = b30 / (b30 + s30) if b30 + s30 else None; bs5 = b5 / (b5 + s5) if b5 + s5 else None
            vpm = (ref or {}).get("vol_per_min") or []
            v5 = sum(b["v"] for b in done[-5:]); norm5 = sum(vpm[max(0, minute - 4):minute + 1]) if vpm else 0
            hist_ok = bs30 is not None and bs30 >= r["bs30_min"]
            now_ok = bs5 is not None and bs5 >= r["bs5_min"]
            vol_ok = (not norm5) or v5 >= norm5
            cl = [b["c"] for b in done]; lo = [b["l"] for b in done]
            price_ok = (len(cl) >= 20 and cl[-1] > max(cl[-17:-2]) and min(lo[-10:]) > min(lo[-20:-10]))
            need = {"strict": (hist_ok, now_ok, vol_ok, price_ok), "medium": (hist_ok, vol_ok, price_ok),
                    "light": (now_ok, price_ok)}[r["level"]]
            if not all(need):
                held.append(f"S1R flip after {d_s1r(details)} min of rejects: not a confirmed reversal "
                            f"(30-min buyers {'ok' if hist_ok else 'no'}, 5-min buyers {'ok' if now_ok else 'no'}, "
                            f"volume {'ok' if vol_ok else 'no'}, price breakout + higher low {'ok' if price_ok else 'no'})")
        s2 = c["S2_spike_confirm"]
        a2 = _s2.get((day, symbol))
        if a2 and minute - a2["m"] > s2["hold_max"]:
            _s2.pop((day, symbol), None); a2 = None
        if a2 and (s2["enabled"] or s2["record_only"]):
            fbm = (details or {}).get("_flowm") or {}
            after = [b for b in done if b["t"] > a2["t"]]
            if s2.get("confirm_mode", "all") == "last":   # [2026-10-22 test] the latest close back over the middle (a dip is allowed)
                held_mid = bool(after) and after[-1]["c"] >= a2["mid"]
            else:                                          # "all": every close since the spike stayed over the middle
                held_mid = bool(after) and min(b["c"] for b in after) >= a2["mid"]
            fl = [fbm.get(_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_after = bsum / (bsum + ssum) if bsum + ssum else None
            ok = held_mid and bs_after is not None and bs_after >= s2["bs_min"]
            if not ok:
                why = (f"S2 weak spike: not confirmed yet -- {len(after)} bar(s) since the spike, "
                       f"{'held' if held_mid else 'NOT held' if after else 'none yet over'} the spike's middle {a2['mid']:.4g}, "
                       f"buyers since {'?' if bs_after is None else f'{bs_after:.0%}'} (need {s2['bs_min']:.0%})")
                if s2["enabled"] and not s2["record_only"]:
                    held.append(why)
                else:
                    log.info(f"[S2 WEAK SPIKE] {symbol}: would hold (record only) -- {why}")
                    if details is not None:
                        details["S2_would_hold"] = why
            else:
                _s2.pop((day, symbol), None)                 # confirmed by the bars after the spike
        s3 = c["S3_block_print"]
        a3 = _s3.get((day, symbol))
        if a3 and minute - a3["m"] > s3["hold_max"]:
            _s3.pop((day, symbol), None); a3 = None
        if a3 and (s3["enabled"] or s3["record_only"]):
            fbm = (details or {}).get("_flowm") or {}
            mb = _mod(a3["t"])
            in_win = mb >= minute - 14
            vol, cum = metrics.get("vol_today"), metrics.get("ref_cum_vol")
            rvol_ex = (vol - a3["v"]) / max(cum, 1) if vol is not None and cum else None
            fl15 = [fbm.get(k) for k in range(minute - 14, minute + 1)]
            B15, S15 = sum(x[0] for x in fl15 if x), sum(x[1] for x in fl15 if x)
            fb = fbm.get(mb) if in_win else None
            if fb:
                B15, S15 = B15 - fb[0], S15 - fb[1]
            bs_ex = B15 / (B15 + S15) if B15 + S15 > 0 else None
            v15 = sum(b["v"] for b in done[-15:]) - (a3["v"] if in_win else 0)
            spm_ex = v15 / 14 if in_win else v15 / 15
            fails = []
            if rvol_ex is not None and rvol_ex < s3["min_rvol"]:
                fails.append(f"RVOL {rvol_ex:.2f}x < {s3['min_rvol']}x")
            if bs_ex is not None and bs_ex < s3["min_buy_share"]:
                fails.append(f"buy share {bs_ex:.0%} < {s3['min_buy_share']:.0%}")
            if spm_ex < s3["min_spm"]:
                fails.append(f"{spm_ex:.0f} shares/min < {s3['min_spm']}")
            if s3.get("mode", "fails_without") == "while_in_window":
                # [2026-10-22 test] the block itself is the warning: hold EVERY buy while it is inside the 15-min window
                fails = [f"the block is still inside the 15-min window ({minute - mb} min ago)"] if in_win else []
            if fails:
                why = f"S3 block print ({a3['v']:.0f} shares): " + ", ".join(fails)
                if s3["enabled"] and not s3["record_only"]:
                    held.append(why)
                else:
                    log.info(f"[S3 BLOCK PRINT] {symbol}: would hold (record only) -- {why}")
                    if details is not None:
                        details["S3_would_hold"] = why
            else:
                _s3.pop((day, symbol), None)                 # passes on its own without the block
        s6 = c["S6_extended"]
        if (s6["enabled"] or s6["record_only"]) and done:
            ext = metrics.get("ext_open_pct")
            if ext is None:
                ext = (done[-1]["c"] / done[0]["o"] - 1) * 100
            atrp = (ref or {}).get("atr14") / (ref or {}).get("prev_close") * 100 if (ref or {}).get("atr14") and (ref or {}).get("prev_close") else None
            if s6["mode"] == "atr" and atrp:
                line, lab = s6["max_atr_x"] * atrp, f"{s6['max_atr_x']:g} x daily ATR ({atrp:.1f}%)"
            else:
                line, lab = s6["max_pct"], f"{s6['max_pct']:g}%"
            if ext > line:
                why = f"S6 extended: {ext:+.1f}% over the open > {lab} -- wait for the price to come back under {line:.1f}%"
                if s6["enabled"] and not s6["record_only"]:
                    held.append(why)
                else:
                    log.info(f"[S6 EXTENDED] {symbol}: would hold (record only) -- {why}")
                    if details is not None:
                        details["S6_would_hold"] = why
        s8 = c["S8_resistance"]
        sp = metrics.get("sr14") or {}
        room = sp.get("dist_to_resistance_pct")
        if (s8["enabled"] or s8["record_only"]) and room is not None and not sp.get("above_14d_high"):
            atrp = (ref or {}).get("atr14") / (ref or {}).get("prev_close") * 100 if (ref or {}).get("atr14") and (ref or {}).get("prev_close") else None
            if s8["mode"] == "atr" and atrp:
                need, lab = s8["min_room_atr_x"] * atrp, f"{s8['min_room_atr_x']:g} x daily ATR ({atrp:.1f}%)"
            else:
                need, lab = s8["min_room_pct"], f"{s8['min_room_pct']:g}%"
            if room < need:
                res = sp.get("resistance") or {}
                why = (f"S8 under resistance: only {room:.2f}% room to {res.get('lo', 0):.4g} ({res.get('touches', '?')} touches) "
                       f"< {lab} -- wait for the break above it")
                if s8["enabled"] and not s8["record_only"]:
                    held.append(why)
                else:
                    log.info(f"[S8 UNDER RESISTANCE] {symbol}: would hold (record only) -- {why}")
                    if details is not None:
                        details["S8_would_hold"] = why
        if "S11" in tags and c["S11_flush_cooldown"]["enabled"]:
            s11 = c["S11_flush_cooldown"]; f = details["S11"]
            after = done[f["flush_bar"] + 1:]
            higher_low = bool(after) and min(b["l"] for b in after) > f["low"]
            if f["ago"] < s11["cooldown"] or not higher_low or done[-1]["c"] <= f["high"]:
                held.append(f"S11 flush {f['ago']} min ago: cool-down {s11['cooldown']} min, higher low "
                            f"{'yes' if higher_low else 'no'}, back over the flush high {'yes' if done[-1]['c'] > f['high'] else 'no'}")
    except Exception as e:
        log.warning(f"[SITUATION] {symbol}: check failed ({e}); buy not affected")
        return []
    return held


def d_s1r(details):
    return ((details or {}).get("S1R") or {}).get("rejected_min", "?")


def new_day(day):
    for k in [k for k in _hist if k[0] != day]:
        del _hist[k]
    for k in [k for k in _pending if k[0] != day]:
        del _pending[k]
    for k in [k for k in _streak if k[0] != day]:
        del _streak[k]
    for k in [k for k in _s3 if k[0] != day]:
        del _s3[k]
    for k in [k for k in _s2 if k[0] != day]:
        del _s2[k]
    for k in [k for k in _s1 if k[0] != day]:
        del _s1[k]
    for k in [k for k in _c3ep if k[0] != day]:
        del _c3ep[k]

# [2026-10-22, user] "any situation that is already covered by V2 should be created as V2": these situations have
# their rule inside entry_v2 (the v2 check IS the situation rule). v2_cover() names the situation behind each v2 'no';
# the monitor logs "[S7 V2] ..." when v2's ONLY objections are covered situations (v2 held the buy for that situation).
V2_COVERED = [
    # (situation, name, test on the v2 reason text, v2 rule)
    ("S7", "stretched above VWAP", lambda r: r.startswith("VWAP:") and not r.startswith("VWAP: -"), "VWAP 0..+2%"),
    ("S14", "below VWAP / VWAP reclaim", lambda r: r.startswith("VWAP: -"), "VWAP >= 0%"),
    ("S6", "extended above the open", lambda r: r.startswith("extended:") and "over the open" in r, "max 3% over the open (breakout exception)"),
    ("S8", "close under resistance", lambda r: r.startswith("2 resistance"), "room >= 1% to resistance / day high"),
    ("S12", "opening minutes (spike then collapse)", lambda r: r.endswith(" bars") and " of " in r, "15-bar wait"),
    ("S15", "falling on net selling", lambda r: r.startswith("3 buy/sell"), "buy share >= 55%, not falling on net selling"),
    ("S21", "thin or wide spread", lambda r: r.startswith("spread:") or r.startswith("liquidity:"), "spread <= 1%, >= 2,000 shares/min"),
]


def v2_cover(reasons):
    """-> (list of (situation, name, v2 rule, reason) for every v2 'no' a covered situation explains,
           True if EVERY non-warning v2 'no' is a covered situation)"""
    out, all_cov = [], True
    for r in reasons or []:
        if r.startswith("warning") or r.startswith("confirming"):
            continue
        hit = next(((sid, nm, rule) for sid, nm, test, rule in V2_COVERED if test(r)), None)
        if hit:
            out.append((hit[0], hit[1], hit[2], r))
        else:
            all_cov = False
    return out, (all_cov and bool(out))

