"""
star_buys_6m.py -- [2026-10-21] the user: "option 2 for now" = test the PLAYBOOK BUYS (Star buys) on the 6 months
2026-04-01..2026-10-02 with the REAL star_live engine (library v3, good days only, library days BEFORE the replayed day).
  - stocks: each day's top-30 list (reports/open_window/bars); a stock QUALIFIES at its first v2 buy signal
    (BASE run of trade/reports/playbook/c3_deep_6m.jsonl: real entry_v2 bar replay; signal bar = buy_m - 1)
  - STAR BUYS at the next open when >= min_refs good days are `similarity` similar AND their buy signal fires
  - SELLS like the live Star bot: Star's sell (sf3 latest) at the next open, with noise2 (k 2.0) holding it while
    close >= VWAP and the dip from the high since the buy < 2 x the 1-min range; 3% safety stop (intrabar); 15:55
  - comparison BB = the live Star bot today: the bot buys on v2's own signals (the BASE buy times), same sells
  - 5 slots per variant, $1,640 per trade, 0.2% cost (real fills). All variants run in lockstep on one engine
    (positions swapped per variant), so the library is loaded once.
    nice -n 19 python3 star_buys_6m.py [START..END]  -> data/star_buys_6m.jsonl + report
"""
import gzip
import json
import sys
from collections import defaultdict
from datetime import datetime, timedelta
from pathlib import Path

import numpy as np

sys.path.insert(0, "/var/www/screener/premarket")
import star_live  # noqa: E402
from star_replay import OW, REFD, ET, rows_to_bars  # noqa: E402

DEEP = Path("/var/www/screener/trade/reports/playbook/c3_deep_6m.jsonl")
OUT = Path("/var/www/screener/premarket/data/star_buys_6m.jsonl")
SIZE, SLOTS, COST, STOP, NOISE_K = 1640.0, 5, 0.2, 3.0, 2.0
VARIANTS = {"SB85": 0.85, "SB80": 0.80, "SB75": 0.75, "BB": None}     # SBxx = Star buys at xx% similar; BB = bot buys


def base_buys():
    q, bb = defaultdict(dict), defaultdict(lambda: defaultdict(list))
    for l in open(DEEP):
        t = json.loads(l)
        if t["run"] != "BASE":
            continue
        s = t["buy_m"] - 1
        q[t["day"]][t["sym"]] = min(q[t["day"]].get(t["sym"], 999), s)
        bb[t["day"]][t["sym"]].append(t["buy_m"])
    return q, bb


def noise2(bars_done, pos):
    if len(bars_done) < 16:
        return False
    pv = sum((b["h"] + b["l"] + b["c"]) / 3 * b["v"] for b in bars_done); vol = sum(b["v"] for b in bars_done)
    vwap = pv / vol if vol else bars_done[-1]["c"]
    close = bars_done[-1]["c"]
    since = [b for b in bars_done if star_live.minute_of(b["t"]) >= pos["buy_m"]] or bars_done[-1:]
    high = max(max(b["h"] for b in since), pos["buy"])
    r1 = sum(b["h"] - b["l"] for b in bars_done[-15:]) / 15
    return close >= vwap and (high - close) < NOISE_K * r1


