"""
Hypothetical: instead of betting YES on the underdog, bet NO on the favorite.
NO on opposite side wins on (target wins) OR (draw). Tests user's hypothesis
that many losing bets were draws.
"""
import json
from pathlib import Path

h = json.loads(Path("E:/soccer/bet_history.json").read_text())

# Filter to Kalshi-tracked bets with a result and score
ks_bets = []
for b in h:
    if not b.get("kalshi_ask") or not b.get("kalshi_stake"):
        continue
    if b.get("won") is None:
        continue
    if not b.get("ftr"):
        continue
    ks_bets.append(b)

# Assume draw priced at 25c (typical soccer mid)
DRAW_PRICE_ASSUMED = 0.25

n = len(ks_bets)
orig_wins = sum(1 for b in ks_bets if b["won"])
orig_pnl = sum(b.get("kalshi_pnl", 0) for b in ks_bets)

# Classify losses
lost_to_draw = 0
lost_to_other = 0
for b in ks_bets:
    if b["won"]:
        continue
    if b["ftr"] == "D":
        lost_to_draw += 1
    else:
        lost_to_other += 1

# Hypothetical NO-side bet
alt_wins = 0
alt_pnl_total = 0.0
no_too_expensive = 0  # skip if NO price > 0.99
for b in ks_bets:
    side = b["bet"]
    is_home = "HOME" in side.upper()
    ftr = b["ftr"]
    stake = b["kalshi_stake"]
    kalshi_ask = b["kalshi_ask"]

    # NO price ≈ kalshi_ask + draw_price (since NO_opposite = own_side + draw)
    no_price = kalshi_ask + DRAW_PRICE_ASSUMED
    if no_price >= 0.99:
        no_too_expensive += 1
        continue

    # NO on opposite side wins when opposite team didn't win
    if is_home:
        # bet was HOME YES; alt is NO AWAY → wins if ftr != A
        alt_wins_this = ftr != "A"
    else:
        # bet was AWAY YES; alt is NO HOME → wins if ftr != H
        alt_wins_this = ftr != "H"

    if alt_wins_this:
        alt_wins += 1
        win_profit = stake * (1 - no_price) / no_price
        alt_pnl_total += win_profit
    else:
        alt_pnl_total -= stake

# ── Report ──
print("=" * 70)
print(f"Sample: {n} resolved Kalshi bets")
print("=" * 70)
print(f"\nORIGINAL (YES on underdog):")
print(f"  Wins: {orig_wins} / {n}  ({orig_wins/n*100:.1f}% WR)")
print(f"  P&L:  ${orig_pnl:+.2f}")
print(f"  Losses breakdown:")
print(f"    Lost to DRAW:        {lost_to_draw}  ({lost_to_draw/n*100:.1f}% of total)")
print(f"    Lost to OTHER team:  {lost_to_other}  ({lost_to_other/n*100:.1f}% of total)")
print(f"  -> {lost_to_draw/(lost_to_draw+lost_to_other)*100:.1f}% of losses are draws")

print(f"\nHYPOTHETICAL (NO on favorite, assuming DRAW priced at {DRAW_PRICE_ASSUMED*100:.0f}c):")
print(f"  Hit rate: {alt_wins} / {n - no_too_expensive}  ({alt_wins/(n-no_too_expensive)*100:.1f}% WR)")
print(f"  P&L:  ${alt_pnl_total:+.2f}")
print(f"  Skipped (NO price would be > 99c): {no_too_expensive}")

print(f"\nDIFFERENCE: ${alt_pnl_total - orig_pnl:+.2f}")

# Sensitivity to draw price assumption
print(f"\nSensitivity to DRAW price assumption:")
print(f"  Draw price | Alt P&L | Alt WR | Delta vs original")
print(f"  -----------+---------+--------+------------------")
for dp in [0.18, 0.22, 0.25, 0.28, 0.32]:
    alt_pnl_s = 0.0; alt_wins_s = 0; n_inc = 0
    for b in ks_bets:
        side = b["bet"]; is_home = "HOME" in side.upper()
        no_price = b["kalshi_ask"] + dp
        if no_price >= 0.99: continue
        n_inc += 1
        stake = b["kalshi_stake"]
        if is_home:
            wins = b["ftr"] != "A"
        else:
            wins = b["ftr"] != "H"
        if wins:
            alt_wins_s += 1
            alt_pnl_s += stake * (1 - no_price) / no_price
        else:
            alt_pnl_s -= stake
    print(f"  {dp*100:>6.0f}c    | ${alt_pnl_s:>+6.2f} | {alt_wins_s/n_inc*100:>5.1f}% | ${alt_pnl_s - orig_pnl:>+6.2f}")
