"""
score_weights_2y.py -- [2026-10-06] PRIORITY: the scanner score re-weighting on 2 years (pm_history/*.json.gz from
premarket_history.py). Same questions as score_weights_study.py (20 days), now year by year:
  1. each indicator's AUC vs MOVER (range >= 5%), UP-RUN (high >= open + 3%), UP-DAY (close > open)
  2. the daily top 30 by the CURRENT score vs the NEW score (scanner.score_v3: ATR% 35, |gap| 25,
     premarket volume 20, premarket volume as a share of the daily average 10, resistance 10) vs a few
     alternative weightings -- share of picks that were movers / up-runs, per year
  3. a simple weight search on YEAR 1 only, checked on YEAR 2 (no peeking)
    python3 score_weights_2y.py
"""
import gzip
import itertools
import json
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
SPLIT = "2025-10-01"


def auc(x, y):
    x = np.asarray(x, float); y = np.asarray(y, bool)
    ok = ~np.isnan(x); x, y = x[ok], y[ok]
    if y.all() or (~y).all():
        return np.nan
    r = x.argsort().argsort() + 1.0
    return (r[y].sum() - y.sum() * (y.sum() + 1) / 2) / (y.sum() * (~y).sum())


def cl(x, lo=0.0, hi=1.0):
    return np.clip(x, lo, hi)


def main():
    R = []
    for f in sorted((HERE / "pm_history").glob("20*.json.gz")):
        d = json.load(gzip.open(f, "rt"))
        for r in d["rows"]:
            r["date"] = d["date"]; R.append(r)
    days = sorted({r["date"] for r in R})
    print(f"{len(R)} stock-days, {len(days)} days {days[0]}..{days[-1]} (year 1 < {SPLIT} <= year 2)")
    g = lambda k: np.array([r.get(k) if r.get(k) is not None else np.nan for r in R], float)
    date = np.array([r["date"] for r in R]); Y1 = date < SPLIT
    o, h, l, c = g("o"), g("h"), g("l"), g("c")
    Y = {"MOVER": h / l - 1 >= 0.05, "UP-RUN": h / o - 1 >= 0.03, "UP-DAY": c > o}
    pmv, adv, gap, st, dist, atr, pc = g("pm_vol"), g("adv20"), g("gap"), g("strength"), g("dist"), g("atr"), g("prev_close")
    atrp = atr / pc * 100
    atrp_ok = np.where(atrp > 25, 0, np.nan_to_num(atrp))
    F = {
        "relative volume (pm vol / daily avg)": pmv / adv, "premarket volume": pmv, "gap, signed": gap,
        "premarket strength": st, "distance to resistance": np.where(np.isnan(dist), 99, dist),
        "gap size (either way)": np.abs(gap), "daily ATR %": atrp_ok,
    }
    old = (30 * cl(pmv / adv / 3) + 20 * cl(pmv / 1e5) + 20 * cl(gap / 10, -1, 1) + 15 * cl(st / 5, -1, 1)
           + 15 * np.where(np.isnan(dist), 1, cl(dist / 8)))
    new = (35 * cl(atrp_ok / 10) + 25 * cl(np.abs(gap) / 10) + 20 * cl(pmv / 1e5) + 10 * cl(pmv / adv / 0.05)
           + 10 * np.where(np.isnan(dist), 1, cl(dist / 8)))
    F["CURRENT SCORE"] = old; F["NEW SCORE (v3)"] = new
    print("\nAUC (0.50 = no information)        " + "".join(f"{k:>22}" for k in Y) + "    [year 1 / year 2]")
    for k, x in F.items():
        print(f"  {k:36}" + "".join(f"{auc(x, y):8.2f} [{auc(x[Y1], y[Y1]):.2f}/{auc(x[~Y1], y[~Y1]):.2f}]" for y in Y.values()))

    def top30(score):
        pick = np.zeros(len(score), bool)
        for d in days:
            k = np.where(date == d)[0]
            pick[k[np.argsort(-score[k])[:30]]] = True
        return pick

    def report(name, score):
        p = top30(score)
        print(f"  {name:44}" + "".join(f" {k} {y[p].mean():.0%} ({y[p & Y1].mean():.0%}/{y[p & ~Y1].mean():.0%})" for k, y in Y.items()))
    print("\ntop 30 per day -> share of picks that were (year 1 / year 2):")
    report("CURRENT score", old)
    report("NEW score (v3)", new)
    report("all stocks (no selection)", np.random.default_rng(0).random(len(R)))
    # weight search on year 1 only
    parts = {"atr": cl(atrp_ok / 10), "agap": cl(np.abs(gap) / 10), "pmv": cl(pmv / 1e5), "rv": cl(pmv / adv / 0.05),
             "res": np.where(np.isnan(dist), 1, cl(dist / 8))}
    best = None
    for w in itertools.product((0, 10, 20, 30, 40), repeat=5):
        if sum(w) == 0:
            continue
        sc = sum(wi * parts[k] for wi, k in zip(w, parts))
        p = top30(sc)
        val = Y["UP-RUN"][p & Y1].mean()
        if best is None or val > best[0]:
            best = (val, w)
    w = best[1]
    sc = sum(wi * parts[k] for wi, k in zip(w, parts))
    print(f"\nbest weights found on YEAR 1 only (target: up-runs) atr/|gap|/pm vol/rvol share/resistance = {w}")
    report("year-1-fitted weights (check year 2!)", sc)


if __name__ == "__main__":
    main()
