"""
v_styles_report.py -- [2026-10-11] The user: how many STYLES of V paths do we have? A report per style with a chart
of the LAST stock that traded like that and all indicator values, including buying and selling volume (example the
user gave: spike at the open, collapse right away, V, then recovery).
Data: the 2 years with trade-by-trade buy/sell flow (reports/open_window/bars + flow, the daily top-30 lists,
Oct 2024 - Oct 2026). One V per stock-day = the user's DEEP V (analyze_trades.deep_v_entry: the deepest V from 9:45,
>= 1% drop, >= 1% rebound holding the low; turn bar = the right entry; right sell = highest close before the low breaks).
STYLE = path BEFORE the V (known at the V) x what happened AFTER (hindsight):
  before  A  Opening spike, then collapse   high in the first 15 min >= +2% over the open and still the day high
                                            when the V forms; the V low >= 3% under that spike
          B  Straight down from the open    never more than +1% over the open before the V
          C  Rally first, then sell-off     the high before the V made after 10:00 and >= +2% over the open
          D  Chop, then flush               everything else
  after   recovery of the WHOLE fall = from the day high before the V down to the V low (until the low breaks):
          1  New day high                   took back >= 100% (went above the day high made before the V)
          2  Strong recovery                took back 50-100%
          3  Partial recovery               took back 25-50%
          4  Weak bounce                    took back < 25%
Indicators at the TURN (the moment of the right entry): gap, daily ATR %, volume so far / prior day, vs VWAP,
vs open, buy share 15 min before the low, buy share low -> turn, selling dried up, volume at the low, net flow 15 min.
    python3 v_styles_report.py            -> v_styles_rows.jsonl + reports/<date>_v_styles.pdf
"""
import gzip
import json
import sys
from collections import Counter, defaultdict
from datetime import datetime, timezone
from pathlib import Path
from zoneinfo import ZoneInfo

import numpy as np

HERE = Path(__file__).resolve().parent
OW = HERE.parent / "open_window"
sys.path.insert(0, str(HERE)); sys.path.insert(0, str(OW)); sys.path.insert(0, "/var/www/screener/trade")
from analyze_trades import deep_v_entry  # noqa: E402
from up2_styles_build import arrays  # noqa: E402
import reference as REF  # noqa: E402

ET = ZoneInfo("America/New_York")
ROWS = HERE / "v_styles_rows.jsonl"
PDF = HERE.parent / "2026-10-11_v_styles.pdf"
PRE = {"A": "Opening spike, then collapse", "B": "Straight down from the open", "C": "Rally first, then sell-off",
       "D": "Chop, then flush"}
POST = {"1": "new day high", "2": "strong recovery", "3": "partial recovery", "4": "weak bounce"}
hm = lambda m: f"{9 + (30 + m) // 60}:{(30 + m) % 60:02d}"
sh = lambda b, s: b / (b + s) if b + s > 0 else float("nan")


def classify(a, j, sell_i):
    O, H, L, C = a[0][0], a[1], a[2], a[3]
    lo = L[j]
    hi_b = H[:j].max(); hi_b_m = int(np.argmax(H[:j]))
    spike = H[:15].max()
    if spike >= O * 1.02 and hi_b_m < 15 and lo <= spike * 0.97:
        pre = "A"
    elif hi_b < O * 1.01:
        pre = "B"
    elif hi_b_m >= 30 and hi_b >= O * 1.02:
        pre = "C"
    else:
        pre = "D"
    prior = H[max(0, j - 30):j].max()
    after = H[j + 1:386].max() if j + 1 < 386 else lo
    # the bounce = up to the right sell (before the low breaks)
    seg_end = sell_i + 1
    top = H[j + 1:seg_end + 1].max() if seg_end > j + 1 else after
    ret = (top - lo) / (hi_b - lo) if hi_b > lo else 0          # share of the whole fall taken back
    post = "1" if top > hi_b else "2" if ret >= 0.5 else "3" if ret >= 0.25 else "4"
    return pre, post, prior, hi_b, spike


