"""
League Reliability Loader
=========================
Loads pre-computed per-league odds reliability scores from
data/league_reliability.json. Called once at orchestrator startup.

The reliability score (0.0 – 1.0) represents how well-calibrated
a league's betting odds are based on historical Brier Score analysis.

Usage:
    from utils.league_reliability import load_league_reliability
    lookup = load_league_reliability()
    rel = lookup.get(league_id, 0.35)
"""

from __future__ import annotations

import json
import os
from typing import Dict


_DATA_FILE = os.path.join(
    os.path.dirname(os.path.abspath(__file__)),
    "..",
    "data",
    "league_reliability.json",
)


def load_league_reliability() -> Dict[str, float]:
    """
    Returns a dict mapping league_id → odds_reliability (0.0-1.0).
    Falls back gracefully to an empty dict if the file is missing.
    """
    if not os.path.isfile(_DATA_FILE):
        print(
            f"⚠️  league_reliability.json not found at {_DATA_FILE}. "
            "All leagues will use default reliability (0.35)."
        )
        return {}

    try:
        with open(_DATA_FILE, "r", encoding="utf-8") as f:
            data = json.load(f)

        lookup: Dict[str, float] = data.get("lookup", {})
        total = len(lookup)
        print(f"✅ Loaded odds reliability for {total} leagues")
        return lookup

    except (json.JSONDecodeError, KeyError, TypeError) as exc:
        print(f"⚠️  Failed to parse league_reliability.json: {exc}")
        return {}
