"""
star_flow.py -- [2026-10-24] tick-rule buy/sell flow (Massive trades, as massive_flow_tick.py) for every stock in the Star bot's
lists (star_lists/<day>.json) of the last 2 years, FROM 15 minutes before the stock was first listed (the live bot back-fills
only 15 min of flow for a stock added by a rescan; the 9:30 list from 9:30). Stocks that already have a full-day file in
open_window/flow_tick are skipped (the simulator masks them the same way).
  output: reports/ev3/flow_star/<day>/<SYM>.json {"buy": [390], "sell": [390], "from_m": m, "trades": n}
Resumable; 3 in parallel; stops in market hours (weekdays 9:10-16:05).
    python3 star_flow.py
"""
import json, sys, time
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo
HERE = Path(__file__).resolve().parent; OW = Path("/var/www/screener/trade/reports/open_window")
sys.path.insert(0, str(OW))
from massive_trial import get, BASE, tick_flow
ET = ZoneInfo("America/New_York"); OUT = HERE / "flow_star"


def market_hours():
    t = datetime.now(ET)
    return t.weekday() < 5 and (9, 10) <= (t.hour, t.minute) < (16, 5)


def first_listed(L):
    f = {s: 0 for s in L["pre"]}
    for t in sorted(L["rescans"], key=int):
        for s in L["rescans"][t]:
            f.setdefault(s, int(t))
    return f


def one(day, sym, from_m):
    f = OUT / day / f"{sym}.json"
    if f.exists():
        return 0
    d0 = datetime.fromisoformat(day).replace(tzinfo=ET)
    a = int(d0.replace(hour=9, minute=30).timestamp() * 1e9) + from_m * 60 * 10 ** 9
    z = int(d0.replace(hour=16).timestamp() * 1e9)
    url, params, tr = f"{BASE}/v3/trades/{sym}", {"timestamp.gte": a, "timestamp.lt": z, "limit": 50000, "sort": "timestamp", "order": "asc"}, []
    while url:
        j = get(url, params)
        tr += [(t["sip_timestamp"], t["price"], t["size"]) for t in j.get("results", [])]
        url, params = j.get("next_url"), None
    buy, sell = tick_flow(tr)
    f.parent.mkdir(parents=True, exist_ok=True)
    tmp = f.with_suffix(".tmp")
    tmp.write_text(json.dumps({"buy": [round(x) for x in buy], "sell": [round(x) for x in sell], "from_m": from_m, "trades": len(tr)}))
    tmp.rename(f)
    return 1


def _safe(x):
    try:
        return one(*x)
    except Exception as e:
        print(f"{x[0]} {x[1]} failed: {e}", flush=True); return 0


def main():
    todo = []
    for p in sorted((HERE / (sys.argv[1] if len(sys.argv) > 1 else "star_lists")).glob("20*.json")):
        L = json.loads(p.read_text()); day = L["date"]
        for s, m in first_listed(L).items():
            if (OW / "flow_tick" / day / f"{s}.json").exists() or (OUT / day / f"{s}.json").exists():
                continue
            todo.append((day, s, max(0, m - 15)))
    print(f"{len(todo)} stock-days to do", flush=True)
    t0, done = time.time(), 0
    for i in range(0, len(todo), 30):
        if market_hours():
            print("stopped: market hours", flush=True); return
        with ThreadPoolExecutor(3) as ex:
            done += sum(ex.map(_safe, todo[i:i + 30]))
        print(f"{todo[min(i + 29, len(todo) - 1)][0]}: {done} done [{(time.time() - t0) / 60:.0f} min]", flush=True)
    print("STAR_FLOW_DONE", flush=True)


if __name__ == "__main__":
    main()
