"""
day_pages.py -- [2026-10-11] The user: a web page per PAST DAY listing every stock the scanners selected; click a stock
-> its chart and a table with every indicator value PER MINUTE, for as long as the bot could see it.
Source = what the bots really saw: data/decisions/<day>.jsonl (one record per symbol per 5-s poll: state, failing
rules, all entry metrics) -> the LAST record of each minute. Bars for the chart: Alpaca 1-min bars. Trades: both bots.
Output (served by Apache): reports/days/index.html, reports/days/<day>/index.html, reports/days/<day>/<SYM>.html
    python3 day_pages.py [DAY ...]      default: today          python3 day_pages.py --all   (every day with decisions)
cron: 16:40 weekdays
"""
import glob
import html
import json
import os
import sys
from collections import defaultdict
from datetime import date, datetime, timezone
from pathlib import Path
from zoneinfo import ZoneInfo

TRADE = Path("/var/www/screener/trade"); STAR = Path("/var/www/screener/premarket")
BOTS = {"trade": TRADE, "star": STAR}
OUT = TRADE / "reports" / "days"
ET = ZoneInfo("America/New_York")
sys.path.insert(0, str(TRADE)); sys.path.insert(0, str(TRADE / "reports" / "playbook"))
LWC = "https://unpkg.com/lightweight-charts@4.1.3/dist/lightweight-charts.standalone.production.js"

# (column title, how to read it from a decision record)
COLS = [
    ("state", lambda r: r.get("state")),
    ("vs open %", lambda m: m.get("ext_open_pct")),
    ("vs VWAP %", lambda m: m.get("vwap_pct")),
    ("RVOL so far", lambda m: m.get("rvol_sofar")),
    ("vol 15m x", lambda m: m.get("vol15_x")),
    ("buy share 15m", lambda m: m.get("buy_share15")),
    ("buy vol 15m", lambda m: m.get("buy_vol15")),
    ("sell vol 15m", lambda m: m.get("sell_vol15")),
    ("smooth R2", lambda m: m.get("r2_15")),
    ("straight", lambda m: m.get("eff15")),
    ("accel 5", lambda m: m.get("acc5")),
    ("move x", lambda m: m.get("move_x")),
    ("spread %", lambda m: m.get("spread_pct")),
    ("shares/min 15", lambda m: m.get("shares_per_min_15")),
    ("room day high %", lambda m: m.get("room_day_high_pct")),
    ("to resistance %", lambda m: (m.get("sr14") or {}).get("dist_to_resistance_pct")),
    ("range pos", lambda m: m.get("range_pos")),
    ("playbook", lambda m: (m.get("playbook") or {}).get("group_name") or (m.get("playbook") or {}).get("action")),
]


def minute_of(ts):
    t = datetime.fromisoformat(ts)
    if t.tzinfo is None:
        t = t.replace(tzinfo=timezone.utc)
    t = t.astimezone(ET)
    return (t.hour - 9) * 60 + t.minute - 30


def hm(m):
    return f"{9 + (30 + m) // 60}:{(30 + m) % 60:02d}"


def read_decisions(root, day):
    """{sym: {minute: last entry record}}, {sym: {minute: last exit record}} -- streamed (files are ~200 MB)."""
    ent, ext = defaultdict(dict), defaultdict(dict)
    p = root / "data" / "decisions" / f"{day}.jsonl"
    if not p.exists():
        return ent, ext
    with open(p) as fh:
        for line in fh:
            try:
                r = json.loads(line)
                m = minute_of(r["timestamp"])
            except Exception:
                continue
            if not 0 <= m < 390:
                continue
            (ent if r.get("kind") == "entry" else ext)[r["symbol"]][m] = r
    return ent, ext


def scanned(root, day):
    first = {}
    for f in sorted(glob.glob(str(root / "data" / "candidates" / f"{day}_scanner*.json"))):
        b = os.path.basename(f)
        t = b.split("_scanner_")[1][:4] if "_scanner_" in b else "0930"
        try:
            for c in json.load(open(f)):
                first.setdefault(c["symbol"], f"{t[:2]}:{t[2:]}")
        except Exception:
            pass
    return first


def trades(root, day):
    p = root / "data" / "trades" / f"{day}_trades.jsonl"
    return [json.loads(l) for l in open(p)] if p.exists() else []


