"""
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 spike         one of the last 2 finished bars moved > spike_x (2x) the normal 1-min range (avg of the 15 before)
  S3 block_print   one bar holds > 50% of the last 15 minutes' volume
  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 do not buy on the spike: wait `wait` minutes; then buy only if v2 still says BUY and no close fell under the
        spike bar's midpoint                                                                      [T5]
  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, "spike_x": 2.0, "wait": 1},
    "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)
_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 spike
        if avg15 and max(rng[-2:]) > c["S2_spike_confirm"]["spike_x"] * avg15:
            i = len(done) - 1 if rng[-1] >= rng[-2] else len(done) - 2
            tags.append("S2"); d["S2"] = {"bar": i, "x": round(max(rng[-2:]) / avg15, 1), "mid": (done[i]["h"] + done[i]["l"]) / 2}
        # S3 single block print
        v15 = [b["v"] for b in done[-15:]]
        if sum(v15) and max(v15) > 0.5 * sum(v15):
            tags.append("S3")
        # 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")
        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"]
        if s2["enabled"]:
            key = (day, symbol); p = _pending.get(key)
            if p and minute - p["m"] > s2["wait"] + 2:          # stale (v2 stopped passing): forget it
                _pending.pop(key, None); p = None
            if p is None and "S2" in tags:
                _pending[key] = {"m": minute, "mid": details["S2"]["mid"]}
                held.append(f"S2 spike {details['S2']['x']}x normal range: wait {s2['wait']} min for confirmation")
            elif p is not None:
                if minute - p["m"] < s2["wait"]:
                    held.append("S2 spike: still waiting for confirmation")
                else:
                    since = [b["c"] for b in done[-(minute - p["m"]):]]
                    _pending.pop(key, None)
                    if since and min(since) < p["mid"]:
                        held.append("S2 spike: the price fell under the spike bar's midpoint -- not confirmed")
        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 _s1 if k[0] != day]:
        del _s1[k]
    for k in [k for k in _c3ep if k[0] != day]:
        del _c3ep[k]
