"""
group_build.py -- [2026-10-09] The user: "after a stock is picked by the scanner, know in what group it will belong
after 15 minutes of trade; monitor the trend, and as soon as it is identified as a winner, trigger a (logged) buy
signal". Step 1: training rows. Scanner-like stocks only (the day's top 30 by the v3 score, pm_history), 5 years.
One row per stock-day per CHECKPOINT (9:45, 10:00, 10:15, 10:30, 11:00, 11:30), using ONLY what the bot knows then:
  gap, daily ATR % (the scanner's own atr / prev close), premarket volume, move from the open, day high/low so far,
  move in ATRs, vs VWAP, volume so far / the PRIOR DAY's volume (the bot's baseline), last-15-min move, straightness
  and range, position in the day's range, distance to the day high, the shape style so far (up2_styles rules).
OUTCOME from the checkpoint price P (the bot would buy there): WIN = +3% over P before -1.5% under P (same bar =
loss), LOSS = -1.5% first, FLAT = neither by 15:55. Trade = +3 / -1.5 / close-P (GROSS). Also the max gain after.
Output: group_rows.csv.gz.      python3 group_build.py [START] [END]
"""
import csv
import gzip
import json
import sys
from datetime import datetime, timedelta, timezone
from pathlib import Path
from zoneinfo import ZoneInfo

import numpy as np

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
from up2_styles_build import v3_top30, arrays, cl  # noqa: E402

ET = ZoneInfo("America/New_York")
OUT = HERE / "group_rows.csv.gz"
CHECKS = (15, 30, 45, 60, 90, 120)
WIN, STOP = 3.0, 1.5


def features(a, t, O, atrp, gap, pmv, prev_vol):
    """What the bot knows at minute t (bars 0..t-1 closed). Shared with trade/group_watch.py (same formulas)."""
    Op, H, L, C, V = a[0][:t], a[1][:t], a[2][:t], a[3][:t], a[4][:t]
    P = C[-1]
    hi, lo = H.max(), L.min()
    cv = V.sum()
    vw = ((H + L + C) / 3 * V).sum() / cv if cv else P
    w = slice(max(0, t - 15), t)
    path = np.abs(np.diff(C[w])).sum()
    base = C[max(0, t - 16)] if t > 15 else O
    ret = (P / O - 1) * 100
    # shape style so far (up2_styles rules at the first +2% touch; "none" before it)
    hit = np.where(H >= O * 1.02)[0]
    style = "none"
    if len(hit):
        k = int(hit[0]); lb = (L[:k + 1].min() / O - 1) * 100
        p2 = np.abs(np.diff(C[:k + 1])).sum() if k else 0.0
        eff = (C[k] - O) / p2 if p2 > 0 else 1.0
        bs = (H[k - 35:k - 5].max() - L[k - 35:k - 5].min()) / O * 100 if k >= 35 else 99
        style = ("deep V" if lb <= -3 else "dip, then up" if lb <= -1 else "straight" if k < 15 else
                 "late move" if k >= 270 else "base break" if bs <= 1.0 else "slow grind" if k >= 60 and eff >= 0.25 else "choppy")
    return {"t": t, "gap": gap, "atrp": atrp, "pmv": pmv, "ret": ret, "hi": (hi / O - 1) * 100, "lo": (lo / O - 1) * 100,
            "ret_atr": ret / atrp if atrp else np.nan, "vwap": (P / vw - 1) * 100,
            "vol_rel": cv / prev_vol if prev_vol else np.nan, "r15": (P / base - 1) * 100,
            "eff15": (C[-1] - C[w][0]) / path if path > 0 else 0.0, "rng15": (H[w].max() - L[w].min()) / O * 100,
            "pos": (P - lo) / (hi - lo) if hi > lo else 0.5, "off_hi": (P / hi - 1) * 100, "style": style}


def outcome(a, t):
    H, L, C = a[1], a[2], a[3]
    P = C[t - 1]; up, dn = P * (1 + WIN / 100), P * (1 - STOP / 100)
    res, pl = "FLAT", (C[385] / P - 1) * 100
    for j in range(t, 386):
        if L[j] <= dn:
            res, pl = "LOSS", -STOP; break
        if H[j] >= up:
            res, pl = "WIN", WIN; break
    return res, pl, (H[t:386].max() / P - 1) * 100, (C[385] / P - 1) * 100


def main(start="2021-10-08", end="2026-12-31"):
    files = [f for f in sorted((HERE / "bars10y").glob("*/*.json.gz")) if start <= f.name[:10] <= end]
    prev_daily, n = {}, 0
    # the day before START, for the prior-day volume
    before = sorted(f for f in (HERE / "bars10y").glob("*/*.json.gz") if f.name[:10] < start)
    if before:
        prev_daily = json.load(gzip.open(before[-1], "rt")).get("daily", {})
    with gzip.open(OUT, "wt", newline="") as fh:
        w = None
        for f in files:
            day = f.name[:10]
            d = json.load(gzip.open(f, "rt"))
            daily = d.get("daily", {})
            pmf = HERE / "pm_history" / f"{day}.json.gz"
            pm = {r["s"]: r for r in json.load(gzip.open(pmf, "rt"))["rows"]} if pmf.exists() else {}
            for s in v3_top30(day):
                if s not in d["bars"] or s not in pm:
                    continue
                a = arrays(d["bars"][s])
                if a is None:
                    continue
                r = pm[s]; O = float(a[0, 0]); pc = r.get("prev_close")
                atrp = r["atr"] / pc * 100 if r.get("atr") and pc else np.nan
                if atrp == atrp and atrp > 25:
                    atrp = np.nan
                gap = (O / pc - 1) * 100 if pc else np.nan
                prev_vol = (prev_daily.get(s) or [None] * 5)[4] or 500000.0   # not in yesterday's list -> < 500k
                for t in CHECKS:
                    row = {"day": day, "sym": s, **features(a, t, O, atrp, gap, r.get("pm_vol") or 0, prev_vol)}
                    res, pl, mx, pc_ = outcome(a, t)
                    row.update(res=res, pl=round(pl, 3), max_after=round(mx, 2), to_close=round(pc_, 3))
                    row = {k: (round(v, 4) if isinstance(v, float) else v) for k, v in row.items()}
                    if w is None:
                        w = csv.DictWriter(fh, fieldnames=list(row)); w.writeheader()
                    w.writerow(row); n += 1
            prev_daily = daily
            del d
            print(day, n, flush=True)
    print("GROUP_BUILD_DONE", n)


if __name__ == "__main__":
    main(*sys.argv[1:3])