def fmt(v):
    if v is None:
        return ""
    if isinstance(v, float):
        return f"{v:,.0f}" if abs(v) >= 1000 else f"{v:.3f}".rstrip("0").rstrip(".") if abs(v) < 10 else f"{v:.2f}"
    return str(v)


CSS = """<style>
:root{--bg:#ffffff;--fg:#1d2733;--mute:#5b6675;--line:#d5dbe3;--head:#f1f4f8;--buy:#2e7d32;--sell:#c62828;--hl:#fff7d6}
@media (prefers-color-scheme: dark){:root:not([data-theme="light"]){--bg:#11161d;--fg:#e3e8ef;--mute:#98a3b3;--line:#2b3440;--head:#1a212b;--buy:#66bb6a;--sell:#ef5350;--hl:#3a3418}}
:root[data-theme="dark"]{--bg:#11161d;--fg:#e3e8ef;--mute:#98a3b3;--line:#2b3440;--head:#1a212b;--buy:#66bb6a;--sell:#ef5350;--hl:#3a3418}
body{font-family:system-ui,Arial,sans-serif;margin:16px;background:var(--bg);color:var(--fg)}
a{color:inherit} h1{font-size:20px;margin:4px 0 8px} .mute{color:var(--mute);font-size:13px}
table{border-collapse:collapse;font-size:12px;margin:8px 0} .wrap{overflow-x:auto;max-width:100%}
th,td{border:1px solid var(--line);padding:3px 6px;text-align:right;white-space:nowrap} th{background:var(--head);position:sticky;top:0}
td:first-child,th:first-child{text-align:left} tr.buy td{background:var(--hl)} .pos{color:var(--buy)} .neg{color:var(--sell)}
#chart{width:100%;height:440px;margin:8px 0} .tabs button{margin-right:6px;padding:4px 10px}
</style>"""


