'use strict';

const { normalizeOutrights, normalizeProps, americanToDecimal } = require('../../src/utils/oddsNormalizer');

const outrightEvents = [
  {
    id: 'evt-ws',
    sport_key: 'baseball_mlb_world_series_winner',
    sport_title: 'MLB World Series Winner',
    commence_time: '2026-10-20T00:00:00Z',
    bookmakers: [
      {
        key: 'draftkings',
        title: 'DraftKings',
        markets: [
          {
            key: 'outrights',
            outcomes: [
              { name: 'Los Angeles Dodgers', price: 350 },
              { name: 'New York Yankees', price: 450 },
            ],
          },
        ],
      },
      {
        key: 'fanduel',
        title: 'FanDuel',
        markets: [
          {
            key: 'outrights',
            outcomes: [
              { name: 'Los Angeles Dodgers', price: 400 }, // better payout than DK's 350
              { name: 'New York Yankees', price: 420 },
            ],
          },
        ],
      },
      {
        key: 'bovada', // NOT allowed → skipped
        markets: [{ key: 'outrights', outcomes: [{ name: 'Los Angeles Dodgers', price: 9999 }] }],
      },
    ],
  },
];

describe('normalizeOutrights', () => {
  test('normalizes outright outcomes (name+price, no point) into markets', () => {
    const markets = normalizeOutrights(outrightEvents);
    expect(markets).toHaveLength(1);
    const m = markets[0];
    expect(m.key).toBe('baseball_mlb_world_series_winner');
    expect(m.title).toBe('MLB World Series Winner');
    expect(m.selections).toHaveLength(2);
  });

  test('picks the BEST (highest decimal payout) price per selection across allowed books', () => {
    const [m] = normalizeOutrights(outrightEvents);
    const dodgers = m.selections.find((s) => s.name === 'Los Angeles Dodgers');
    // FanDuel +400 beats DraftKings +350; Bovada is not an allowed book.
    expect(dodgers.price).toBe(400);
    expect(dodgers.book).toBe('fanduel');
    const yankees = m.selections.find((s) => s.name === 'New York Yankees');
    // DraftKings +450 beats FanDuel +420.
    expect(yankees.price).toBe(450);
    expect(yankees.book).toBe('draftkings');
  });

  test('normalizeProps would DROP these outright outcomes (no point) — the reason a new branch exists', () => {
    // Feed the SAME events through the player-prop normalizer: outrights have
    // no `point` and no Over/Under, so normalizeProps yields nothing.
    const props = normalizeProps(outrightEvents);
    expect(props).toEqual([]);
    // ...while normalizeOutrights keeps them.
    expect(normalizeOutrights(outrightEvents)[0].selections.length).toBeGreaterThan(0);
  });

  test('never fabricates: empty / malformed input → empty array', () => {
    expect(normalizeOutrights([])).toEqual([]);
    expect(normalizeOutrights(null)).toEqual([]);
    expect(normalizeOutrights([{ sport_key: 'x', bookmakers: [] }])).toEqual([]);
    expect(normalizeOutrights([{ sport_key: 'x', bookmakers: [{ key: 'draftkings', markets: [{ key: 'outrights', outcomes: [{ name: 'A', price: null }] }] }] }])).toEqual([]);
  });

  test('americanToDecimal: +150→2.5, -200→1.5, junk→null', () => {
    expect(americanToDecimal(150)).toBeCloseTo(2.5, 5);
    expect(americanToDecimal(-200)).toBeCloseTo(1.5, 5);
    expect(americanToDecimal(0)).toBeNull();
    expect(americanToDecimal(null)).toBeNull();
    expect(americanToDecimal('150')).toBeNull();
  });
});
