"""
add_day.py -- [2026-09-28] Add a finished trading day to every test data set,
from the bot's real 9:28 list (data/candidates/<date>_scanner.json):

  1. 1-min bar backtester   bars/bars_<date>.json.gz + bars/bench_<date>.json.gz
  2. tick replays (trade)   tick20/cands/<date>.json
  3. trade1 simulator       /var/www/screener/trade1/sim/candidates/<date>.json
  4. tick data              <cache>/<date>/<SYMBOL>.jsonl for the 30 symbols (downloaded)

Usage (after 16:00 ET):  python3 add_day.py 2026-09-28
Then rerun daily_winners.py / winner_patterns.py to include the day in the studies.
"""
import gzip
import json
import shutil
import sys
from datetime import datetime, timedelta
from pathlib import Path
from zoneinfo import ZoneInfo

HERE = Path(__file__).resolve().parent
TRADE = HERE.parents[1]
sys.path.insert(0, str(TRADE))
from alpaca_client import get_client
import volatility

ET = ZoneInfo("America/New_York")
TRADE1 = Path("/var/www/screener/trade1")


def main(day: str):
    src = TRADE / "data" / "candidates" / f"{day}_scanner.json"
    cands = json.load(open(src))
    syms = [c["symbol"] for c in cands]
    client = get_client()
    d = datetime.fromisoformat(day).replace(tzinfo=ET)

    # 1. bar backtester data
    daily = client.get_daily_bars_bulk(syms, d - timedelta(days=45), d - timedelta(seconds=1))
    data = {}
    for c in cands:
        s, m = c["symbol"], c["metrics"]
        raw = client.get_minute_bars(s, start=d.replace(hour=9, minute=30), end=d.replace(hour=16), limit=1000)
        bars = [[int(b.timestamp.timestamp()), float(b.open), float(b.high), float(b.low), float(b.close),
                 float(b.volume)] for b in raw]
        hist = [b for b in daily.get(s, []) if b["t"] < d]
        if len(hist) < 2 or len(bars) < 10:
            print("  skip", s, len(hist), len(bars))
            continue
        data[s] = {"bars": bars,
                   "levels": {"premarket_high": m.get("premarket_high"), "prev_day_high": m.get("previous_day_high"),
                              "range_20d_high": m.get("range_20d_high"), "daily_atr": volatility.daily_atr(hist),
                              **volatility.five_day_reference(hist)},
                   "baseline": m.get("avg_daily_volume") or hist[-1]["v"]}
    kept = [s for s in syms if s in data]
    with gzip.open(HERE / "bars" / f"bars_{day}.json.gz", "wt") as f:
        json.dump({"date": day, "top30_current": kept, "top30_alt": kept, "symbols": data,
                   "_note": "real 9:28 list (fixed scanner); no alt ranking for this day"}, f)
    bench = {}
    for b in ("SPY", "IWM"):
        raw = client.get_minute_bars(b, start=d.replace(hour=9, minute=30), end=d.replace(hour=16), limit=1000)
        bench[b] = [[int(x.timestamp.timestamp()), float(x.open), float(x.high), float(x.low), float(x.close),
                     float(x.volume)] for x in raw]
    with gzip.open(HERE / "bars" / f"bench_{day}.json.gz", "wt") as f:
        json.dump(bench, f)
    print(f"1. bars: {len(kept)} symbols, bench SPY/IWM")

    # 2. tick replay candidate list (same shape as the other 20 days)
    out = [{"symbol": c["symbol"], "metrics": {"premarket_high": c["metrics"].get("premarket_high"),
                                               "previous_day_high": c["metrics"].get("previous_day_high"),
                                               "range_20d_high": c["metrics"].get("range_20d_high"),
                                               "avg_daily_volume": c["metrics"].get("avg_daily_volume")}}
           for c in cands if c["symbol"] in data]
    json.dump(out, open(HERE / "tick20" / "cands" / f"{day}.json", "w"))
    print(f"2. tick20/cands/{day}.json")

    # 3. trade1 simulator list
    shutil.copy(src, TRADE1 / "sim" / "candidates" / f"{day}.json")
    print(f"3. trade1/sim/candidates/{day}.json")

    if "--no-ticks" in sys.argv:
        return
    # 4. tick data (same downloader the trade1 simulator uses)
    sys.path.insert(0, str(TRADE1))
    import importlib.util
    spec = importlib.util.spec_from_file_location("t1sim", TRADE1 / "simulate.py")
    t1 = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(t1)
    cache = Path(json.load(open(TRADE1 / "config.json"))["simulator"]["cache_dir"]) / day
    t1.ensure_cache(lambda: client, kept, day, cache)
    print(f"4. ticks cached in {cache}")


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