// Integration: /api/schedule/:sport (Session 23).
//
// The schedule service is mocked at the redis layer so we exercise the
// route + service normalization + flag enrichment without hitting ESPN
// or a live Redis. ESPN itself is stubbed via axios.

const express = require('express');
const request = require('supertest');

jest.mock('axios');
const axios = require('axios');

// In-memory redis stand-in. cacheGet returns whatever we seed; cacheSet
// records writes so we can assert the cache-aside warm path.
const store = {};
jest.mock('../../src/utils/redis', () => ({
  cacheGet: jest.fn(async (k) => (k in store ? store[k] : null)),
  cacheSet: jest.fn(async (k, v) => { store[k] = v; return true; }),
}));

function mountApp() {
  delete require.cache[require.resolve('../../src/routes/schedule')];
  delete require.cache[require.resolve('../../src/services/scheduleService')];
  const scheduleRoutes = require('../../src/routes/schedule');
  const app = express();
  app.use(express.json());
  app.use('/api/schedule', scheduleRoutes);
  return app;
}

// NOTE (Session 57, Phase 0): the event date must fall on the REQUESTED ET
// date (2026-06-12) — 00:40Z Jun 13 = 20:40 ET Jun 12. The old fixture was
// dated a day later and the route served it anyway; that exact hole is how the
// Jun 13 NYK@SA Finals game kept rendering as "tonight" all July.
const ESPN_NBA = {
  data: {
    events: [
      {
        id: '401234567',
        date: '2026-06-13T00:40:00Z',
        status: { type: { state: 'pre' } },
        competitions: [{
          venue: { fullName: 'Frost Bank Center' },
          broadcasts: [{ names: ['ABC'] }],
          competitors: [
            { homeAway: 'home', score: '0', team: { displayName: 'San Antonio Spurs', abbreviation: 'SA' } },
            { homeAway: 'away', score: '0', team: { displayName: 'New York Knicks', abbreviation: 'NYK' } },
          ],
        }],
      },
    ],
  },
};

beforeEach(() => {
  for (const k of Object.keys(store)) delete store[k];
  jest.clearAllMocks();
});

describe('GET /api/schedule/:sport', () => {
  test('returns normalized games from a fresh ESPN fetch (cache miss)', async () => {
    axios.get.mockResolvedValue(ESPN_NBA);
    const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
    expect(res.status).toBe(200);
    expect(res.body.sport).toBe('nba');
    expect(res.body.source).toBe('espn');
    expect(res.body.games).toHaveLength(1);
    const g = res.body.games[0];
    expect(g.homeTeam.abbreviation).toBe('SA');
    expect(g.awayTeam.name).toBe('New York Knicks');
    expect(g.status).toBe('pre');
    expect(g.venue).toBe('Frost Bank Center');
    expect(g.broadcast).toBe('ABC');
    expect(g.score).toBeNull();          // pre-game → no score
    expect(g.hasOdds).toBe(false);
    expect(g.hasGameLines).toBe(false);
  });

  test('warms the cache, second call does not re-fetch ESPN', async () => {
    axios.get.mockResolvedValue(ESPN_NBA);
    const app = mountApp();
    await request(app).get('/api/schedule/nba?date=2026-06-12');
    const callsAfterFirst = axios.get.mock.calls.length;
    await request(app).get('/api/schedule/nba?date=2026-06-12');
    expect(axios.get.mock.calls.length).toBe(callsAfterFirst); // served from cache
  });

  test('pins the ESPN fetch to the requested date (?dates=YYYYMMDD)', async () => {
    axios.get.mockResolvedValue(ESPN_NBA);
    await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
    expect(axios.get.mock.calls[0][0]).toContain('dates=20260612');
  });

  // Session 57 (Phase 0) regression — in the off-season ESPN returns the
  // NEAREST slate, not today's. Any event whose ET date ≠ the requested date
  // must be dropped, or a June Finals game renders as tonight's slate in July.
  test('filters out events from a different ET date (fake off-season game)', async () => {
    axios.get.mockResolvedValue({
      data: {
        events: [
          {
            id: '401999999',
            date: '2026-06-14T00:30:00Z', // Jun 13 ET — NOT the requested Jun 12
            status: { type: { state: 'pre' } },
            competitions: [{
              competitors: [
                { homeAway: 'home', score: '0', team: { displayName: 'San Antonio Spurs', abbreviation: 'SA' } },
                { homeAway: 'away', score: '0', team: { displayName: 'New York Knicks', abbreviation: 'NYK' } },
              ],
            }],
          },
        ],
      },
    });
    const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
    expect(res.status).toBe(200);
    expect(res.body.games).toEqual([]);
  });

  test('drops events with no parseable date (live feed only)', async () => {
    axios.get.mockResolvedValue({
      data: {
        events: [{
          id: '77', // no `date` field at all
          status: { type: { state: 'pre' } },
          competitions: [{
            competitors: [
              { homeAway: 'home', score: '0', team: { displayName: 'A', abbreviation: 'A' } },
              { homeAway: 'away', score: '0', team: { displayName: 'B', abbreviation: 'B' } },
            ],
          }],
        }],
      },
    });
    const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
    expect(res.body.games).toEqual([]);
  });

  test('returns empty array (not error) when no games today', async () => {
    axios.get.mockResolvedValue({ data: { events: [] } });
    const res = await request(mountApp()).get('/api/schedule/mlb?date=2026-06-12');
    expect(res.status).toBe(200);
    expect(res.body.games).toEqual([]);
  });

  test('hasOdds true when odds-api cache has props for the slate', async () => {
    store['odds:nba:2026-06-12'] = { updated_at: 'x', props: [{ player: 'Wemby' }] };
    axios.get.mockResolvedValue(ESPN_NBA);
    const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
    expect(res.body.games[0].hasOdds).toBe(true);
    expect(res.body.games[0].hasGameLines).toBe(false);
  });

  test('hasGameLines true when Tank01 odds cache has data', async () => {
    store['tank01:nba:odds:20260612'] = { body: { '20260612_NYK@SA': { homeML: '-110' } } };
    axios.get.mockResolvedValue(ESPN_NBA);
    const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
    expect(res.body.games[0].hasGameLines).toBe(true);
  });

  test('scores appear once a game is in/post', async () => {
    axios.get.mockResolvedValue({
      data: { events: [{
        id: '9', date: '2026-06-12T20:00:00Z',
        status: { type: { state: 'in' } },
        competitions: [{
          competitors: [
            { homeAway: 'home', score: '54', team: { displayName: 'A', abbreviation: 'A' } },
            { homeAway: 'away', score: '49', team: { displayName: 'B', abbreviation: 'B' } },
          ],
        }],
      }] },
    });
    const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
    expect(res.body.games[0].score).toEqual({ home: 54, away: 49 });
  });

  test('unknown sport returns 404', async () => {
    const res = await request(mountApp()).get('/api/schedule/cricket');
    expect(res.status).toBe(404);
  });
});
