#!/usr/bin/env python3
"""Import WNBA and NBA odds from Kaggle."""
import csv
import json
from pathlib import Path

DATA_DIR = Path("/var/www/html/eventheodds/data/betting")
KAGGLE_DIR = Path("/var/www/html/eventheodds/data/kaggle")

def decimal_to_american(decimal_odds):
    """Convert decimal odds to American format."""
    if decimal_odds is None or decimal_odds <= 1:
        return None
    if decimal_odds >= 2.0:
        return int((decimal_odds - 1) * 100)
    else:
        return int(-100 / (decimal_odds - 1))

def import_sport(csv_file, output_file, sport_name):
    """Import odds from a CSV file."""
    if not csv_file.exists():
        print(f"File not found: {csv_file}")
        return 0
    
    # Load existing data
    existing = []
    if output_file.exists():
        with open(output_file) as f:
            existing = json.load(f)
    
    existing_keys = set()
    for g in existing:
        key = (g.get("home_team", ""), g.get("away_team", ""), g.get("date", "")[:10])
        existing_keys.add(key)
        existing_keys.add((g.get("away_team", ""), g.get("home_team", ""), g.get("date", "")[:10]))
    
    print(f"Existing {sport_name} records: {len(existing)}")
    print(f"Loading {csv_file.name}...")
    
    new_games = []
    with open(csv_file, newline="", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        for row in reader:
            team1 = row.get("team1", "").strip()
            team2 = row.get("team2", "").strip()
            timestamp = row.get("timestamp", "")[:10]
            
            if not team1 or not team2 or not timestamp:
                continue
            
            key = (team1, team2, timestamp)
            if key in existing_keys:
                continue
            
            try:
                ml1_dec = float(row.get("team1_moneyline", 0)) if row.get("team1_moneyline") else None
                ml2_dec = float(row.get("team2_moneyline", 0)) if row.get("team2_moneyline") else None
                spread1 = float(row.get("team1_spread", 0)) if row.get("team1_spread") else None
                total = float(row.get("over_total", 0)) if row.get("over_total") else None
            except:
                continue
            
            ml1 = decimal_to_american(ml1_dec)
            ml2 = decimal_to_american(ml2_dec)
            
            record = {
                "game_id": f"kaggle_{sport_name.lower()}_{team1}_{team2}_{timestamp}".replace(" ", "_"),
                "sport": sport_name,
                "date": timestamp,
                "home_team": team1,
                "away_team": team2,
                "spread": spread1,
                "total": total,
                "home_ml": ml1,
                "away_ml": ml2,
                "odds": {
                    "source": "kaggle",
                    "sportsbooks": {
                        "pinnacle": {
                            "spread": spread1,
                            "total": total,
                            "home_ml": ml1,
                            "away_ml": ml2
                        }
                    }
                }
            }
            new_games.append(record)
            existing_keys.add(key)
    
    print(f"New {sport_name} games: {len(new_games)}")
    
    # Merge and save
    all_data = existing + new_games
    with open(output_file, "w") as f:
        json.dump(all_data, f, indent=2)
    
    return len(new_games)

def main():
    DATA_DIR.mkdir(parents=True, exist_ok=True)
    
    # Import WNBA
    wnba_files = [
        KAGGLE_DIR / "wnba_main_lines.csv",
        KAGGLE_DIR / "wnba_main_lines_history.csv"
    ]
    wnba_output = DATA_DIR / "wnba_historical.json"
    
    total_wnba = 0
    for f in wnba_files:
        total_wnba += import_sport(f, wnba_output, "WNBA")
    print(f"Total WNBA added: {total_wnba}")
    
    # Import NBA from this dataset
    nba_files = [
        KAGGLE_DIR / "nba_main_lines.csv"
    ]
    nba_output = DATA_DIR / "nba_historical.json"
    
    total_nba = 0
    for f in nba_files:
        total_nba += import_sport(f, nba_output, "NBA")
    print(f"Total NBA added: {total_nba}")
    
    # Final count
    print("\n=== IMPORT COMPLETE ===")
    for sport, output in [("WNBA", wnba_output), ("NBA", nba_output)]:
        if output.exists():
            with open(output) as f:
                data = json.load(f)
            print(f"{sport}: {len(data)} total records")

if __name__ == "__main__":
    main()