def build():
    out = open(ROWS, "w")
    for p in sorted(OW.glob("bars/bars_20*.json.gz")):
        day = p.name[5:15]
        d = json.load(gzip.open(p, "rt"))
        for s in d["top30_current"]:
            fp = OW / "flow" / day / f"{s}.json"
            if s not in d["symbols"] or not fp.exists():
                continue
            a = arrays(d["symbols"][s]["bars"])
            if a is None:
                continue
            B = [{"m": m, "h": float(a[1, m]), "l": float(a[2, m]), "c": float(a[3, m])} for m in range(390)]
            dv = deep_v_entry(B, None)
            if not dv:
                continue
            j, q, sl, g = dv
            fl = json.loads(fp.read_text()); bu, se = np.array(fl["buy"], float), np.array(fl["sell"], float)
            O = float(a[0, 0]); V = a[4]
            pre, post, prior, hi_b, spike = classify(a, j, sl)
            ref = REF.load(day, s) or {}
            pc = ref.get("prev_close"); atr = ref.get("atr14") or d["symbols"][s]["levels"].get("daily_atr")
            cv = V[:q + 1].sum(); vw = ((a[1, :q + 1] + a[2, :q + 1] + a[3, :q + 1]) / 3 * V[:q + 1]).sum() / cv if cv else a[3, q]
            pre10 = slice(max(0, j - 10), j)
            r = {"day": day, "sym": s, "style": pre + post, "v_m": j, "turn_m": q, "sell_m": sl,
                 "v_low": float(a[2, j]), "turn_px": float(a[3, q]), "sell_px": float(a[3, sl]), "open": O,
                 "drop": (prior / a[2, j] - 1) * 100, "v_vs_open": (a[2, j] / O - 1) * 100,
                 "spike": (spike / O - 1) * 100, "hi_before": (hi_b / O - 1) * 100,
                 "gain": g * 100, "close_vs_turn": (a[3, 385] / a[3, q] - 1) * 100,
                 "held": bool(a[2, j + 1:386].min() >= a[2, j]) if j + 1 < 386 else True,
                 "gap": (O / pc - 1) * 100 if pc else None, "atr_pct": atr / pc * 100 if atr and pc else None,
                 "rvol": cv / d["symbols"][s]["baseline"] if d["symbols"][s].get("baseline") else None,
                 "vwap": (a[3, q] / vw - 1) * 100,
                 "bs_pre15": sh(bu[max(0, j - 14):j + 1].sum(), se[max(0, j - 14):j + 1].sum()),
                 "bs_turn": sh(bu[j:q + 1].sum(), se[j:q + 1].sum()),
                 "sell_dry": float(se[j:q + 1].mean() / se[pre10].mean()) if se[pre10].mean() > 0 else None,
                 "vol_low": float(V[j:q + 1].mean() / V[pre10].mean()) if V[pre10].mean() > 0 else None,
                 "net15": float((bu[max(0, q - 14):q + 1].sum() - se[max(0, q - 14):q + 1].sum())
                                / max(bu[max(0, q - 14):q + 1].sum() + se[max(0, q - 14):q + 1].sum(), 1))}
            out.write(json.dumps({k: (round(v, 4) if isinstance(v, float) else v) for k, v in r.items()}) + "\n")
        del d
        print(day, flush=True)
    out.close()


