/**
 * Seed today's NBA games (2/24/2026) with real opening market pricing.
 * Source: FanDuel market lines as of 2/24/2026.
 */

import 'dotenv/config';
import pool from '../db';

interface GameSeed {
  awayTeam: string;
  awayShort: string;
  homeTeam: string;
  homeShort: string;
  startsAt: string; // ISO
  moneyline: { home: number; away: number };
  spread: { home: { line: number; odds: number }; away: { line: number; odds: number } };
  total: { over: { line: number; odds: number }; under: { line: number; odds: number } };
}

const TODAYS_GAMES: GameSeed[] = [
  {
    awayTeam: 'Philadelphia 76ers', awayShort: 'PHI',
    homeTeam: 'Indiana Pacers', homeShort: 'IND',
    startsAt: '2026-02-24T19:00:00-05:00',
    moneyline: { home: 350, away: -450 },
    spread: { home: { line: 10, odds: -110 }, away: { line: -10, odds: -110 } },
    total: { over: { line: 233.5, odds: -110 }, under: { line: 233.5, odds: -110 } },
  },
  {
    awayTeam: 'Dallas Mavericks', awayShort: 'DAL',
    homeTeam: 'Brooklyn Nets', homeShort: 'BKN',
    startsAt: '2026-02-24T19:30:00-05:00',
    moneyline: { home: 110, away: -130 },
    spread: { home: { line: 2, odds: -110 }, away: { line: -2, odds: -110 } },
    total: { over: { line: 225.5, odds: -110 }, under: { line: 225.5, odds: -110 } },
  },
  {
    awayTeam: 'New York Knicks', awayShort: 'NYK',
    homeTeam: 'Cleveland Cavaliers', homeShort: 'CLE',
    startsAt: '2026-02-24T19:30:00-05:00',
    moneyline: { home: -166, away: 140 },
    spread: { home: { line: -4, odds: -110 }, away: { line: 4, odds: -110 } },
    total: { over: { line: 232.5, odds: -110 }, under: { line: 232.5, odds: -110 } },
  },
  {
    awayTeam: 'Oklahoma City Thunder', awayShort: 'OKC',
    homeTeam: 'Toronto Raptors', homeShort: 'TOR',
    startsAt: '2026-02-24T19:30:00-05:00',
    moneyline: { home: -130, away: 110 },
    spread: { home: { line: -1.5, odds: -110 }, away: { line: 1.5, odds: -110 } },
    total: { over: { line: 216.5, odds: -110 }, under: { line: 216.5, odds: -110 } },
  },
  {
    awayTeam: 'Washington Wizards', awayShort: 'WAS',
    homeTeam: 'Atlanta Hawks', homeShort: 'ATL',
    startsAt: '2026-02-24T19:30:00-05:00',
    moneyline: { home: -769, away: 540 },
    spread: { home: { line: -13.5, odds: -110 }, away: { line: 13.5, odds: -110 } },
    total: { over: { line: 236.5, odds: -110 }, under: { line: 236.5, odds: -110 } },
  },
  {
    awayTeam: 'Charlotte Hornets', awayShort: 'CHA',
    homeTeam: 'Chicago Bulls', homeShort: 'CHI',
    startsAt: '2026-02-24T20:00:00-05:00',
    moneyline: { home: 265, away: -330 },
    spread: { home: { line: 8.5, odds: -110 }, away: { line: -8.5, odds: -110 } },
    total: { over: { line: 232.5, odds: -110 }, under: { line: 232.5, odds: -110 } },
  },
  {
    awayTeam: 'Miami Heat', awayShort: 'MIA',
    homeTeam: 'Milwaukee Bucks', homeShort: 'MIL',
    startsAt: '2026-02-24T20:00:00-05:00',
    moneyline: { home: 205, away: -250 },
    spread: { home: { line: 6.5, odds: -110 }, away: { line: -6.5, odds: -110 } },
    total: { over: { line: 226.5, odds: -110 }, under: { line: 226.5, odds: -110 } },
  },
  {
    awayTeam: 'Golden State Warriors', awayShort: 'GSW',
    homeTeam: 'New Orleans Pelicans', homeShort: 'NOP',
    startsAt: '2026-02-24T20:00:00-05:00',
    moneyline: { home: 102, away: -120 },
    spread: { home: { line: 1.5, odds: -110 }, away: { line: -1.5, odds: -110 } },
    total: { over: { line: 227.5, odds: -110 }, under: { line: 227.5, odds: -110 } },
  },
  {
    awayTeam: 'Boston Celtics', awayShort: 'BOS',
    homeTeam: 'Phoenix Suns', homeShort: 'PHX',
    startsAt: '2026-02-24T21:00:00-05:00',
    moneyline: { home: 172, away: -200 },
    spread: { home: { line: 7, odds: -110 }, away: { line: -7, odds: -110 } },
    total: { over: { line: 205.5, odds: -110 }, under: { line: 205.5, odds: -110 } },
  },
  {
    awayTeam: 'Minnesota Timberwolves', awayShort: 'MIN',
    homeTeam: 'Portland Trail Blazers', homeShort: 'POR',
    startsAt: '2026-02-24T22:00:00-05:00',
    moneyline: { home: 215, away: -260 },
    spread: { home: { line: 6.5, odds: -110 }, away: { line: -6.5, odds: -110 } },
    total: { over: { line: 235.5, odds: -110 }, under: { line: 235.5, odds: -110 } },
  },
  {
    awayTeam: 'Orlando Magic', awayShort: 'ORL',
    homeTeam: 'Los Angeles Lakers', homeShort: 'LAL',
    startsAt: '2026-02-24T22:30:00-05:00',
    moneyline: { home: -205, away: 172 },
    spread: { home: { line: -5, odds: -110 }, away: { line: 5, odds: -110 } },
    total: { over: { line: 230.5, odds: -110 }, under: { line: 230.5, odds: -110 } },
  },
];

