import assert from 'node:assert/strict';
import { buildStoredMlbSnapshotFromPhase, summarizeMlbOperationalAlerts } from '../../src/services/mlb-snapshot';

function run(): void {
  const phase = {
    score: 0.64,
    rawData: {
      firstFive: {
        homeStarter: { name: 'Pablo Lopez', fip: 3.45 },
        awayStarter: { name: 'Kevin Gausman', fip: 3.62 },
      },
      bullpen: {
        homeBullpen: { workload: { fatigueScore: 0.62, last1DayPitches: 31 } },
        awayBullpen: { workload: { fatigueScore: 0.41, last1DayPitches: 19 } },
      },
      context: {
        lineups: { homeStatus: 'Confirmed Lineup', awayStatus: 'Projected Lineup' },
        travel: { home: { travelMiles: 0 }, away: { travelMiles: 874 } },
        weather: { temperatureF: 58, windMph: 11, windDirection: 'out' },
        weatherRunBias: 0.03,
      },
      applicationHints: { f5SideReady: true, propMatrixReady: true },
    },
  };

  const snapshot = buildStoredMlbSnapshotFromPhase(phase, '2026-03-20T18:00:00.000Z');
  assert.deepEqual(snapshot, {
    captured_at: '2026-03-20T18:00:00.000Z',
    phase_score: 0.64,
    probable_starters: {
      home: { name: 'Pablo Lopez', fip: 3.45 },
      away: { name: 'Kevin Gausman', fip: 3.62 },
    },
    lineups: { homeStatus: 'Confirmed Lineup', awayStatus: 'Projected Lineup' },
    travel: { home: { travelMiles: 0 }, away: { travelMiles: 874 } },
    weather: { temperatureF: 58, windMph: 11, windDirection: 'out' },
    weather_run_bias: 0.03,
    bullpen: {
      home: { fatigueScore: 0.62, last1DayPitches: 31 },
      away: { fatigueScore: 0.41, last1DayPitches: 19 },
    },
    application_hints: { f5SideReady: true, propMatrixReady: true },
  });

  const alerts = summarizeMlbOperationalAlerts({
    snapshot,
    startsAt: new Date(Date.now() + 90 * 60000).toISOString(),
    candidateCount: 0,
    teamName: 'Minnesota Twins',
  });
  assert.deepEqual(alerts.map((a) => a.code), [
    'missing_lineups_near_first_pitch',
    'zero_candidate_prop_game',
  ]);
  console.log('ok - mlb phase snapshot');
}

run();