def run_day(eng, day, q, bbuys, out):
    d = json.load(gzip.open(OW / "bars" / f"bars_{day}.json.gz", "rt"))
    bj = OW / "bars" / f"bench_{day}.json.gz"
    bench = json.load(gzip.open(bj, "rt")) if bj.exists() else {}
    bench = (rows_to_bars(bench.get("SPY", [])), rows_to_bars(bench.get("IWM", [])))
    eng.set_day(day)
    data = {}
    for s in q:
        rp, fp = REFD / day / f"{s}.json", OW / "flow" / day / f"{s}.json"
        if s not in d["symbols"] or not rp.exists() or not fp.exists():
            continue
        fl = json.loads(fp.read_text()); bars = rows_to_bars(d["symbols"][s]["bars"])
        data[s] = {"bars": bars, "by": {star_live.minute_of(b["t"]): b for b in bars}, "ref": json.loads(rp.read_text()),
                   "flow": {m: (fl["buy"][m], fl["sell"][m]) for m in range(390)}}
    del d
    d0 = datetime.fromisoformat(day).replace(tzinfo=ET)
    pos = {v: {} for v in VARIANTS}                    # variant -> sym -> trade dict (+ "star" = engine pos state)
    n = defaultdict(int)
    for m in range(0, 386):
        now = d0.replace(hour=9, minute=30) + timedelta(minutes=m + 1)
        nxt = m + 1
        for s, x in data.items():
            if m >= q[s]:                              # Star only needs the stock once it is qualified (catches up)
                eng.update(s, [b for b in x["bars"] if star_live.minute_of(b["t"]) <= m], x["ref"],
                           tuple([b for b in bb if star_live.minute_of(b["t"]) <= m] for bb in bench), x["flow"], now)
                eng.state[s]["qualified"] = True
        for v, sim in VARIANTS.items():
            P = pos[v]
            for s in list(P):
                p = P[s]; x = data[s]; b = x["by"].get(m); st = eng.state[s]
                if b and b["l"] <= p["stop"] and m >= p["buy_m"]:
                    p.update(sell_m=m, sell=min(p["stop"], b["o"]) if m > p["buy_m"] else p["stop"], why="safety stop")
                elif m >= 385:
                    last = [y for k, y in x["by"].items() if k <= m]
                    p.update(sell_m=m, sell=max(last, key=lambda y: y["t"])["c"] if last else p["buy"], why="15:55")
                else:
                    st["pos"] = p["star"]
                    sell, why = eng.sell_check(s)
                    p["star"] = st["pos"]; st["pos"] = None
                    if not (sell and x["by"].get(nxt)):
                        continue
                    done = [y for y in x["bars"] if star_live.minute_of(y["t"]) <= m]
                    if noise2(done, p):
                        p["star"].update(diverged=False, climb=False, out=0); p["holds"] = p.get("holds", 0) + 1
                        continue
                    p.update(sell_m=nxt, sell=x["by"][nxt]["o"], why=why.split(" (")[0].split(",")[0])
                p.pop("star", None)
                p["pl"] = (p["sell"] / p["buy"] - 1) * 100 - COST
                out.write(json.dumps(p) + "\n"); n[v] += 1
                del P[s]
            if m >= 385:
                continue
            for s, x in data.items():
                if s in P or len(P) >= SLOTS or m < q[s] or not x["by"].get(nxt):
                    continue
                if sim is None:
                    ok = nxt in bbuys.get(s, [])
                    info = {}
                else:
                    eng.c["similarity"] = sim
                    st = eng.state[s]; st["pos"] = None
                    ok, info = eng.buy_check(s)
                if ok:
                    e = x["by"][nxt]["o"]
                    st = eng.state[s]; st["pos"] = None
                    eng.open_position(s)
                    P[s] = {"run": v, "day": day, "sym": s, "q_m": q[s], "buy_m": nxt, "buy": e, "stop": e * (1 - STOP / 100),
                            "refs": info.get("refs"), "star": st["pos"]}
                    st["pos"] = None
    print(day, dict(n), flush=True)


def report():
    T = [json.loads(l) for l in open(OUT)]
    days = sorted({t["day"] for t in T}); half = days[len(days) // 2]
    print(f"\n{len(days)} days {days[0]}..{days[-1]}; ${SIZE:.0f} per trade, {COST}% cost, 5 slots, 3% safety stop, Star sells + noise2")
    print(f"   {'variant':36} {'trades':>6} {'per trade':>9} {'win':>4} {'$ total':>9} | {'half 1 $':>9} {'half 2 $':>9} | per day")
    names = {"SB85": "Star buys, 85% similar", "SB80": "Star buys, 80% similar", "SB75": "Star buys, 75% similar",
             "BB": "bot buys (live Star bot today)"}
    for v in VARIANTS:
        X = [t for t in T if t["run"] == v]
        if not X:
            print(f"   {names[v]:36} 0"); continue
        p = np.array([t["pl"] for t in X]); h = np.array([t["day"] < half for t in X])
        print(f"   {names[v]:36} {len(p):6} {p.mean():+8.3f}% {np.mean(p > 0):4.0%} {p.sum() * SIZE / 100:+9.2f} | "
              f"{p[h].sum() * SIZE / 100:+9.2f} {p[~h].sum() * SIZE / 100:+9.2f} | {len(p) / len(days):.1f} trades")
        why = defaultdict(list)
        for t in X:
            why[t["why"]].append(t["pl"])
        print("        exits: " + "; ".join(f"{w} {len(a)} ({np.mean(a):+.2f}%)" for w, a in sorted(why.items(), key=lambda z: -len(z[1]))))
        if v != "BB":
            dl = np.array([t["buy_m"] - t["q_m"] for t in X])
            print(f"        buy after the v2 qualification: median {np.median(dl):.0f} min")
    print("STARBUYS_DONE")


if __name__ == "__main__":
    if "--report" in sys.argv:
        report(); sys.exit()
    rng = sys.argv[1] if len(sys.argv) > 1 else "2026-04-01..2026-10-02"
    a, b = rng.split("..")
    Q, BBY = base_buys()
    eng = star_live.StarEngine({"similarity": 0.80, "min_refs": 30}, today="2099-01-01")
    with open(OUT, "w") as out:
        for day in sorted(Q):
            if a <= day <= b:
                run_day(eng, day, Q[day], BBY[day], out)
                out.flush()
    report()