def stock_page(day, sym, bars, bot_rows, bot_exits, bot_trades, first_seen):
    data = [{"time": int(b["t"].timestamp()), "open": b["o"], "high": b["h"], "low": b["l"], "close": b["c"], "v": b["v"]} for b in bars]
    pv = cv = 0.0; vw = []
    for b in bars:
        pv += (b["h"] + b["l"] + b["c"]) / 3 * b["v"]; cv += b["v"]
        vw.append({"time": int(b["t"].timestamp()), "value": round(pv / cv, 4) if cv else b["c"]})
    t0 = int(bars[0]["t"].timestamp()) - 0 if bars else 0
    markers = []
    for bot, T in bot_trades.items():
        for t in T:
            for k, col, shape, lab in (("entry_time", "#2e7d32", "arrowUp", "buy"), ("exit_time", "#c62828", "arrowDown", "sell")):
                if t.get(k):
                    ts = int(datetime.fromisoformat(t[k]).replace(second=0, microsecond=0).timestamp())
                    markers.append({"time": ts - ts % 60, "position": "belowBar" if lab == "buy" else "aboveBar", "color": col,
                                    "shape": shape, "text": f"{bot} {lab} {t.get('entry_price' if lab == 'buy' else 'exit_price')}"})
    for bot, t in first_seen.items():
        h, mi = int(t[:2]), int(t[3:])
        dt = datetime.fromisoformat(day).replace(hour=h, minute=mi, tzinfo=ET)
        markers.append({"time": int(dt.timestamp()), "position": "aboveBar", "color": "#6a1b9a", "shape": "circle", "text": f"{bot} list"})
    markers.sort(key=lambda x: x["time"])
    tables = []
    for bot, rows in bot_rows.items():
        if not rows:
            continue
        head = "<tr><th>time</th><th>open</th><th>high</th><th>low</th><th>close</th><th>volume</th>" + "".join(
            f"<th>{html.escape(c)}</th>" for c, _ in COLS) + "<th>exit check</th><th>failing rules</th></tr>"
        byb = {(b["t"].astimezone(ET).hour - 9) * 60 + b["t"].astimezone(ET).minute - 30: b for b in bars}
        body = []
        for m in sorted(rows):
            r = rows[m]; mt = r.get("metrics") or {}; b = byb.get(m)
            cells = [hm(m)] + ([fmt(b["o"]), fmt(b["h"]), fmt(b["l"]), fmt(b["c"]), fmt(b["v"])] if b else [""] * 5)
            for c, f in COLS:
                v = f(r) if c == "state" else f(mt)
                cells.append(fmt(v))
            xr = bot_exits.get(bot, {}).get(m)
            cells.append(f"{xr.get('state')}" if xr else "")
            reasons = "; ".join(r.get("reasons_against") or [])
            cls = ' class="buy"' if r.get("state") == "BUY" else ""
            body.append(f"<tr{cls}>" + "".join(f"<td>{html.escape(x)}</td>" for x in cells)
                        + f"<td style='text-align:left'>{html.escape(reasons[:220])}</td></tr>")
        tables.append(f"<div class='bot' id='t_{bot}'><h2>{'Trade bot' if bot == 'trade' else 'Star bot'}: what it saw, minute by minute "
                      f"({hm(min(rows))} to {hm(max(rows))})</h2><div class='wrap'><table>{head}{''.join(body)}</table></div></div>")
    tl = []
    for bot, T in bot_trades.items():
        for t in T:
            tl.append(f"<tr><td>{bot}</td><td>{fmt(t.get('entry_price'))}</td><td>{(t.get('entry_time') or '')[11:16]} UTC</td>"
                      f"<td>{fmt(t.get('exit_price'))}</td><td>{(t.get('exit_time') or '')[11:16]} UTC</td>"
                      f"<td class='{'pos' if (t.get('pl_dollars') or 0) > 0 else 'neg'}'>{fmt(t.get('pl_dollars'))}</td>"
                      f"<td style='text-align:left'>{html.escape((t.get('exit_reason') or '')[:90])}</td></tr>")
    trade_tbl = ("<h2>Trades</h2><table><tr><th>bot</th><th>buy</th><th>at</th><th>sell</th><th>at</th><th>P/L $</th><th>exit reason</th></tr>"
                 + "".join(tl) + "</table>") if tl else "<p class='mute'>No trades in this stock.</p>"
    tabs = "".join('<button onclick="show(\'%s\')">%s</button>' % (b, "Trade bot" if b == "trade" else "Star bot")
                   for b in bot_rows if bot_rows[b])
    return f"""<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>{sym} {day}</title>{CSS}<script src="{LWC}"></script></head><body>
<p class="mute"><a href="index.html">&larr; {day} list</a> &middot; <a href="../index.html">all days</a></p>
<h1>{sym} &middot; {day}</h1>
<p class="mute">Candles = 1-min bars (Alpaca); orange = VWAP; purple dot = when each bot's scanner added it; arrows = real buys / sells.
Times in the table are New York time; the trade table times are UTC.</p>
<div id="chart"></div>{trade_tbl}
<div class="tabs">{tabs}</div>
{''.join(tables)}
<script>
const data={json.dumps(data)}, vw={json.dumps(vw)}, mk={json.dumps(markers)};
const dark=matchMedia('(prefers-color-scheme: dark)').matches;
const el=document.getElementById('chart');
const ch=LightweightCharts.createChart(el,{{height:440,layout:{{background:{{color:dark?'#11161d':'#ffffff'}},textColor:dark?'#e3e8ef':'#1d2733'}},
 grid:{{vertLines:{{color:dark?'#2b3440':'#eef1f5'}},horzLines:{{color:dark?'#2b3440':'#eef1f5'}}}},
 timeScale:{{timeVisible:true,secondsVisible:false}},localization:{{timeFormatter:t=>new Date(t*1000).toLocaleTimeString('en-US',{{timeZone:'America/New_York',hour:'2-digit',minute:'2-digit'}})}}}});
const cs=ch.addCandlestickSeries({{upColor:'#2e7d32',downColor:'#c62828',borderVisible:false,wickUpColor:'#2e7d32',wickDownColor:'#c62828'}});
cs.setData(data); cs.setMarkers(mk);
ch.addLineSeries({{color:'#e08a00',lineWidth:1}}).setData(vw);
const vs=ch.addHistogramSeries({{priceFormat:{{type:'volume'}},priceScaleId:'v'}});
ch.priceScale('v').applyOptions({{scaleMargins:{{top:0.8,bottom:0}}}});
vs.setData(data.map(d=>({{time:d.time,value:d.v,color:d.close>=d.open?'rgba(46,125,50,.45)':'rgba(198,40,40,.45)'}})));
ch.timeScale().fitContent(); new ResizeObserver(()=>ch.applyOptions({{width:el.clientWidth}})).observe(el);
function show(b){{document.querySelectorAll('.bot').forEach(d=>d.style.display=d.id==='t_'+b?'block':'none')}}
show('{next((b for b in bot_rows if bot_rows[b]), 'trade')}');
</script></body></html>"""


