#!/usr/bin/env python3
"""Odds-API 补充下载：d2(德乙)、sc1(苏超)、tr1(土超) 的 B365 让球盘赔率"""
import time, os, sys
from pathlib import Path
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import requests

REPO = Path('/root/.openclaw/workspace-flj_bot1/football-predictor-repro')
OUT_DIR = REPO / 'artifacts' / 'odds_api_history'
OUT_DIR.mkdir(parents=True, exist_ok=True)

ODDS_API_KEY = os.environ.get('ODDS_API_KEY', '08e1ca367452b446f7bee50bbc9d0fc1')
BASE = 'https://api.the-odds-api.com'
REGIONS = 'eu'
MARKETS = 'spreads,h2h,totals'

# Odds-API sport keys for our target leagues
ODDS_LEAGUES = {
    'd2':  ('soccer_germany_bundesliga2', '德乙'),
    'sc1': ('soccer_spl', '苏超'),
    'tr1': ('soccer_turkey_super_league', '土超'),
}

# Seasons to cover
SEASONS = [2023, 2024, 2025]

def date_range(start_str, end_str):
    start = datetime.strptime(start_str, '%Y-%m-%d')
    end = datetime.strptime(end_str, '%Y-%m-%d')
    dates = []
    cur = start
    while cur <= end:
        dates.append(cur.strftime('%Y-%m-%d'))
        cur += timedelta(weeks=1)
    return dates

# Season date ranges (approximate)
SEASON_RANGES = {
    2023: ('2023-05-01', '2024-05-31'),
    2024: ('2024-05-01', '2025-05-31'),
    2025: ('2025-05-01', '2026-05-31'),
}

def fetch_season(sport_key, code, season):
    start_str, end_str = SEASON_RANGES[season]
    dates = date_range(start_str, end_str)
    all_rows = []
    
    out_file = OUT_DIR / f'{code}_odds_api_S{season}.csv'
    existing_dates = set()
    if out_file.exists():
        try:
            ex = pd.read_csv(out_file)
            existing_dates = set(ex['Date'].unique()) if 'Date' in ex.columns else set()
            print(f"  {code} S{season}: 已存在 {len(ex)} 行，跳过已有日期")
        except: pass

    print(f"  {code} S{season}: 抓取 {len(dates)} 个采样日...", end=' ', flush=True)
    
    for d in dates:
        if d in existing_dates:
            continue
        try:
            ts = f'{d}T12:00:00Z'
            r = requests.get(
                f'{BASE}/v4/sports/{sport_key}/odds-history',
                params={'apiKey': ODDS_API_KEY, 'regions': REGIONS, 'markets': MARKETS, 'date': ts},
                timeout=30
            )
            if r.status_code == 429:
                print(f"\n  速率限制，等待 60s...")
                time.sleep(60)
                r = requests.get(f'{BASE}/v4/sports/{sport_key}/odds-history',
                    params={'apiKey': ODDS_API_KEY, 'regions': REGIONS, 'markets': MARKETS, 'date': ts},
                    timeout=30)
            
            if r.status_code == 200:
                data = r.json()
                for event in data.get('data', []):
                    sport_key_name = event.get('sport_key', '')
                    home_team = event.get('home_team', '')
                    away_team = event.get('away_team', '')
                    match_time = event.get('commence_time', '')[:10]
                    for bookmaker in event.get('bookmakers', []):
                        if bookmaker.get('key') != 'bet365':
                            continue
                        for market in bookmaker.get('markets', []):
                            if market.get('key') != 'spreads':
                                continue
                            hcap = market.get('handicap', '')
                            outcomes = market.get(' outcomes', [])
                            b365h = b365a = None
                            for o in outcomes:
                                if o.get('name') == home_team:
                                    b365h = o.get('price')
                                elif o.get('name') == away_team:
                                    b365a = o.get('price')
                            if b365h and b365a:
                                all_rows.append({
                                    'Date': match_time,
                                    'HomeTeam': home_team,
                                    'AwayTeam': away_team,
                                    'season': f'{season}/{season+1}',
                                    'AHh': hcap,
                                    'B365AHH': b365h,
                                    'B365AHA': b365a,
                                    'odds_source': 'odds_api'
                                })
            time.sleep(0.3)
        except Exception as e:
            print(f'\n  Error on {d}: {e}', flush=True)
            continue

    if all_rows:
        df = pd.DataFrame(all_rows)
        existing = pd.DataFrame()
        if out_file.exists():
            try: existing = pd.read_csv(out_file)
            except: pass
        combined = pd.concat([existing, df], ignore_index=True).drop_duplicates(subset=['Date','HomeTeam','AwayTeam']).sort_values('Date')
        combined.to_csv(out_file, index=False, encoding='utf-8')
        print(f"→ 新增 {len(df)} 行 → {out_file.name} ({len(combined)} 总行)")
    else:
        print("→ 无新数据")

if __name__ == '__main__':
    print("=" * 60)
    print("Odds-API 补充下载：d2/sc1/tr1 B365让球盘")
    print("=" * 60)
    for code, (sport_key, name) in ODDS_LEAGUES.items():
        for season in SEASONS:
            fetch_season(sport_key, code, season)
    
    # Merge into _odds_api_merged.csv for ah_1year model
    for code in ODDS_LEAGUES:
        csvs = sorted(OUT_DIR.glob(f'{code}_odds_api_S*.csv'))
        if not csvs:
            continue
        dfs = [pd.read_csv(f) for f in csvs]
        combined = pd.concat(dfs, ignore_index=True).drop_duplicates(subset=['Date','HomeTeam','AwayTeam']).sort_values('Date')
        merged_path = OUT_DIR / f'{code}_odds_api_merged.csv'
        combined.to_csv(merged_path, index=False, encoding='utf-8')
        print(f"[{code}] 合并完成: {merged_path.name} ({len(combined)} 行)")
    
    print("\nOdds-API 补充完成！")
