"""
sim_full.py -- [2026-10-24] FULL account simulation of the trade bot as configured (config.json "ev3", read at start), from raw data:
minute by minute per daily top-30 stock the EV3 entry logic (same order / per-setup cool-down / ATR filter / N2+N3 gate with
exemptions / one setup per stock per minute / last entry minute as ev3.EV3.entry_check, on finished bars), buy at the next open,
stop under the support, C3 exit (GB2 + X1 control, sim_2y.sim), 15:55; account rules: 5 slots, one position per stock, 1% risk
capped at 19% notional, re-entry after a loss only above that entry + 1c, no new buys after 15:15; $100,000 start, 0.2% cost.
  --src lib   the 2-year library (bars/bars_<day>, top30_current, reference files), --flow quote (flow/) or tick (flow_tick/)
  --src 10y   the first 2 years of the 10-year data (top30_10y/<day>.json from build_10y_top30.py: v3 picks + refs,
              bars10y minute bars, flow_tick tick-rule flow)
  --src star  [2026-10-24] the STAR bot's selection (star_lists/<day>.json: v1 top 50 at 9:30 + intraday re-score every 5 min from
              9:50, top 50) feeding EV3: a stock can be bought only while it is on the current list; its flow starts 15 min
              before it was first listed (flow_star/, or flow_tick/ masked the same way); vol_per_min from bars10y (14 days).
    nice -n 19 python3 sim_full.py --src 10y --out sim10y_first2y [--start D] [--end D]
"""
import argparse, gzip, json, math, sys
from collections import defaultdict
from pathlib import Path
import numpy as np
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE)); sys.path.insert(0, "/var/www/screener/trade/reports/playbook"); sys.path.insert(0, "/var/www/screener/trade")
import v2_bar_replay as R
import ev3 as E
from queue_test import Day
from sim_2y import sim as exit_sim
OW = Path("/var/www/screener/trade/reports/open_window")
CFG = E.EV3(json.load(open("/var/www/screener/trade/config.json"))["ev3"]).c
COST = 0.2; START = 100000.0; LAST_BUY_M = 344


def entries(D, sym, elig=None):
    """EV3.entry_check logic on finished bars, flow fully known (setups read flow only up to the finished minute)."""
    ref = D.ref; out = []
    if not ref or not ref.get("atr14") or not ref.get("prev_close"):
        return out
    atrp = ref["atr14"] / ref["prev_close"] * 100; last = {}
    for i in range(4, len(D.b) - 1):
        m = D.m[i]
        if m >= CFG["last_entry_minute"]:
            break
        if elig is not None and not elig(m + 1):
            continue
        done = D.b[:i + 1]
        for name in CFG["order"]:
            sc = CFG["setups"].get(name)
            if sc is None or sc.get("enabled") is False:
                continue
            if "atr_min" in sc and atrp < sc["atr_min"] or "atr_max" in sc and atrp >= sc["atr_max"]:
                continue
            mod = E.MODS[name]
            if m - last.get(name, -9999) < mod.DEFAULTS.get("cooldown", 15):
                continue
            try:
                ok, info = mod.check(done, D.fl, ref, m, {k: v for k, v in sc.items() if k not in E.CONTROL_KEYS} or None)
            except Exception:
                continue
            if ok:
                last[name] = m
                P = done[-1]["c"]
                if not sc.get("no_trade_exempt"):
                    g = CFG["no_trade"]
                    B = sum(D.fl["buy"][max(0, m - 14):m + 1]); S = sum(D.fl["sell"][max(0, m - 14):m + 1])
                    if g.get("enabled") and ((g.get("below_prev_low") and ref.get("prev_low") and P < ref["prev_low"])
                                             or (B + S and B / (B + S) < g["min_bs15"])):
                        continue
                out.append((m, name, info)); break
    return out