def build_day(day, client):
    import analyze_trades as A
    first = {b: scanned(root, day) for b, root in BOTS.items()}
    syms = sorted(set(first["trade"]) | set(first["star"]))
    if not syms:
        print(day, "no scanner lists"); return
    dec = {b: read_decisions(root, day) for b, root in BOTS.items()}
    tr = {b: trades(root, day) for b, root in BOTS.items()}
    d = OUT / day; d.mkdir(parents=True, exist_ok=True)
    rows = []
    for s in syms:
        try:
            bars = A.load_bars(client, s, day, None)
        except Exception:
            bars = []
        if not bars:
            continue
        bt = {b: [t for t in tr[b] if t["symbol"] == s] for b in BOTS}
        page = stock_page(day, s, bars, {b: dec[b][0].get(s, {}) for b in BOTS}, {b: dec[b][1].get(s, {}) for b in BOTS},
                          {b: v for b, v in bt.items() if v}, {b: first[b][s] for b in BOTS if s in first[b]})
        (d / f"{s}.html").write_text(page)
        O = bars[0]["o"]; H = max(b["h"] for b in bars); C = bars[-1]["c"]
        pl = sum(t.get("pl_dollars") or 0 for v in bt.values() for t in v)
        n = sum(len(v) for v in bt.values())
        buys = {b: sum(1 for r in dec[b][0].get(s, {}).values() if r.get("state") == "BUY") for b in BOTS}
        rows.append((s, first["trade"].get(s, ""), first["star"].get(s, ""), (H / O - 1) * 100, (C / O - 1) * 100, n, pl, buys))
    body = "".join(
        f"<tr><td><a href='{s}.html'>{s}</a></td><td>{ft}</td><td>{fs}</td><td class='{'pos' if hi >= 3 else ''}'>{hi:+.1f}%</td>"
        f"<td class='{'pos' if cl > 0 else 'neg'}'>{cl:+.1f}%</td><td>{bu['trade']}/{bu['star']}</td><td>{n or ''}</td>"
        f"<td class='{'pos' if pl > 0 else 'neg' if pl < 0 else ''}'>{f'{pl:+.2f}' if n else ''}</td></tr>"
        for s, ft, fs, hi, cl, n, pl, bu in sorted(rows, key=lambda r: -r[3]))
    (d / "index.html").write_text(f"""<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scanner stocks {day}</title>{CSS}</head><body><p class="mute"><a href="../index.html">&larr; all days</a></p>
<h1>Scanner stocks &middot; {day}</h1><p class="mute">{len(rows)} stocks selected by either bot's scanner (9:30 list + rescans), sorted by the
day's high over the open. Click a stock for its chart and the bot's indicators minute by minute.</p>
<div class="wrap"><table><tr><th>stock</th><th>trade bot list from</th><th>Star bot list from</th><th>day high vs open</th><th>close vs open</th>
<th>v2 BUY polls (trade/Star)</th><th>trades</th><th>P/L $</th></tr>{body}</table></div></body></html>""")
    print(day, len(rows), "stocks", flush=True)


def build_index():
    days = sorted((p.name for p in OUT.iterdir() if p.is_dir() and (p / "index.html").exists()), reverse=True)
    (OUT / "index.html").write_text(f"""<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Past days</title>{CSS}</head><body><h1>Past days</h1><p class="mute">Every stock the scanners selected, with chart and the bot's
indicators per minute.</p><ul>{''.join(f"<li><a href='{d}/index.html'>{d}</a></li>" for d in days)}</ul></body></html>""")


def main(args):
    from alpaca_client import get_client
    c = get_client()
    if "--all" in args:
        days = sorted({os.path.basename(f)[:10] for f in glob.glob(str(TRADE / "data" / "decisions" / "2026-*.jsonl"))})
    else:
        days = args or [date.today().isoformat()]
    for d in days:
        build_day(d, c)
    build_index()


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