"""
c3_deep_report.py -- [2026-10-21] the user: "go deeper" on the 6-month C3 (3 up bars) test, mainly the base buys it BLOCKED.
Reads c3_deep_6m.jsonl (c3_deep.py: BASE / C3_green3 / C3_wait with the 3-bar state at every signal + the path after the buy).
    python3 c3_deep_report.py     -> c3_deep_report.log
"""
import json
from collections import defaultdict
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
COST, BS_TOL, VOL_RATIO = 0.2, 0.05, 0.9
T = [json.loads(l) for l in open(HERE / "c3_deep_6m.jsonl")]
days = sorted({t["day"] for t in T}); half = days[len(days) // 2]
pl = lambda t: t["pl_gross"] - COST


def line(lab, X, extra=True):
    if not X:
        print(f"   {lab:46} 0"); return
    p = np.array([pl(t) for t in X]); h = np.array([t["day"] < half for t in X])
    s = (f"   {lab:46} {len(p):5} {p.mean():+7.3f}% {np.mean(p > 0):4.0%} | {p[h].mean() if h.any() else 0:+7.3f} "
         f"{p[~h].mean() if (~h).any() else 0:+7.3f} | total {p.sum():+7.1f}%")
    if extra:
        mx = np.array([t["max60"] or 0 for t in X])
        s += f" | reached +1%: {np.mean(mx >= 1):4.0%}  +2%: {np.mean(mx >= 2):4.0%}"
    print(s)


def reasons(c):
    if not c:
        return ["no data"]
    r = []
    if c["up_bars"] < 3:
        r.append(f"price: {c['up_bars']} of 3 up bars")
    if c["bs3"] is not None and c["bs3_prev"] is not None and c["bs3"] < c["bs3_prev"] - BS_TOL:
        r.append("buy share falling")
    if c["vol3_x"] is not None and c["vol3_x"] < VOL_RATIO:
        r.append("volume falling")
    return r


base = [t for t in T if t["run"] == "BASE"]
key = lambda t: (t["day"], t["sym"], t["buy_m"])
g3 = {key(t) for t in T if t["run"] == "C3_green3"}
blocked = [t for t in base if key(t) not in g3]
kept = [t for t in base if key(t) in g3]
print(f"{len(days)} days {days[0]}..{days[-1]}; per trade after {COST}% cost | trades, per trade, win, half 1, half 2\n")
print("1. TOTALS")
line("all base buys", base); line("blocked by the 3-bar rule", blocked); line("kept (3 up bars at the signal)", kept)
for rn in ("C3_green3", "C3_wait"):
    line(f"run {rn}", [t for t in T if t["run"] == rn])

print("\n2. THE BLOCKED BUYS: WHAT THEY TURNED INTO")
p = np.array([pl(t) for t in blocked])
for lab, m in (("big winners (> +2%)", p > 2), ("winners +0.5..+2%", (p > 0.5) & (p <= 2)), ("small -0.5..+0.5%", (p >= -0.5) & (p <= 0.5)),
               ("losers -2..-0.5%", (p < -0.5) & (p >= -2)), ("big losers (< -2%)", p < -2)):
    print(f"   {lab:30} {m.sum():4}  sum {p[m].sum():+7.1f}%")
print(f"   -> profit given up {p[p > 0].sum():+.1f}%, losses avoided {p[p < 0].sum():+.1f}%")
print("   biggest winners it blocked:")
for t in sorted(blocked, key=pl, reverse=True)[:10]:
    print(f"      {t['day']} {t['sym']:6} {9 + (30 + t['buy_m']) // 60}:{(30 + t['buy_m']) % 60:02d} {pl(t):+6.2f}%  bars up {t['c3']['up_bars'] if t['c3'] else '?'}  {reasons(t['c3'])}")

print("\n3. WHY IT BLOCKED (a buy can fail several checks)")
by = defaultdict(list)
for t in blocked:
    for r in reasons(t["c3"]):
        by[r].append(t)
    if len(reasons(t["c3"])) == 1:
        by["ONLY " + reasons(t["c3"])[0]].append(t)
for r in sorted(by):
    line(r, by[r])
print("   all base buys by number of up bars at the signal:")
for n in range(4):
    line(f"   {n} of the last 3 bars up", [t for t in base if t["c3"] and t["c3"]["up_bars"] == n])

print("\n4. WAS IT AN ENTRY OR AN EXIT PROBLEM? (blocked buys)")
mx = np.array([t["max60"] or 0 for t in blocked]); md = np.array([t["max_day"] or 0 for t in blocked])
mn = np.array([t["min60"] or 0 for t in blocked])
print(f"   best price in 60 min: median {np.median(mx):+.2f}%; reached +1% {np.mean(mx >= 1):.0%}, +2% {np.mean(mx >= 2):.0%}")
print(f"   worst price in 60 min: median {np.median(mn):+.2f}%; hit -1% {np.mean(mn <= -1):.0%}, -2% {np.mean(mn <= -2):.0%}")
print(f"   best price rest of day: median {np.median(md):+.2f}%;  losers that were up >= +1% first: "
      f"{sum(1 for t in blocked if pl(t) < 0 and (t['max60'] or 0) >= 1)} of {sum(1 for t in blocked if pl(t) < 0)}")
print("   by exit reason:")
ex = defaultdict(list)
for t in blocked:
    ex[t["why"]].append(t)
for w in sorted(ex, key=lambda w: -len(ex[w])):
    line(f"   {w}", ex[w], extra=False)

print("\n5. BLOCKED BUYS BY SITUATION / TIME / FEATURES")
for s in ("S1", "S2", "S3", "S6", "S7", "S8", "S10", "S11", "S16", "S17", "S20", "S21"):
    X = [t for t in blocked if s in t["tags"]]
    if len(X) >= 10:
        line(f"   {s}", X, extra=False)
for lab, lo, hi in (("9:45-10:30", 15, 60), ("10:30-12:00", 60, 150), ("12:00-14:00", 150, 270), ("14:00-15:15", 270, 346)):
    line(f"   bought {lab}", [t for t in blocked if lo <= t["buy_m"] < hi], extra=False)
for f, cuts in (("ext_open_pct", (-99, 0, 1.5, 3, 99)), ("vwap_pct", (-99, 0.5, 1, 1.5, 99)), ("atr_pct", (0, 6, 8, 12, 999)),
                ("vol15_x", (0, 2, 3, 5, 999)), ("buy_share15", (0, 0.6, 0.65, 0.7, 1.1))):
    for lo, hi in zip(cuts, cuts[1:]):
        X = [t for t in blocked if t["feat"].get(f) is not None and lo <= t["feat"][f] < hi]
        line(f"   {f} {lo}..{hi}", X, extra=False)

print("\n6. SAME STOCK-DAY: did the waiting rule buy the blocked stocks later?")
w = defaultdict(list)
for t in T:
    if t["run"] == "C3_wait":
        w[(t["day"], t["sym"])].append(t)
later = [t for t in blocked if any(u["buy_m"] > t["buy_m"] for u in w.get((t["day"], t["sym"]), []))]
print(f"   blocked buys whose stock the wait rule bought later the same day: {len(later)} of {len(blocked)}")
line("   those blocked buys (base result)", later, extra=False)
line("   the wait rule's later buys of them", [u for t in later for u in w[(t["day"], t["sym"])] if u["buy_m"] > t["buy_m"]][:len(later)], extra=False)
print("C3REPORT_DONE")
