"""Is there an exploitable edge BETWEEN Polymarket and Kalshi?
Read-only analysis over the 47-day backfill (poly mid vs kalshi mid, timestamp-aligned).
Pre-KO rows only. No trades."""
import csv, glob, math
from collections import defaultdict

rows = []
for f in glob.glob(r'E:\soccer\poly_backfill\*_polybackfill.csv'):
    with open(f, encoding='utf-8') as fh:
        for r in csv.DictReader(fh):
            rows.append(r)

def fl(x):
    try:
        return float(x)
    except (TypeError, ValueError):
        return None

# Keep pre-KO rows where BOTH books have a mid on the same side.
def col(r, k):
    return fl(r.get(k))

# Build per-fixture: take the LAST pre-KO snapshot that has both books (closest to KO).
# Key by game_date+home+away.
fix = {}
for r in rows:
    el = col(r, 'elapsed')
    if el is None or el >= 0:   # pre-KO only
        continue
    rh, ra = col(r, 'result_home'), col(r, 'result_away')
    if rh is None or ra is None:
        continue
    key = (r['game_date'], r['home'], r['away'])
    rec = {
        'kh': col(r, 'kalshi_home_mid'), 'ka': col(r, 'kalshi_away_mid'),
        'kh_ask': col(r, 'kalshi_home_ask'), 'ka_ask': col(r, 'kalshi_away_ask'),
        'ph': col(r, 'poly_home_price'),   'pa': col(r, 'poly_away_price'),
        'pfh': col(r, 'pin_fair_home'),    'pfa': col(r, 'pin_fair_away'),
        'home_win': 1 if rh > ra else 0,
        'away_win': 1 if ra > rh else 0,
        'el': el,
    }
    # need both books on at least one side
    if (rec['kh'] and rec['ph']) or (rec['ka'] and rec['pa']):
        prev = fix.get(key)
        if prev is None or el > prev['el']:   # closer to KO
            fix[key] = rec

print(f"fixtures with both-book pre-KO snapshot: {len(fix)}\n")

# ---- 1) Price divergence distribution (poly_mid - kalshi_mid) ----
def stats(xs):
    xs = sorted(xs)
    n = len(xs)
    if not n:
        return None
    mean = sum(xs)/n
    med = xs[n//2]
    return n, mean, med

for side, (pk, kk) in [('HOME', ('ph','kh')), ('AWAY', ('pa','ka'))]:
    diffs = [ (r[pk]-r[kk]) for r in fix.values() if r[pk] and r[kk] ]
    absd  = [abs(d) for d in diffs]
    n, mean, med = stats(diffs)
    _, amean, amed = stats(absd)
    print(f"[{side}] poly-kalshi mid diff: n={n}  mean={mean*100:+.2f}c  median={med*100:+.2f}c  |  |diff| mean={amean*100:.2f}c median={amed*100:.2f}c")
    big = sum(1 for d in absd if d >= 0.03)
    print(f"        |diff|>=3c: {big} ({big/n*100:.0f}%)   |diff|>=5c: {sum(1 for d in absd if d>=0.05)} ({sum(1 for d in absd if d>=0.05)/n*100:.0f}%)")

# ---- 2) When books diverge, which is closer to pin_fair? ----
print("\n--- When |poly-kalshi|>=2c, which book is closer to Pinnacle fair? ---")
for side, (pk, kk, pfk) in [('HOME',('ph','kh','pfh')), ('AWAY',('pa','ka','pfa'))]:
    poly_closer = kalshi_closer = 0
    for r in fix.values():
        if not (r[pk] and r[kk] and r[pfk]):
            continue
        if abs(r[pk]-r[kk]) < 0.02:
            continue
        if abs(r[pk]-r[pfk]) < abs(r[kk]-r[pfk]):
            poly_closer += 1
        else:
            kalshi_closer += 1
    tot = poly_closer+kalshi_closer
    if tot:
        print(f"[{side}] poly closer {poly_closer} ({poly_closer/tot*100:.0f}%) | kalshi closer {kalshi_closer} ({kalshi_closer/tot*100:.0f}%)  (n={tot})")

# ---- 3) Settlement: bet the side where one book is CHEAPER than the other ----
# Hypothesis: the lower-priced book is the value buy. Bet that team at that book's mid.
# ROI = sum( (1-price)/price if win else -1 ) / n
def roi_test(label, picks):
    # picks = list of (price, won)
    if not picks:
        print(f"  {label}: no bets"); return
    pnl = 0.0
    w = 0
    for price, won in picks:
        if won:
            pnl += (1-price)/price
            w += 1
        else:
            pnl -= 1
    n = len(picks)
    roi = pnl/n*100
    wr = w/n*100
    # t-stat on per-bet pnl
    mean = pnl/n
    var = sum(( ((1-p)/p if wn else -1) - mean)**2 for p,wn in picks)/(n-1) if n>1 else 0
    se = math.sqrt(var/n) if var else 0
    t = mean/se if se else 0
    print(f"  {label}: n={n} WR={wr:.1f}% ROI={roi:+.1f}% t={t:.2f}")

print("\n--- Strategy A: bet the team that is CHEAPER on Poly than Kalshi (by >=Xc), at poly mid ---")
for thr in (0.02, 0.03, 0.05):
    picks = []
    for r in fix.values():
        for pk, kk, won in [('ph','kh',r['home_win']), ('pa','ka',r['away_win'])]:
            if r[pk] and r[kk] and (r[kk]-r[pk]) >= thr:   # poly cheaper
                picks.append((r[pk], won))
    roi_test(f"poly-cheaper>={int(thr*100)}c (buy@poly_mid)", picks)

print("\n--- Strategy B: bet the team that is CHEAPER on Kalshi than Poly (by >=Xc), at kalshi mid ---")
for thr in (0.02, 0.03, 0.05):
    picks = []
    for r in fix.values():
        for pk, kk, won in [('ph','kh',r['home_win']), ('pa','ka',r['away_win'])]:
            if r[pk] and r[kk] and (r[pk]-r[kk]) >= thr:   # kalshi cheaper
                picks.append((r[kk], won))
    roi_test(f"kalshi-cheaper>={int(thr*100)}c (buy@kalshi_mid)", picks)

print("\n--- Baseline: bet EVERY home and away at each book's mid (no divergence filter) ---")
for book, hk, ak in [('poly','ph','pa'), ('kalshi','kh','ka')]:
    picks = []
    for r in fix.values():
        if r[hk]: picks.append((r[hk], r['home_win']))
        if r[ak]: picks.append((r[ak], r['away_win']))
    roi_test(f"{book} all sides @mid", picks)
