// Unit: stat-filter config + /api/stats/filters/:sport (Session 23).

const express = require('express');
const request = require('supertest');
const { STAT_FILTERS, getStatFilters, isValidStat } = require('../../src/config/statFilters');

describe('statFilters config', () => {
  test('every sport list starts with "all"', () => {
    for (const list of Object.values(STAT_FILTERS)) {
      expect(list[0]).toBe('all');
    }
  });

  test('NBA categories include the headline stats', () => {
    expect(getStatFilters('nba')).toEqual(
      expect.arrayContaining(['points', 'rebounds', 'assists', 'threes', 'pra']),
    );
  });

  test('MLB categories include home_runs and total_bases', () => {
    expect(getStatFilters('mlb')).toEqual(
      expect.arrayContaining(['home_runs', 'total_bases', 'on_base']),
    );
  });

  test('unknown sport falls back to ["all"]', () => {
    expect(getStatFilters('quidditch')).toEqual(['all']);
  });

  test('isValidStat — all is always valid, unknown stat is not', () => {
    expect(isValidStat('nba', 'all')).toBe(true);
    expect(isValidStat('nba', 'points')).toBe(true);
    expect(isValidStat('nba', 'home_runs')).toBe(false);
    expect(isValidStat('mlb', 'home_runs')).toBe(true);
  });
});

describe('GET /api/stats/filters/:sport', () => {
  function app() {
    const a = express();
    a.use('/api/stats', require('../../src/routes/stats'));
    return a;
  }

  test('returns the category list for a sport', async () => {
    const res = await request(app()).get('/api/stats/filters/nba');
    expect(res.status).toBe(200);
    expect(res.body.sport).toBe('nba');
    expect(res.body.filters).toContain('points');
  });
});
