"""Combina i NUOVI edge (pairs + TSM01) col MASTER esistente: migliora il portafoglio?

Aggiunge al MASTER a 9 sleeve (6 fade + 3 honest) due nuove fonti scoperte
nell'esplorazione, poco correlate:
  - PAIRS market-neutral (ETH/BTC, LTC/ETH, ADA/ETH)  -> corr ~0 col mercato
  - TSM01 (TSMOM multi-orizzonte + risk-off)          -> corr ~0.53 con ROT02

Misura correlazione delle nuove sleeve vs esistenti e confronta MASTER-9 vs
MASTER-esteso su Ret/CAGR/DD/Sharpe, FULL e OOS (finestra comune 2021-2026).
"""
from __future__ import annotations

import sys
from pathlib import Path

import pandas as pd

PROJECT_ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(PROJECT_ROOT))

from scripts.analysis.combine_portfolio import (
    build_all_sleeves, port_returns, metrics, yearly_returns, SPLIT, OOS_DATE, IDX,
)
from scripts.analysis.honest_improve2 import _daily_equity, _norm
from scripts.analysis.pairs_research import pairs_sim
from scripts.analysis.tsmom_research import tsmom_sim


def daily_from(eq_ts, eq_v):
    return _norm(_daily_equity(eq_ts, eq_v, IDX))


def main():
    print("Costruzione equity (puo' richiedere ~1-2 min)...\n")
    S = build_all_sleeves()                       # 9 sleeve esistenti

    # nuove sleeve: i 6 pairs robusti di PR01 + TSM01
    from scripts.strategies.PR01_pairs_reversion import PAIRS
    new = {}
    for a, b, p in PAIRS:
        r = pairs_sim(a, b, **p)
        new[f"PR_{a}{b}"] = daily_from(r["eq_ts"], r["eq_v"])
    t = tsmom_sim()
    new["TSM01"] = daily_from(t["eq_ts"], t["eq_v"])

    allS = {**S, **new}

    # --- correlazione nuove vs esistenti ---
    dr = pd.DataFrame({k: v.pct_change().fillna(0.0) for k, v in allS.items()})
    corr = dr.corr()
    old_k = list(S); new_k = list(new)
    print("=" * 88)
    print("  CORRELAZIONE rendimenti giornalieri — NUOVE (righe) vs media esistenti")
    print("=" * 88)
    for nk in new_k:
        avg = corr.loc[nk, old_k].mean()
        mx = corr.loc[nk, old_k].abs().max()
        print(f"  {nk:<12s} corr media col MASTER-9 = {avg:+.2f}   |max| = {mx:.2f}")

    # --- confronto portafogli ---
    def line(label, members):
        pr = port_returns(members)
        f, o = metrics(pr), metrics(pr, lo=SPLIT)
        print(f"  {label:<26s}{f['ret']:>+9.0f}{f['cagr']:>7.0f}{f['dd']:>7.1f}{f['sharpe']:>7.2f}"
              f"   | {o['ret']:>+9.0f}{o['dd']:>7.1f}{o['sharpe']:>7.2f}")
        return pr

    print("\n" + "=" * 96)
    print(f"  MASTER-9 vs MASTER-ESTESO (con pairs+TSM01) | OOS da {OOS_DATE} | equal-weight daily")
    print("=" * 96)
    print(f"  {'portafoglio':<26s}{'Ret%':>9s}{'CAGR':>7s}{'DD%':>7s}{'Shrp':>7s}"
          f"   | {'oRet%':>9s}{'oDD%':>7s}{'oShrp':>7s}")
    print("  " + "-" * 92)
    pairs_only = {k: v for k, v in new.items() if k.startswith('PR_')}
    line(f"MASTER-9 (base)", S)
    line(f"MASTER +pairs ({len(S)+len(pairs_only)})", {**S, **pairs_only})
    line(f"MASTER +TSM01 ({len(S)+1})", {**S, "TSM01": new["TSM01"]})
    pr_all = line(f"MASTER-esteso ({len(allS)})", allS)
    print("  " + "-" * 92)
    pa = yearly_returns(pr_all)
    print("  MASTER-esteso per-anno: " + "  ".join(f"{y}:{v:+.0f}%" for y, v in pa.items()))
    print("\n  Se il MASTER-esteso ha DD piu' basso e/o Sharpe piu' alto del MASTER-9, le nuove")
    print("  famiglie aggiungono valore (diversificazione da fonti scorrelate).")


if __name__ == "__main__":
    main()
