import { describe, expect, it } from 'vitest';

import { deduplicateProps } from '../prop-dedup';

describe('prop-dedup', () => {
  it('deduplicates same player/stat/direction/market by edge', () => {
    const props = [
      {
        id: 'a',
        player_name: 'Luka Doncic',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'points', recommendation: 'over', market_type: 'PLAYER_PROP', edge: 4.1, line: 31.5, odds: -110 },
      },
      {
        id: 'b',
        player_name: 'Luka Doncic',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'points', recommendation: 'over', market_type: 'PLAYER_PROP', edge: 7.2, line: 31.5, odds: -120 },
      },
    ];

    expect(deduplicateProps(props).map((p) => p.id)).toEqual(['b']);
  });

  it('uses direction-aware line tie-breaks', () => {
    const overProps = [
      {
        id: 'over-high',
        player_name: 'Jayson Tatum',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'points', recommendation: 'over', market_type: 'PLAYER_PROP', edge: 5, line: 29.5, odds: -110 },
      },
      {
        id: 'over-low',
        player_name: 'Jayson Tatum',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'points', recommendation: 'over', market_type: 'PLAYER_PROP', edge: 5, line: 28.5, odds: -110 },
      },
    ];
    const underProps = [
      {
        id: 'under-low',
        player_name: 'Jayson Tatum',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'rebounds', recommendation: 'under', market_type: 'PLAYER_PROP', edge: 5, line: 8.5, odds: -110 },
      },
      {
        id: 'under-high',
        player_name: 'Jayson Tatum',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'rebounds', recommendation: 'under', market_type: 'PLAYER_PROP', edge: 5, line: 9.5, odds: -110 },
      },
    ];

    expect(deduplicateProps(overProps).map((p) => p.id)).toEqual(['over-low']);
    expect(deduplicateProps(underProps).map((p) => p.id)).toEqual(['under-high']);
  });

  it('uses lower vig before stable order when edge and line tie', () => {
    const props = [
      {
        id: 'worse-vig',
        player_name: 'Nikola Jokic',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'assists', recommendation: 'over', market_type: 'PLAYER_PROP', edge: 4, line: 9.5, odds: -125 },
      },
      {
        id: 'better-vig',
        player_name: 'Nikola Jokic',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'assists', recommendation: 'over', market_type: 'PLAYER_PROP', edge: 4, line: 9.5, odds: -105 },
      },
    ];

    expect(deduplicateProps(props).map((p) => p.id)).toEqual(['better-vig']);
  });

  it('does not deduplicate different market types', () => {
    const props = [
      {
        id: 'full-game',
        player_name: 'Tarik Skubal',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'strikeouts', recommendation: 'over', market_type: 'PLAYER_PROP', edge: 5, line: 6.5 },
      },
      {
        id: 'f5',
        player_name: 'Tarik Skubal',
        forecast_type: 'PLAYER_PROP',
        forecast_payload: { stat_type: 'strikeouts', recommendation: 'over', market_type: 'MLB_F5', edge: 5, line: 4.5 },
      },
    ];

    expect(deduplicateProps(props).map((p) => p.id)).toEqual(['full-game', 'f5']);
  });
});
