const { SPORT_CONFIG, getActiveSports, getSportConfig, SPORTS, isActiveSport } = require('../../src/config/sports');

describe('SPORT_CONFIG', () => {
  test('all 7 sports present in the pipeline config', () => {
    expect(Object.keys(SPORT_CONFIG).sort()).toEqual(['mlb','ncaab','ncaafb','nfl','nhl','wnba','nba'].sort());
  });

  test('every sport is active for the pipeline', () => {
    for (const s of getActiveSports()) {
      expect(s.active).toBe(true);
    }
    expect(getActiveSports()).toHaveLength(7);
  });

  test('each sport has espnScoreboard + espnSummary + statMap', () => {
    for (const s of getActiveSports()) {
      expect(typeof s.espnScoreboard).toBe('string');
      expect(typeof s.espnSummary).toBe('string');
      expect(s.statMap).toBeDefined();
      expect(Object.keys(s.statMap).length).toBeGreaterThan(0);
    }
  });

  test('MLB carries the useMlbStatsApi flag + base URL', () => {
    const mlb = getSportConfig('mlb');
    expect(mlb.useMlbStatsApi).toBe(true);
    expect(mlb.mlbStatsApiBase).toMatch(/^https:\/\/statsapi\.mlb\.com/);
  });

  test('NFL statMap uses category-based entries', () => {
    const nfl = getSportConfig('nfl');
    expect(nfl.statMap.passing_yards).toMatchObject({ category: 'passing', field: 'passingYards' });
    expect(nfl.statMap.receiving_yards).toMatchObject({ category: 'receiving', field: 'receivingYards' });
  });

  test('NBA statMap exposes combos via calc()', () => {
    const nba = getSportConfig('nba');
    const stats = []; stats[1] = 25; stats[5] = 8; stats[6] = 6;
    expect(nba.statMap.pts_reb_ast.calc(stats)).toBe(39);
    expect(nba.statMap.stl_blk.calc([0,0,0,0,0,0,0,0,2,3])).toBe(5);
  });

  test('threes_made parses "3-7" → 3 and tolerates empty/null/undefined', () => {
    const parse = getSportConfig('nba').statMap.threes_made.parse;
    expect(parse('3-7')).toBe(3);
    expect(parse('')).toBe(0);
    expect(parse(null)).toBe(0);
    expect(parse(undefined)).toBe(0);
  });

  test('MLB inningsPitched parses "5.1" → 5.333…', () => {
    const parse = getSportConfig('mlb').statMap.inningsPitched.parse;
    expect(parse('5.1')).toBeCloseTo(5.333, 3);
    expect(parse('7.2')).toBeCloseTo(7.667, 3);
    expect(parse('')).toBe(0);
  });

  test('getSportConfig throws on unknown sport', () => {
    expect(() => getSportConfig('curling')).toThrow(/Unknown sport/);
  });
});

describe('legacy SPORTS surface (still in use)', () => {
  test('UI flags untouched — nfl/nhl remain UI-inactive', () => {
    expect(isActiveSport('nfl')).toBe(false);
    expect(isActiveSport('nba')).toBe(true);
    expect(SPORTS.nba.color).toBe('#E94B3C');
  });
});
