#!/usr/bin/env python3
from __future__ import annotations

import json
import re
from pathlib import Path
from typing import Dict, List

import pandas as pd

FIXED_OU_PATTERNS = [
    r'>2\.5', r'<2\.5', r'>1\.5', r'<1\.5', r'>3\.5', r'<3\.5',
    r'>2\.25', r'<2\.25', r'>2\.75', r'<2\.75', r'>2/2\.5', r'<2/2\.5',
]
DYNAMIC_LINE_CANDIDATES = [
    'ou_line', 'ouline', 'goal_line', 'goals_line', 'total_line', 'totals_line',
    'over_under_line', 'line_over_under', 'ou handicap', 'over/under line'
]
SPECIAL_COLUMNS = ['BbOU']


def classify_columns(columns: List[str]) -> Dict[str, List[str]]:
    fixed_hits = []
    dynamic_hits = []
    special_hits = []
    for col in columns:
        low = col.lower()
        if any(key in low for key in DYNAMIC_LINE_CANDIDATES):
            dynamic_hits.append(col)
        if any(re.search(p, col, flags=re.I) for p in FIXED_OU_PATTERNS):
            fixed_hits.append(col)
        if col in SPECIAL_COLUMNS:
            special_hits.append(col)
    return {
        'fixed_ou_columns': sorted(set(fixed_hits)),
        'dynamic_line_columns': sorted(set(dynamic_hits)),
        'special_columns': sorted(set(special_hits)),
    }


def main() -> None:
    base = Path('historical_data')
    rows = []
    for path in sorted(base.glob('*_history.csv')):
        df0 = pd.read_csv(path, nrows=5, low_memory=False)
        cols = list(df0.columns)
        classified = classify_columns(cols)
        bbou_note = ''
        if 'BbOU' in cols:
            try:
                sample = pd.read_csv(path, usecols=['BbOU'], nrows=20, low_memory=False)['BbOU'].dropna().tolist()
                bbou_note = f"sample={sample[:10]}"
            except Exception as exc:  # noqa: BLE001
                bbou_note = f'error={exc}'
        rows.append({
            'league': path.stem.replace('_history', ''),
            'rows': int(pd.read_csv(path, usecols=[cols[0]], low_memory=False).shape[0]) if cols else 0,
            'fixed_ou_columns': '|'.join(classified['fixed_ou_columns']),
            'dynamic_line_columns': '|'.join(classified['dynamic_line_columns']),
            'special_columns': '|'.join(classified['special_columns']),
            'has_dynamic_line': bool(classified['dynamic_line_columns']),
            'bbou_note': bbou_note,
        })
    report = pd.DataFrame(rows).sort_values(['has_dynamic_line', 'rows'], ascending=[False, False])
    out_dir = Path('experiments/goal_number_dynamic_line_audit')
    out_dir.mkdir(parents=True, exist_ok=True)
    report.to_csv(out_dir / 'dynamic_line_support.csv', index=False, encoding='utf-8-sig')
    summary = {
        'supports_dynamic_line_from_historical_data': bool(report['has_dynamic_line'].any()) if len(report) else False,
        'league_count': int(len(report)),
        'dynamic_line_leagues': report.loc[report['has_dynamic_line'], 'league'].tolist(),
        'note': 'Current historical_data only exposes fixed-threshold OU columns such as >2.5/<2.5. BbOU is bookmaker-count metadata, not a totals line.'
    }
    (out_dir / 'summary.json').write_text(json.dumps(summary, ensure_ascii=False, indent=2), encoding='utf-8')
    print(json.dumps(summary, ensure_ascii=False, indent=2))


if __name__ == '__main__':
    main()