def star_loader(lists="star_lists"):
    import gzip as _gz
    from collections import deque
    hist = {}; state = {"last": None}
    def minute(ts):
        return R.minute(R.bars_of([[ts, 0, 0, 0, 0, 0]])[0]["t"])
    def days():
        return sorted(p.stem for p in (HERE / lists).glob("20*.json"))
    def warm(day):                                    # volume-by-minute history from bars10y up to the day before
        files = sorted(p for y in ("2024", "2025", "2026") for p in (OW / "bars10y" / y).glob("20*.json.gz"))
        todo = [f for f in files if (state["last"] or "2024-09-01") < f.name[:10] < day]
        for f in todo:
            b = json.load(_gz.open(f, "rt"))["bars"]
            for s_, bl in b.items():
                a_ = np.zeros(390, np.float32)
                for x in bl:
                    k = R.minute(R.bars_of([x])[0]["t"])
                    if 0 <= k < 390:
                        a_[k] = x[5]
                hist.setdefault(s_, deque(maxlen=14)).append(a_)
            state["last"] = f.name[:10]
    def load_day(day):
        L = json.loads((HERE / lists / f"{day}.json").read_text())
        if lists in ("star_lists", "lists_v3", "lists_v1"):   # the real-day lists carry the bot's own vol_per_min
            warm(day)
        first = {s_: 0 for s_ in L["pre"]}
        for t in sorted(L["rescans"], key=int):
            for s_ in L["rescans"][t]:
                first.setdefault(s_, int(t))
        times = sorted(int(t) for t in L["rescans"])
        def lists_at(m):
            k = max((t for t in times if t <= m), default=None)
            return set(L["pre"]) if k is None else set(L["rescans"][str(k)])
        cache = {}
        def elig_for(s_):
            def f(m):
                if m not in cache:
                    cache[m] = lists_at(m)
                return s_ in cache[m]
            return f
        bars = json.load(_gz.open(OW / "bars10y" / day[:4] / f"{day}.json.gz", "rt"))["bars"]
        out = []
        for s_ in sorted(first, key=lambda z: (first[z], L["pre"].index(z) if z in L["pre"] else 99)):
            from_m = max(0, first[s_] - 15)
            fp = HERE / "flow_star" / day / f"{s_}.json"
            if not fp.exists():
                fp = OW / "flow_tick" / day / f"{s_}.json"
            if s_ not in bars or not fp.exists():
                continue
            fl = json.loads(fp.read_text())
            fl = {"buy": [0.0] * from_m + fl["buy"][from_m:], "sell": [0.0] * from_m + fl["sell"][from_m:]}
            ref = dict(L["ref"].get(s_) or {})
            if s_ in hist and len(hist[s_]) >= 5:
                ref["vol_per_min"] = [float(x) for x in np.mean(np.array(hist[s_]), axis=0)]
            bl = [x for x in R.bars_of(bars[s_]) if 0 <= R.minute(x["t"]) < 390]
            if len(bl) < 100 or not ref.get("atr14"):
                continue
            D = Day(bl, [R.minute(x["t"]) for x in bl], fl, ref); D.elig = elig_for(s_)
            out.append((s_, D))
        return out
    return days, load_day


def loader(src, flow):
    if src == "star":
        return star_loader()
    if src == "starlive":
        return star_loader("star_lists_live")
    if src == "livev1":                                # [2026-10-25] the trade bot's new list (old score, top 30) on Star's real days
        return star_loader("lists_live_v1")
    if src in ("v3", "v1"):                            # [2026-10-24] premarket-only score lists (score_lists.py)
        return star_loader(f"lists_{src}")
    def days():
        if src == "lib":
            return sorted(p.name[5:15] for p in (OW / "bars").glob("bars_20*.json.gz"))
        return sorted(p.stem for p in (HERE / "top30_10y").glob("20*.json"))
    def load_day(day):
        if src == "lib":
            d = json.load(gzip.open(OW / "bars" / f"bars_{day}.json.gz", "rt")); top = d["top30_current"]; bars = {s: d["symbols"][s]["bars"] for s in top if s in d["symbols"]}
            refs = {s: R.REF.load(day, s) for s in top}
        else:
            t = json.loads((HERE / "top30_10y" / f"{day}.json").read_text()); top = t["top30"]; refs = t["ref"]
            b = json.load(gzip.open(OW / "bars10y" / day[:4] / f"{day}.json.gz", "rt"))["bars"]; bars = {s: b[s] for s in top if s in b}
        out = []
        for s in top:
            fp = OW / ("flow" if flow == "quote" else "flow_tick") / day / f"{s}.json"
            if s not in bars or not fp.exists() or not refs.get(s):
                continue
            bl = [x for x in R.bars_of(bars[s]) if 0 <= R.minute(x["t"]) < 390]
            if len(bl) < 100:
                continue
            fl = json.loads(fp.read_text())
            out.append((s, Day(bl, [R.minute(x["t"]) for x in bl], fl, refs[s])))
        return out
    return days, load_day


