#!/usr/bin/env bash
# Full Data Ingest - Populate 2025-2026 sports data from all sources
set -euo pipefail

cd /var/www/html/eventheodds

# Load environment
if [[ -f .env ]]; then
  export $(grep -v '^#' .env | xargs)
fi

echo "========================================"
echo "  FULL DATA INGEST - 2025/2026 SEASONS"
echo "  $(date)"
echo "========================================"

# Function to run ingest with timeout
run_ingest() {
  local desc="$1"
  shift
  echo ""
  echo ">>> $desc"
  timeout 300 "$@" 2>&1 || echo "  [WARN] $desc timed out or failed"
}

echo ""
echo "=== PHASE 1: SportsDB Games & Odds ==="

# NBA 2025-2026
run_ingest "NBA 2025 games" node scripts/ingest_sportsdb.js --league nba --season 2025 --mode games
run_ingest "NBA 2025 odds" node scripts/ingest_sportsdb.js --league nba --season 2025 --mode odds
run_ingest "NBA 2026 games" node scripts/ingest_sportsdb.js --league nba --season 2026 --mode games

# NFL 2025-2026
run_ingest "NFL 2025 games" node scripts/ingest_sportsdb.js --league nfl --season 2025 --mode games
run_ingest "NFL 2025 odds" node scripts/ingest_sportsdb.js --league nfl --season 2025 --mode odds
run_ingest "NFL 2026 games" node scripts/ingest_sportsdb.js --league nfl --season 2026 --mode games

# NHL 2025-2026
run_ingest "NHL 2025 games" node scripts/ingest_sportsdb.js --league nhl --season 2025 --mode games
run_ingest "NHL 2025 odds" node scripts/ingest_sportsdb.js --league nhl --season 2025 --mode odds
run_ingest "NHL 2026 games" node scripts/ingest_sportsdb.js --league nhl --season 2026 --mode games

# MLB 2025-2026
run_ingest "MLB 2025 games" node scripts/ingest_sportsdb.js --league mlb --season 2025 --mode games
run_ingest "MLB 2025 odds" node scripts/ingest_sportsdb.js --league mlb --season 2025 --mode odds
run_ingest "MLB 2026 games" node scripts/ingest_sportsdb.js --league mlb --season 2026 --mode games

echo ""
echo "=== PHASE 2: External Feeds (SGO) ==="
run_ingest "External feeds" node scripts/ingest_sportsdb.js --mode external

echo ""
echo "=== PHASE 3: College Sports ==="

# NCAAB
run_ingest "NCAAB games" node scripts/ingest_ncaa_api_games.js --league ncaab --season 2025 2>&1 || true
run_ingest "NCAAB odds (Kaggle)" python3 scripts/ingest_kaggle_ncaa_odds.py 2>&1 || true

# NCAAF
run_ingest "NCAAF games" node scripts/ingest_sdv_cfb_games.js --season 2025 2>&1 || true
run_ingest "NCAAF odds" python3 scripts/ingest_sdv_cfb_odds.py 2>&1 || true

echo ""
echo "=== PHASE 4: Upcoming Schedules ==="
run_ingest "Upcoming schedules" python3 scripts/ingest_upcoming_schedules.py 2>&1 || true

echo ""
echo "=== PHASE 5: Historical Odds Backfill ==="
run_ingest "Historical betting data" node scripts/ingest_betting_historical_games.js 2>&1 || true

echo ""
echo "========================================"
echo "  INGEST COMPLETE - $(date)"
echo "========================================"

# Show updated stats
echo ""
echo "=== POST-INGEST COVERAGE ==="
PGPASSWORD=eventheodds_dev_password psql -h 127.0.0.1 -p 5433 -U eventheodds -d eventheodds_sports -c "
SELECT
  league,
  season,
  COUNT(*) as games,
  COUNT(CASE WHEN \"moneylineHome\" IS NOT NULL THEN 1 END) as with_odds,
  ROUND(100.0 * COUNT(CASE WHEN \"moneylineHome\" IS NOT NULL THEN 1 END) / COUNT(*), 1) as odds_pct
FROM \"SportsGame\"
WHERE season >= 2025 AND league IN ('nba','nfl','nhl','mlb','ncaab','ncaaf')
GROUP BY league, season
ORDER BY league, season;
"
