// Session 60 (night2/G) — the aggregator's content formats (spec §12).
// streak-watch is the ZERO-GRADE daily format; daily-report flags itself
// DO-NOT-POST until the record is real (n>=20).

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

const mockStore = new Map();
jest.mock('../../src/utils/redis', () => ({
  cacheGet: async (k) => (mockStore.has(k) ? mockStore.get(k) : null),
  cacheSet: async (k, v) => { mockStore.set(k, v); return true; },
  getRedisClient: () => null,
  isDegraded: () => true,
}));
jest.mock('../../src/services/rosterLogs', () => ({ loadRosterLogs: jest.fn(async () => []) }));
const { loadRosterLogs } = require('../../src/services/rosterLogs');

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

beforeEach(() => { mockStore.clear(); jest.clearAllMocks(); });

describe('GET /api/content/top-signals/:sport', () => {
  test('returns tonight top graded props from the snapshot cache', async () => {
    mockStore.set('snapshot:mlb:latest', {
      updated_at: 'x',
      grades: [
        { player: 'Aaron Judge', stat_type: 'home_runs', line: 0.5, direction: 'over', grade: 'A', confidence: 80, gradedAt: { timestamp: 't1' } },
        { player: 'Settled Guy', stat_type: 'hits', line: 1.5, grade: 'A', confidence: 90, outcome: { result: 'hit' } },
      ],
    });
    const res = await request(mountApp()).get('/api/content/top-signals/mlb');
    expect(res.status).toBe(200);
    expect(res.body.dataLevel).toBe('full');
    expect(res.body.signals).toHaveLength(1); // settled props excluded
    expect(res.body.signals[0].player).toBe('Aaron Judge');
  });
});

describe('GET /api/content/streak-watch/:sport', () => {
  test('the zero-grade format: real streaks through the lens', async () => {
    loadRosterLogs.mockResolvedValue([
      { name: 'Streaky Guy', team: 'Tampa Bay Rays', games: [
        { date: '2026-07-10', opponent: 'Boston Red Sox', hits: 2 },
        { date: '2026-07-09', opponent: 'Boston Red Sox', hits: 1 },
        { date: '2026-07-08', opponent: 'New York Yankees', hits: 1 },
      ] },
    ]);
    const res = await request(mountApp()).get('/api/content/streak-watch/mlb');
    expect(res.status).toBe(200);
    expect(res.body.dataLevel).toBe('full');
    expect(res.body.streaks[0].player).toBe('Streaky Guy');
    expect(res.body.streaks[0].read).toContain('hit streak');
  });
});

describe('GET /api/content/daily-report/:sport', () => {
  test('flags DO-NOT-POST while the record is building', async () => {
    const res = await request(mountApp()).get('/api/content/daily-report/mlb');
    expect(res.status).toBe(200);
    expect(res.body.do_not_post).toBe(true);
    expect(res.body.reason).toContain('record building');
  });
});
