#!/usr/bin/env python3
"""
将 historical_data 中所有 *_history.csv 的亚盘相关列取反（符号取反），
使其与 pro_v4 的 AHh 约定一致（正数=主队让球/主队强）。
"""
import pandas as pd
from pathlib import Path

REPO = Path('/root/.openclaw/workspace-flj_bot1/football-predictor-repro')
HIST_DIR = REPO / 'historical_data'

# 需要取反的列：AHh 以及以 AHH/AHA 结尾/开头的赔率列
AH_COLS_TO_NEGATE = [
    'AHh', 'AHCh',     # 让球数
    'B365AHH', 'B365AHA', 'B365CAHH', 'B365CAHA',  # B365亚盘
    'PAHH', 'PAHA', 'PCAHH', 'PCAHA',               # Pinnacle亚盘
    'MaxAHH', 'MaxAHA', 'MaxCAHH', 'MaxCAHA',      # Max亚盘
    'AvgAHH', 'AvgAHA', 'AvgCAHH', 'AvgCAHA',       # Avg亚盘
    'BFEAHH', 'BFEAHA', 'BFECAHH', 'BFECAHA',      # Betfair亚盘
]

fixed_count = 0
skipped_count = 0

for hist_file in sorted(HIST_DIR.glob('*_history.csv')):
    df = pd.read_csv(hist_file, encoding='utf-8-sig', low_memory=False)
    cols_present = [c for c in AH_COLS_TO_NEGATE if c in df.columns]
    cols_to_fix = [c for c in cols_present if df[c].dtype in ('float64', 'int64', 'float32', 'int32')]

    if not cols_to_fix:
        print(f"  跳过 {hist_file.name}（无亚盘列）")
        skipped_count += 1
        continue

    # 只对已有数据的列取反
    for col in cols_to_fix:
        df[col] = -df[col]

    # 保存
    df.to_csv(hist_file, index=False, encoding='utf-8-sig')
    print(f"  ✅ {hist_file.name}: 取反 {len(cols_to_fix)} 列 → {', '.join(cols_to_fix)}")
    fixed_count += 1

print(f"\n完成！修复 {fixed_count} 个文件，跳过 {skipped_count} 个")