async function main() {
  console.log('='.repeat(60));
  console.log('SEEDING TODAY\'S NBA GAMES — 2/24/2026');
  console.log('='.repeat(60));
  console.log(`${TODAYS_GAMES.length} games to seed\n`);

  // Clear old NBA events from rm_events for today
  await pool.query(`DELETE FROM rm_events WHERE league = 'nba' AND DATE(starts_at) = '2026-02-24'`);
  console.log('Cleared old NBA events for today.\n');

  for (let i = 0; i < TODAYS_GAMES.length; i++) {
    const g = TODAYS_GAMES[i];
    const eventId = `nba-${g.awayShort.toLowerCase()}-${g.homeShort.toLowerCase()}-20260224`;

    await pool.query(
      `INSERT INTO rm_events (event_id, league, home_team, home_short, away_team, away_short, starts_at, status, moneyline, spread, total, source)
       VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
       ON CONFLICT (event_id) DO UPDATE SET
         moneyline = EXCLUDED.moneyline,
         spread = EXCLUDED.spread,
         total = EXCLUDED.total,
         updated_at = NOW()`,
      [
        eventId, 'nba',
        g.homeTeam, g.homeShort,
        g.awayTeam, g.awayShort,
        g.startsAt, 'scheduled',
        JSON.stringify(g.moneyline),
        JSON.stringify(g.spread),
        JSON.stringify(g.total),
        'fanduel',
      ]
    );

    console.log(`  [${i + 1}/${TODAYS_GAMES.length}] ${g.awayShort} @ ${g.homeShort} — ML: ${g.moneyline.away}/${g.moneyline.home} SPR: ${g.spread.away.line} TOT: ${g.total.over.line}`);
  }

  console.log(`\nSeeded ${TODAYS_GAMES.length} NBA games.`);
  console.log('='.repeat(60));
  await pool.end();
}

main().catch(err => {
  console.error('Fatal:', err);
  process.exit(1);
});
