import assert from 'node:assert/strict';
import { __mlbPropInternals } from '../../src/services/grok';

const phaseResult = {
  rawData: {
    firstFive: {
      homeStarter: { name: 'Ace Home', fip: 3.05, xfip: 3.2, whip: 1.08, kBbPct: 0.22 },
      awayStarter: { name: 'Ace Away', fip: 2.9, xfip: 3.0, whip: 1.02, kBbPct: 0.24 },
      homeOffense: { fullWrcPlus: 108, recentWrcPlus: 112, projectedWrcPlus: 110 },
      awayOffense: { fullWrcPlus: 116, recentWrcPlus: 118, projectedWrcPlus: 117 },
    },
    bullpen: {
      homeBullpen: { fip: 3.35, xfip: 3.42, whip: 1.14, kPct: 0.27, workload: { fatigueScore: 0.72 } },
      awayBullpen: { fip: 3.1, xfip: 3.2, whip: 1.08, kPct: 0.29, workload: { fatigueScore: 0.33 } },
    },
    context: {
      lineups: { homeStatus: 'Confirmed Lineup', awayStatus: 'Projected Lineup' },
      weatherRunBias: 0.04,
    },
  },
};

function run(): void {
  assert.equal(__mlbPropInternals.normalizeMlbStat('Pitching Strikeouts'), 'pitchingstrikeouts');
  assert.equal(__mlbPropInternals.normalizeMlbStat('RBIs'), 'rbis');
  assert.equal(__mlbPropInternals.normalizeMlbStat('Total Bases'), 'totalbases');

  const scoredHome = __mlbPropInternals.scoreMlbCandidatesForPublishing(
    [
      {
        player: 'Lead Hitter',
        statType: 'total_bases',
        marketLineValue: 1.5,
        recommendationHint: 'over',
        source: 'player_prop_line',
        propLabel: 'Total Bases 1.5',
        overOdds: -110,
        underOdds: -110,
      },
      {
        player: 'Home Starter',
        statType: 'pitching_strikeouts',
        marketLineValue: 6.5,
        recommendationHint: 'over',
        source: 'player_prop_line',
        propLabel: 'Strikeouts 6.5',
        overOdds: -110,
        underOdds: -110,
      },
    ],
    phaseResult,
    'home',
  );

  const hitter = scoredHome.find((item) => item.player === 'Lead Hitter');
  const pitcher = scoredHome.find((item) => item.player === 'Home Starter');
  assert.ok(hitter, 'home hitter market should survive as a valid under candidate');
  assert.equal(hitter?.recommendationHint, 'under', 'home hitter should flip to under into strong opposing starter + rested bullpen');
  assert.notEqual(pitcher?.recommendationHint, 'over', 'pitcher K prop should not publish an over against strong opposing offense + tired bullpen');

  const scoredAway = __mlbPropInternals.scoreMlbCandidatesForPublishing(
    [
      {
        player: 'Away Hitter',
        statType: 'hits',
        marketLineValue: 1.5,
        recommendationHint: 'over',
        source: 'player_prop_line',
        propLabel: 'Hits 1.5',
        overOdds: -110,
        underOdds: -110,
      },
    ],
    phaseResult,
    'away',
  );
  assert.equal(scoredAway[0]?.suppressionReason, 'waiting on confirmed lineup', 'away hitter props should be suppressed until lineup is confirmed');

  const summary = __mlbPropInternals.summarizeMlbCandidateFilters([
    {
      player: 'A',
      statType: 'hits',
      marketLineValue: 1.5,
      source: 'player_prop_line',
      propLabel: 'Hits 1.5',
      suppressionReason: 'waiting on confirmed lineup',
    },
    {
      player: 'B',
      statType: 'total_bases',
      marketLineValue: 1.5,
      source: 'player_prop_line',
      propLabel: 'Total Bases 1.5',
      suppressionReason: 'waiting on confirmed lineup',
    },
  ]);
  assert.match(summary, /2 waiting on confirmed lineup/);
  console.log('ok - mlb prop filters');
}

run();
