"""
Test the user's hypothesis: did the edge drop after adding BSA, USL, Sweden, Norway?
Reconstructs the exact signal set (mirrors strategy.ipynb / _ideas_study.py) but does
NOT apply EXCLUDE, so we can see every league's contribution. Pure analysis.
"""
import os, sys
import numpy as np, pandas as pd
from scipy import stats
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
os.chdir(r'E:\soccer')
pd.set_option('display.width', 160)

EDGE_LO, EDGE_HI = 0.05, 0.12
SIDES = ['home', 'away']
GAME = ['game_date', 'home', 'away']
SIDE_LETTER = {'home': 'H', 'away': 'A'}

dfs = [pd.read_csv('live_data/' + f) for f in os.listdir('live_data')
       if f.endswith('.csv') and f != 'test.csv']
df = pd.concat(dfs, ignore_index=True)
df['ts'] = pd.to_datetime(df['timestamp'], format='mixed', errors='coerce')
df = df[df['ts'].notna()]
pre = df[(df['status'] == 'PRE') & (df['elapsed'] < 0)].copy()
# NOTE: deliberately NOT excluding any league here

entries = []
for side in SIDES:
    c = f'clv_kalshi_{side}'
    q = pre[(pre[c] >= EDGE_LO) & (pre[c] < EDGE_HI)].copy()
    q = q.sort_values('ts').drop_duplicates(GAME, keep='first')
    q['side'] = side
    q['clv'] = q[c]
    q['ask'] = q[f'kalshi_{side}_ask']
    q['p_win'] = q[f'pin_fair_{side}']
    entries.append(q)
sig = pd.concat(entries, ignore_index=True)
sig = sig.sort_values('clv', ascending=False).drop_duplicates(GAME, keep='first')
sig = sig.sort_values('ts').reset_index(drop=True)
sig['odds'] = np.where((sig['ask'] > 0), 1.0 / sig['ask'], np.nan)
sig['won'] = (sig['ftr'] == sig['side'].map(SIDE_LETTER)).astype(int)
sig = sig.dropna(subset=['odds', 'p_win', 'ftr']).reset_index(drop=True)
sig['net'] = np.where(sig['won'] == 1, sig['odds'] - 1, -1.0)

def zp(net):
    n = len(net)
    if n < 2:
        return float('nan'), float('nan')
    z = net.mean() / (net.std(ddof=1) / np.sqrt(n))
    return z, 1 - stats.norm.cdf(z)

def line(name, s):
    if len(s) == 0:
        print(f'  {name:<26} n=  0'); return
    z, p = zp(s['net'].values)
    print(f'  {name:<26} n={len(s):>4} | WR {s.won.mean():>5.1%} | ROI {s.net.mean():>+7.1%} | net {s.net.sum():>+7.1f}u | z={z:+.2f} p={p:.3f}')

print(f"ALL leagues, no exclusions: n={len(sig)}\n")
print("="*95)
print("PER-LEAGUE breakdown (sorted by net units):")
order = sig.groupby('league')['net'].sum().sort_values().index
for lg in order:
    line(lg, sig[sig.league == lg])

# the 4 leagues the user flagged -- try common code spellings
print("\n" + "="*95)
print("Available league codes:", sorted(sig.league.unique()))
flagged = [c for c in sig.league.unique()
           if any(k in c.upper() for k in ['BSA', 'USL', 'SWE', 'SW', 'NOR', 'NO'])]
print("auto-matched flagged-ish codes:", flagged)

print("\n" + "="*95)
print("WITH vs WITHOUT the flagged leagues (edit FLAGGED below if codes differ):")
FLAGGED = flagged
line('ALL (current live set)', sig)
line('drop USL,BSA only', sig[~sig.league.isin(['USL', 'BSA'])])
line('drop all flagged', sig[~sig.league.isin(FLAGGED)])
line('ONLY flagged', sig[sig.league.isin(FLAGGED)])
