#!/usr/bin/env python3
"""
Standalone ELO computation script.

Usage:
  python scripts/compute_elo.py                # football only
  python scripts/compute_elo.py --sport basketball
  python scripts/compute_elo.py --sport all    # football + basketball

Designed for cron or manual execution.
Calculates ELO ratings from match history and persists to both JSON and DB.
"""

import os
import sys
import time
import argparse

# Add ai-engine root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from features.elo_system import ELORatingSystem


def main():
    parser = argparse.ArgumentParser(description="Compute ELO ratings from match history")
    parser.add_argument(
        "--sport",
        choices=["football", "basketball", "all"],
        default="football",
        help="Sport to compute ELO for (default: football)",
    )
    args = parser.parse_args()

    sports = ["football", "basketball"] if args.sport == "all" else [args.sport]

    for sport in sports:
        print(f"\n{'='*60}")
        print(f"🏆 Computing ELO ratings for: {sport.upper()}")
        print(f"{'='*60}")

        start = time.time()

        system = ELORatingSystem()
        system.calculate_all_from_history(sport)

        elapsed = time.time() - start

        print(f"\n✅ {sport} ELO computation completed in {elapsed:.1f}s")
        print(f"   Teams rated: {len(system.ratings)}")

        if system.ratings:
            top = sorted(
                system.ratings.values(),
                key=lambda r: r.overall_elo,
                reverse=True,
            )[:5]
            print("   Top 5:")
            for i, t in enumerate(top, 1):
                print(f"     {i}. {t.team_name:25} → {t.overall_elo:.0f}")


if __name__ == "__main__":
    main()