def chart(ax1, ax2, r):
    d = json.load(gzip.open(OW / "bars" / f"bars_{r['day']}.json.gz", "rt"))
    a = arrays(d["symbols"][r["sym"]]["bars"]); del d
    fl = json.loads((OW / "flow" / r["day"] / f"{r['sym']}.json").read_text())
    x = np.arange(390); O, H, L, C, V = a
    cv = np.cumsum(V); vw = np.cumsum((H + L + C) / 3 * V) / np.where(cv > 0, cv, np.nan)
    ax1.vlines(x, L, H, color="#9aa4b2", lw=0.6)
    ax1.plot(x, C, color="#1f3b5c", lw=1.0, label="close")
    ax1.plot(x, vw, color="#e08a00", lw=1.0, label="VWAP")
    ax1.axhline(O[0], color="#777", ls="--", lw=0.8, label=f"open {O[0]:.2f}")
    ax1.scatter([r["v_m"]], [r["v_low"]], color="#c62828", zorder=5, s=40, label=f"V low {hm(r['v_m'])}")
    ax1.scatter([r["turn_m"]], [r["turn_px"]], color="#2e7d32", marker="^", zorder=5, s=55, label=f"turn {hm(r['turn_m'])}")
    ax1.scatter([r["sell_m"]], [r["sell_px"]], color="#6a1b9a", marker="v", zorder=5, s=55, label=f"right sell {hm(r['sell_m'])}")
    ax1.set_title(f"{r['sym']}  {r['day']}", fontsize=10, loc="left")
    ax1.legend(fontsize=6.5, loc="best", ncol=2); ax1.grid(alpha=0.25)
    ticks = [0, 30, 90, 150, 210, 270, 330, 389]
    ax1.set_xticks(ticks); ax1.set_xticklabels([hm(t) for t in ticks], fontsize=7); ax1.tick_params(labelsize=7)
    bu, se = np.array(fl["buy"]), np.array(fl["sell"])
    ax2.bar(x, bu / 1e3, color="#2e7d32", width=1.0, label="buy volume")
    ax2.bar(x, -se / 1e3, color="#c62828", width=1.0, label="sell volume")
    ax2.axvline(r["v_m"], color="#c62828", lw=0.7, ls=":"); ax2.axvline(r["turn_m"], color="#2e7d32", lw=0.7, ls=":")
    ax2.set_ylabel("k shares", fontsize=7); ax2.legend(fontsize=6.5, loc="upper right"); ax2.grid(alpha=0.25)
    ax2.set_xticks(ticks); ax2.set_xticklabels([hm(t) for t in ticks], fontsize=7); ax2.tick_params(labelsize=7)


def fmt(v, f="{:+.2f}"):
    return "-" if v is None or v != v else f.format(v)


