#!/usr/bin/env python3
"""Pinnacle AH 下载 v2 - 使用 commence_time 作为日期"""
import os, time
from pathlib import Path
from datetime import datetime, timedelta
import pandas as pd
import requests
import warnings
warnings.filterwarnings('ignore')

ODDS_DIR = Path('/root/.openclaw/workspace-flj_bot1/football-predictor-repro/artifacts/odds_api_history')
ODDS_API_KEY = os.environ.get('ODDS_API_KEY', '08e1ca367452b446f7bee50bbc9d0fc1')

SPORT_LEAGUES = {
    'soccer_epl':                    ('e0', '英超'),
    'soccer_efl_champ':              ('e1', '英冠'),
    'soccer_england_league1':        ('e2', '英甲'),
    'soccer_england_league2':        ('e2n', '英乙'),
    'soccer_germany_bundesliga':      ('d1', '德甲'),
    'soccer_germany_bundesliga2':    ('d2', '德乙'),
    'soccer_france_ligue_one':       ('f1', '法甲'),
    'soccer_france_ligue_two':       ('f2', '法乙'),
    'soccer_italy_serie_a':          ('i1', '意甲'),
    'soccer_italy_serie_b':         ('i2', '意乙'),
    'soccer_spain_la_liga':          ('sp1', '西甲'),
    'soccer_spain_segunda_division': ('sp2', '西乙'),
    'soccer_turkey_super_league':   ('tr1', '土超'),
    'soccer_portugal_primeira_liga': ('p1', '葡超'),
    'soccer_belgium_first_div':     ('b1', '比甲'),
    'soccer_greece_super_league':    ('g1', '希腊超'),
    'soccer_netherlands_eredivisie': ('n1', '荷兰甲'),
    'soccer_norway_eliteserien':    ('n2', '荷兰乙'),
    'soccer_spl':                   ('sc1', '苏超'),
    'soccer_austria_bundesliga':     ('a1', '澳超'),
    'soccer_usa_mls':              ('m1', '美职联'),
    'soccer_japan_j_league':       ('j1', '日职联'),
    'soccer_russia_premier_league': ('r1', '俄超'),
    'soccer_brazil_campeonato':    ('br1', '巴甲'),
    'soccer_poland_ekstraklasa':   ('pl1', '波兰甲'),
    'soccer_denmark_superliga':   ('dk1', '丹麦超'),
    'soccer_czech republic_liga':   ('cz1', '捷克甲'),
}

def season_dates(year):
    start = datetime(year, 8, 1)
    end = datetime(year+1, 5, 31)
    dates = []
    cur = start
    while cur <= end:
        dates.append(cur.strftime('%Y-%m-%d'))
        cur += timedelta(days=4)
    return dates

def fetch_pinn(sport_key, dates, code):
    all_rows = []
    out_file = ODDS_DIR / f'{code}_pinn_ah.csv'
    existing = set()
    if out_file.exists():
        try:
            ex = pd.read_csv(out_file, usecols=['Date','HomeTeam','AwayTeam'])
            existing = set(zip(ex['Date'].astype(str), ex['HomeTeam'].astype(str), ex['AwayTeam'].astype(str)))
        except: pass
    new = 0
    for d in dates:
        try:
            r = requests.get(f'https://api.the-odds-api.com/v4/sports/{sport_key}/odds-history',
                params={'apiKey': ODDS_API_KEY, 'regions': 'eu', 'markets': 'spreads', 'date': f'{d}T12:00:00Z'},
                timeout=20)
            if r.status_code == 429:
                time.sleep(10)
                r = requests.get(f'https://api.the-odds-api.com/v4/sports/{sport_key}/odds-history',
                    params={'apiKey': ODDS_API_KEY, 'regions': 'eu', 'markets': 'spreads', 'date': f'{d}T12:00:00Z'},
                    timeout=20)
            if r.status_code == 200:
                for event in r.json().get('data', []):
                    home = event.get('home_team', '')
                    away = event.get('away_team', '')
                    mtime = event.get('commence_time', '')[:10]
                    key = (mtime, home, away)
                    if key in existing: continue
                    for bm in event.get('bookmakers', []):
                        if bm.get('key') != 'pinnacle': continue
                        for mkt in bm.get('markets', []):
                            if mkt.get('key') != 'spreads': continue
                            outcomes = mkt.get('outcomes', [])
                            if len(outcomes) < 2: continue
                            home_pt = away_pt = home_pr = away_pr = None
                            for o in outcomes:
                                pt = o.get('point')
                                if pt is None: continue
                                try: pt_v = float(pt)
                                except: continue
                                pr = o.get('price')
                                if pr is None or float(pr) <= 1: continue
                                if pt_v > 0:
                                    home_pt = pt_v; home_pr = float(pr)
                                elif pt_v < 0:
                                    away_pt = abs(pt_v); away_pr = float(pr)
                            if home_pt and away_pt and home_pr and away_pr:
                                all_rows.append({'Date': mtime, 'HomeTeam': home, 'AwayTeam': away,
                                    'AHh': home_pt, 'B365AHH': home_pr, 'B365AHA': away_pr, 'odds_source': 'pinnacle'})
                                new += 1; existing.add(key)
            time.sleep(0.1)
        except: continue
    if all_rows:
        df = pd.DataFrame(all_rows)
        ex_df = pd.read_csv(out_file) if out_file.exists() else pd.DataFrame()
        comb = pd.concat([ex_df, df], ignore_index=True).drop_duplicates(subset=['Date','HomeTeam','AwayTeam'])
        comb.to_csv(out_file, index=False, encoding='utf-8')
        print(f'  +{new} → {len(comb)}', flush=True)
    return new

print('Pinnacle AH v2 (commence_time + 优化采样)')
for sport_key, (code, name) in SPORT_LEAGUES.items():
    print(f'[{code}] {name}', flush=True)
    total = 0
    for year in [2023, 2024, 2025]:
        dates = season_dates(year)
        print(f'  {year} ({len(dates)}天)...', end=' ', flush=True)
        n = fetch_pinn(sport_key, dates, code)
        total += n
    print(f'  +{total}', flush=True)
print('DONE')
