#!/usr/bin/env python3
"""只下载所有联赛的 fixtures（比赛结果），不获取赔率"""
import os, time
from pathlib import Path
from datetime import datetime, timezone, timedelta
import numpy as np
import pandas as pd
import requests
__import__('warnings').filterwarnings('ignore')

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)

API_KEY = os.environ.get('API_FOOTBALL_KEY', '57b64e857ebeffdc156f0c41d43c7894')
BASE = 'https://v3.football.api-sports.io'
HEADERS = {'x-apisports-key': API_KEY}
TZ = timezone(timedelta(hours=8))

LEAGUE_IDS = {
    'e0':39,'e1':40,'e2':41,'e2n':42,'e3':183,
    'd1':78,'d2':79,'f1':61,'f2':62,
    'i1':135,'i2':136,'sp1':140,'sp2':141,
    'tr1':203,'p1':94,'b1':145,'g1':197,
    'n1':88,'n2':89,'sc1':180,'cz1':237,
    'a1':279,'r1':164,'br1':71,'m1':253,'j1':98,
}
SEASONS = [2023, 2024, 2025]

def get_fixtures(code, league_id, season):
    cache = OUT_DIR / f'{code}_S{season}_fixtures.csv'
    if cache.exists() and cache.stat().st_size > 200:
        try:
            ex = pd.read_csv(cache, usecols=['FixtureID'])
            if len(ex) > 100:
                print(f'  {code} S{season}: 已缓存 {len(ex)} ✓', flush=True)
                return cache
        except: pass
    
    print(f'  {code} S{season}: 下载...', end=' ', flush=True)
    rows = []
    page = 1
    while True:
        params = {'league': league_id, 'season': season}
        if page > 1:
            params['page'] = page
        try:
            r = requests.get(f'{BASE}/fixtures', params=params, headers=HEADERS, timeout=30)
            if r.status_code != 200:
                break
            d = r.json()
            fixtures = d.get('response', [])
            if not fixtures:
                break
            for fix in fixtures:
                fi = fix['fixture']
                teams = fix['teams']
                goals = fix.get('goals', {})
                rows.append({
                    'Date': datetime.fromtimestamp(fi['timestamp'], tz=TZ).strftime('%Y-%m-%d'),
                    'LeagueCode': code, 'LeagueID': league_id, 'Season': season,
                    'HomeTeam': teams['home']['name'], 'AwayTeam': teams['away']['name'],
                    'FTHG': goals.get('home'), 'FTAG': goals.get('away'),
                    'Status': fi.get('status', {}).get('short', ''),
                    'FixtureID': fi['id'],
                })
            paging = d.get('paging', {})
            total = int(paging.get('total', 1))
            if page >= total:
                break
            page += 1
            time.sleep(0.05)
        except:
            break
    
    if rows:
        df = pd.DataFrame(rows)
        df.to_csv(cache, index=False, encoding='utf-8')
        print(f'{len(df)} fixtures ✓', flush=True)
        return cache
    print('0 ⚠️', flush=True)
    return None

def merge_all(code):
    files = sorted(OUT_DIR.glob(f'{code}_S*_fixtures.csv'))
    if not files:
        return 0
    dfs = []
    for f in files:
        try:
            dfs.append(pd.read_csv(f, low_memory=False))
        except: pass
    if not dfs:
        return 0
    df = pd.concat(dfs, ignore_index=True).drop_duplicates(subset=['FixtureID'])
    df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
    df = df.sort_values('Date').reset_index(drop=True)
    out = OUT_DIR / f'{code}_fixtures.csv'
    df.to_csv(out, index=False, encoding='utf-8')
    print(f'  {code}: {len(df)} fixtures → {out.name}')
    return len(df)

print('=' * 60)
print('Fixture 下载（仅比赛结果，无赔率）')
print('=' * 60)

# STEP 1: 下载 fixtures
tasks = [(code, lid, s) for code, lid in LEAGUE_IDS.items() for s in SEASONS]
for i, (code, lid, season) in enumerate(tasks):
    print(f'[{i+1}/{len(tasks)}]', end=' ')
    get_fixtures(code, lid, season)

# STEP 2: 合并
print('\n' + '=' * 60)
print('合并数据...')
print('=' * 60)
for code in sorted(LEAGUE_IDS.keys()):
    merge_all(code)

print('\n✅ Fixture 下载完成！')