def make_pdf():
    import matplotlib
    matplotlib.use("Agg")
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_pdf import PdfPages
    R = [json.loads(l) for l in open(ROWS)]
    days = sorted({r["day"] for r in R}); nd = len(days)
    cnt = Counter(r["style"] for r in R)
    med = lambda X, k: np.nanmedian([x[k] for x in X if x.get(k) is not None]) if X else float("nan")
    with PdfPages(PDF) as pdf:
        fig = plt.figure(figsize=(8.27, 11.69)); fig.text(0.07, 0.95, "V path styles -- 2 years", fontsize=16, weight="bold")
        lines = [f"{len(R)} deep Vs (the user's rule, the deepest per stock-day) on {nd} days, {days[0]} .. {days[-1]};",
                 "the daily top-30 lists with trade-by-trade buy/sell flow. HINDSIGHT classification:",
                 "'before' is known at the V, 'after' only at the close. One V per stock-day.", "",
                 "BEFORE the V:   A Opening spike, then collapse   B Straight down from the open",
                 "                C Rally first, then sell-off    D Chop, then flush",
                 "AFTER the V (share of the whole fall from the day high before the V taken back):",
                 "                1 new day high  2 strong recovery (50-100%)  3 partial (25-50%)  4 weak bounce (< 25%)"]
        for i, t in enumerate(lines):
            fig.text(0.07, 0.91 - i * 0.022, t, fontsize=9, family="monospace")
        y = 0.67
        for title, cell in (("Vs per day (count)", lambda X: f"{len(X) / nd:4.1f} ({len(X):4})"),
                            ("median gain turn -> right sell", lambda X: f"{med(X, 'gain'):+5.1f}%"),
                            ("low held to the close", lambda X: f"{np.mean([r['held'] for r in X]) if X else 0:4.0%}"),
                            ("close above the turn price", lambda X: f"{np.mean([r['close_vs_turn'] > 0 for r in X]) if X else 0:4.0%}")):
            fig.text(0.07, y, title, fontsize=8.5, weight="bold")
            y -= 0.022
            fig.text(0.07, y, f"{'':31}" + "".join(f"{POST[k]:>17}" for k in "1234"), fontsize=7.2, family="monospace")
            for pk in "ABCD":
                y -= 0.018
                fig.text(0.07, y, f"{pk} {PRE[pk]:29}" + "".join(f"{cell([r for r in R if r['style'] == pk + qk]):>17}" for qk in "1234"),
                         fontsize=7.2, family="monospace")
            y -= 0.035
        y -= 0.04
        fig.text(0.07, y, "Note (tests 10/8-10/10): bought causally at the confirmation, no V style showed a profit after costs;",
                 fontsize=8, style="italic")
        fig.text(0.07, y - 0.02, "this report describes the paths, it is not a trading rule.", fontsize=8, style="italic")
        pdf.savefig(fig); plt.close(fig)
        IND = [("gap", "gap %"), ("atr_pct", "daily ATR %"), ("spike", "first 15 min high vs open %"),
               ("hi_before", "day high before the V vs open %"), ("drop", "drop into the V %"), ("v_vs_open", "V low vs open %"),
               ("rvol", "volume so far / prior day (at turn)"), ("vwap", "vs VWAP at turn %"), ("bs_pre15", "buy share 15 min before the low"),
               ("bs_turn", "buy share low -> turn"), ("sell_dry", "sell vol low->turn / 10 min before"),
               ("vol_low", "volume low->turn / 10 min before"), ("net15", "net flow 15 min at turn"),
               ("gain", "gain turn -> right sell %"), ("close_vs_turn", "close vs turn price %")]
        for st in sorted(cnt, key=lambda k: (k[0], k[1])):
            X = [r for r in R if r["style"] == st]
            last = max(X, key=lambda r: (r["day"], r["v_m"]))
            fig = plt.figure(figsize=(8.27, 11.69))
            fig.text(0.07, 0.96, f"Style {st}: {PRE[st[0]]} -> {POST[st[1]]}", fontsize=13, weight="bold")
            tdist = Counter("9:45-11" if r["v_m"] < 90 else "11-13" if r["v_m"] < 210 else "13-15:15" for r in X)
            fig.text(0.07, 0.935, f"{len(X)} cases ({len(X) / nd:.1f} per day, {len(X) / len(R):.0%} of all deep Vs) | low held to close "
                     f"{np.mean([r['held'] for r in X]):.0%} | V time: " + ", ".join(f"{k} {v / len(X):.0%}" for k, v in sorted(tdist.items())),
                     fontsize=8)
            fig.text(0.07, 0.905, f"{'indicator':38}{'this style (median)':>20}{'all Vs (median)':>18}{'last example':>16}",
                     fontsize=7.5, family="monospace", weight="bold")
            for i, (k, lab) in enumerate(IND):
                fig.text(0.07, 0.885 - i * 0.0165, f"{lab:38}{fmt(med(X, k)):>20}{fmt(med(R, k)):>18}{fmt(last.get(k)):>16}",
                         fontsize=7.5, family="monospace")
            ax1 = fig.add_axes([0.08, 0.29, 0.88, 0.32]); ax2 = fig.add_axes([0.08, 0.07, 0.88, 0.18], sharex=ax1)
            chart(ax1, ax2, last)
            fig.text(0.08, 0.63, f"Last example: {last['sym']} {last['day']} -- V {hm(last['v_m'])} at {last['v_low']:.2f}, "
                     f"turn {hm(last['turn_m'])} {last['turn_px']:.2f}, right sell {hm(last['sell_m'])} {last['sell_px']:.2f} "
                     f"({last['gain']:+.2f}%)", fontsize=8)
            pdf.savefig(fig); plt.close(fig)
    print("PDF ->", PDF)


if __name__ == "__main__":
    if "--pdf" not in sys.argv:
        build()
    make_pdf()
