'use strict';

// Item 6 (Truth-Everywhere Part 2) — the Desk showcase renders REAL snapshot
// data (alt-line ladder + same-team correlation) or hides. No mocked numbers.

const { getDeskShowcase, __internals } = require('../../src/services/deskShowcaseService');

const cacheFrom = (map) => async (k) => (k in map ? map[k] : null);
const grade = (o) => ({
  player_name: o.player, stat_type: o.stat || 'hits', grade: o.grade,
  team: o.team || null, alt_lines: o.alt || [],
});

describe('getDeskShowcase', () => {
  test('renders a real A/B ladder; inflated edges are guarded to null', async () => {
    const cacheGet = cacheFrom({ 'snapshot:mlb:latest': { grades: [
      grade({ player: 'Vary', stat: 'strikeouts', grade: 'A', alt: [
        { line: 4.5, grade: 'A', edge_pct: 8.2 }, { line: 6.5, grade: 'B', edge_pct: 3.1, base: true }, { line: 8.5, grade: 'C', edge_pct: 91.3 },
      ] }),
    ] } });
    const out = await getDeskShowcase({ cacheGet, sports: ['mlb'] });
    expect(out.available).toBe(true);
    expect(out.ladder.player).toBe('Vary');
    expect(out.ladder.rungs.map((r) => r.grade)).toEqual(['A', 'B', 'C']); // sorted by line
    expect(out.ladder.rungs[0].edge).toBe(8.2);     // sane edge kept
    expect(out.ladder.rungs[2].edge).toBeNull();     // 91.3% guarded (artifact, not a market edge)
    expect(out.kelly).toBeNull();                    // no odds → never a fabricated %
  });

  test('prefers the ladder with the MOST grade variation (best real example)', async () => {
    const cacheGet = cacheFrom({ 'snapshot:mlb:latest': { grades: [
      grade({ player: 'Flat', grade: 'B', alt: [{ line: 0.5, grade: 'B' }, { line: 1.5, grade: 'B' }] }),
      grade({ player: 'Varies', grade: 'A', alt: [{ line: 0.5, grade: 'A' }, { line: 1.5, grade: 'C' }] }),
    ] } });
    const out = await getDeskShowcase({ cacheGet, sports: ['mlb'] });
    expect(out.ladder.player).toBe('Varies');
  });

  test('parlay φ is REAL — the model correlation for two real same-team legs', async () => {
    const cacheGet = cacheFrom({ 'snapshot:mlb:latest': { grades: [
      grade({ player: 'Alice', grade: 'A', team: 'NYY', alt: [{ line: 0.5, grade: 'A' }, { line: 1.5, grade: 'B' }] }),
      grade({ player: 'Bob', grade: 'B', team: 'NYY', alt: [{ line: 0.5, grade: 'B' }, { line: 1.5, grade: 'C' }] }),
    ] } });
    const out = await getDeskShowcase({ cacheGet, sports: ['mlb'] });
    expect(out.parlay).toEqual({ value: 0.34, legs: ['Alice', 'Bob'], team: 'NYY' });
  });

  test('no same-team pair → parlay null (never an invented correlation)', async () => {
    const cacheGet = cacheFrom({ 'snapshot:mlb:latest': { grades: [
      grade({ player: 'Solo', grade: 'A', team: 'NYY', alt: [{ line: 0.5, grade: 'A' }, { line: 1.5, grade: 'C' }] }),
    ] } });
    const out = await getDeskShowcase({ cacheGet, sports: ['mlb'] });
    expect(out.parlay).toBeNull();
  });

  test('no A/B ladder anywhere → { available:false } (visuals hide)', async () => {
    const cacheGet = cacheFrom({ 'snapshot:mlb:latest': { grades: [
      grade({ player: 'Weak', grade: 'D', alt: [{ line: 0.5, grade: 'D' }] }),
    ] } });
    const out = await getDeskShowcase({ cacheGet, sports: ['mlb'] });
    expect(out).toEqual({ available: false });
  });
});
