"""
group_confirm_report.py -- [2026-10-09] Step 2 of the user's group design (rows: group_confirm_study.py).
  1. every GROUP x CONFIRMATION option: fires, P/L at the fade (minus the real 0.2% cost), win %, max gain --
     training years (< 2024) and the unseen test years (2024-2026) side by side
  2. per group the best option on the TRAINING years only (>= 150 fires, average > +0.10%) -> that group is a
     WINNER group with that option; groups with no such option = LOGGED ONLY (known losers / no edge)
  3. the combined rule as it would run live: per stock-day, minute by minute, the FIRST fire of the option chosen
     for the group the stock is in at that minute = the group buy signal (the group is then locked)
     -> train vs test, per year; vs the simple baselines (the first fire of any option; v2-like O5 everywhere)
  4. writes the live model: /var/www/screener/trade/data/group_confirm_model.json
    python3 group_confirm_report.py
"""
import csv
import gzip
import json
from collections import defaultdict
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
MODEL = Path("/var/www/screener/trade/data/group_confirm_model.json")
COST, SPLIT, MIN_N, MIN_MEAN = 0.2, "2024-01-01", 150, 0.10


def load():
    R = defaultdict(list)
    with gzip.open(HERE / "group_confirm_rows.csv.gz", "rt") as fh:
        for r in csv.DictReader(fh):
            for k in ("day", "sym", "opt", "group"):
                R[k].append(r[k])
            for k in ("m", "pl", "max_gain", "to_close"):
                R[k].append(float(r[k]))
    D = {k: np.array(v) for k, v in R.items()}
    D["y"] = np.clip(D["pl"], -15, 15) - COST
    D["tr"] = D["day"] < SPLIT
    D["year"] = np.array([d[:4] for d in D["day"]])
    return D


def stats(D, k):
    n = int(k.sum())
    if not n:
        return "     0"
    return f"{n:6} {D['y'][k].mean():+.3f}% win {np.mean(D['y'][k] > 0):3.0%}"


def main():
    D = load()
    groups = sorted(set(D["group"])); opts = sorted(set(D["opt"]))
    print(f"{len(D['day'])} fires, {len(set(D['day']))} days; train < {SPLIT}; P/L at the fade (or 14:30) minus {COST}%")
    print("\n1. GROUP x OPTION   (train | TEST)")
    chosen = {}
    for g in groups:
        kg = D["group"] == g
        print(f"\n  {g}")
        best = None
        for o in opts:
            k = kg & (D["opt"] == o)
            ktr, kte = k & D["tr"], k & ~D["tr"]
            print(f"    {o:16} train {stats(D, ktr):26} | TEST {stats(D, kte)}")
            if ktr.sum() >= MIN_N and D["y"][ktr].mean() > MIN_MEAN and (best is None or D["y"][ktr].mean() > best[1]):
                best = (o, float(D["y"][ktr].mean()), int(ktr.sum()))
        if best:
            chosen[g] = best
            print(f"    -> WINNER group, option {best[0]} (train {best[1]:+.3f}%, n {best[2]})")
        else:
            print("    -> logged only (no option with an edge in the training years)")
    # 3. combined live-like rule
    order = np.lexsort((D["m"], D["sym"], D["day"]))
    first_sig, first_any, first_o5 = {}, {}, {}
    for i in order:
        key = (D["day"][i], D["sym"][i])
        first_any.setdefault(key, i)
        if D["opt"][i] == "O5 smooth trend":
            first_o5.setdefault(key, i)
        g = D["group"][i]
        if key not in first_sig and g in chosen and D["opt"][i] == chosen[g][0]:
            first_sig[key] = i
    print(f"\n2. {len(chosen)} WINNER groups of {len(groups)}: " + "; ".join(f"{g} -> {o[0]}" for g, o in chosen.items()))
    print("\n3. ONE SIGNAL PER STOCK-DAY (first fire), per trade after the cost")
    yrs = sorted(set(D["year"]))
    for lab, idx in (("GROUP SIGNAL (the design)", first_sig), ("first fire of any option", first_any),
                     ("O5 smooth trend everywhere (v2-like)", first_o5)):
        ix = np.array(sorted(idx.values()), int)
        if not len(ix):
            print(f"  {lab}: none"); continue
        tr, te = ix[D["tr"][ix]], ix[~D["tr"][ix]]
        f = lambda z: f"{len(z):5} {D['y'][z].mean():+.3f}% win {np.mean(D['y'][z] > 0):.0%} max gain med {np.median(D['max_gain'][z]):.2f}%" if len(z) else "0"
        print(f"  {lab:38} train {f(tr)} | TEST {f(te)}")
        print("     by year: " + "  ".join(f"{y}: {D['y'][ix[D['year'][ix] == y]].mean():+.2f}% (n {np.sum(D['year'][ix] == y)})"
                                         for y in yrs if np.sum(D["year"][ix] == y)))
    MODEL.parent.mkdir(parents=True, exist_ok=True)
    json.dump({"built": "2026-10-09", "split": SPLIT, "cost": COST, "first_minute": 15, "last_minute": 300,
               "chosen": {g: {"option": o, "train_mean": m, "train_n": n} for g, (o, m, n) in chosen.items()},
               "groups": groups}, open(MODEL, "w"), indent=1)
    print(f"\nmodel -> {MODEL}")


if __name__ == "__main__":
    main()