def main():
    ap = argparse.ArgumentParser(); ap.add_argument("--src", default="10y"); ap.add_argument("--flow", default="tick")
    ap.add_argument("--out", required=True); ap.add_argument("--start", default="0"); ap.add_argument("--end", default="9")
    a = ap.parse_args()
    days, load_day = loader(a.src, a.flow)
    eq = START; trades = []; daily = []; skipped = defaultdict(int); trig_n = defaultdict(int)
    for day in days():
        if not (a.start <= day <= a.end):
            continue
        stocks = load_day(day); rank = {s: i for i, (s, _) in enumerate(stocks)}; Ds = dict(stocks)
        ev = []
        for s, D in stocks:
            for m, name, info in entries(D, s, getattr(D, "elig", None)):
                ev.append((m + 1, rank[s], s, name, m, info)); trig_n[name] += 1
        ev.sort(key=lambda z: z[:2])
        open_pos = []; last = {}; realized = 0.0; e0 = eq
        for bm, _, s, st, m, info in ev:
            if bm > LAST_BUY_M:
                skipped["after 15:15"] += 1; continue
            for op in list(open_pos):
                if op["exit_m"] <= bm:
                    open_pos.remove(op); realized += op["pl_usd"]; last[op["sym"]] = op
            if any(op["sym"] == s for op in open_pos):
                skipped["stock already open"] += 1; continue
            if len(open_pos) >= 5:
                skipped["no free slot"] += 1; continue
            D = Ds[s]; bi = D.m.index(m) + 1; P = D.b[bi]["o"]
            if s in last and last[s]["pl_usd"] <= 0 and not P > last[s]["entry"] + 0.01:
                skipped["re-entry after a loss"] += 1; continue
            sup = (info or {}).get("support") or P * 0.99
            stop = min(max(sup * (1 - CFG["stop_buf"]), P * (1 - CFG["stop_max_pct"] / 100)), P * (1 - CFG["stop_min_pct"] / 100))
            if P <= stop:
                continue
            equity = e0 + realized
            qty = min(math.floor(equity * 0.01 / (P - stop)), math.floor(equity * 0.19 / P))
            if qty < 1:
                continue
            pl, j, why = exit_sim(D, bi, P, stop, CFG["arm_pct"], "C3" if CFG["controller"].get("enabled") else "GB")
            t = {"day": day, "sym": s, "setup": st, "buy_m": D.m[bi], "exit_m": D.m[j], "entry": P, "qty": qty,
                 "pl_pct": round(pl, 3), "pl_usd": round(qty * P * pl / 100, 2), "why": why}
            open_pos.append(t); trades.append(t)
        realized += sum(op["pl_usd"] for op in open_pos); eq = e0 + realized; daily.append((day, eq))
        R.REF._CACHE.clear()
        print(day, len(stocks), len(ev), round(eq), flush=True)
    json.dump({"trades": trades, "daily": daily, "skipped": dict(skipped), "triggers": dict(trig_n), "args": vars(a)},
              open(HERE / f"{a.out}.json", "w"))
    report(a.out)


def report(name):
    Q = json.load(open(HERE / f"{name}.json")); T = Q["trades"]; dl = Q["daily"]
    e = np.array([x[1] for x in dl]); dd = (e / np.maximum.accumulate(e) - 1).min() * 100
    a = np.array([t["pl_usd"] for t in T]); pc = np.array([t["pl_pct"] for t in T])
    print(f"\n===== {name} {Q['args']}  {dl[0][0]} .. {dl[-1][0]} ({len(dl)} days)")
    print(f"  $100,000 -> ${e[-1]:,.0f} ({(e[-1] / START - 1) * 100:+.1f}%) | max drawdown {dd:.1f}% | {len(T)} trades | win {np.mean(a > 0):.0%} "
          f"| avg {pc.mean():+.3f}%/trade | profit factor {a[a > 0].sum() / max(1e-9, -a[a < 0].sum()):.2f}")
    half = dl[len(dl) // 2][0]
    for lab, f in (("first year", lambda d: d < half), ("second year", lambda d: d >= half)):
        Y = [t for t in T if f(t["day"])]; ya = np.array([t["pl_usd"] for t in Y])
        print(f"  {lab}: {len(Y)} trades ${ya.sum():+,.0f}, win {np.mean(ya > 0):.0%}, avg {np.mean([t['pl_pct'] for t in Y]):+.3f}%")
    print("  by setup: " + " | ".join(f"{s} {len(X)} ${sum(t['pl_usd'] for t in X):+,.0f} ({np.mean([t['pl_pct'] for t in X]):+.2f}%)"
                                     for s in CFG["order"] for X in [[t for t in T if t["setup"] == s]] if X))
    w = defaultdict(lambda: [0, 0.0])
    for t in T:
        w[t["why"]][0] += 1; w[t["why"]][1] += t["pl_usd"]
    print("  by exit:  " + " | ".join(f"{x} {n} ${v:+,.0f}" for x, (n, v) in sorted(w.items(), key=lambda z: -z[1][0])))
    mo = defaultdict(float)
    for t in T:
        mo[t["day"][:7]] += t["pl_usd"]
    print("  by month: " + " ".join(f"{m_[2:]} {v:+,.0f}" for m_, v in sorted(mo.items())))
    print(f"  months positive {sum(v > 0 for v in mo.values())}/{len(mo)} | triggers {Q['triggers']} | skipped {Q['skipped']}")
    big = sorted(T, key=lambda t: -t["pl_usd"])[:5]
    print("  biggest: " + ", ".join(f"{t['sym']} {t['day']} {t['setup']} {t['pl_pct']:+.1f}%" for t in big)
          + f" | without them ${a.sum() - sum(t['pl_usd'] for t in big):+,.0f}")
    print("SIMFULL_DONE")


if __name__ == "__main__":
    report(sys.argv[2]) if sys.argv[1:2] == ["--report"] else main()
