// Session 45 — Phase 3: pre-graded snapshot overlay + the GameCard swap.

const fs = require('fs');
const path = require('path');
const WEB = path.join(__dirname, '..', '..', 'web', 'src');
const read = (rel) => fs.readFileSync(path.join(WEB, rel), 'utf8');
const a = require('../../web/src/lib/slateAdapter');

describe('snapshot overlay adapter', () => {
  const grades = [
    { player: 'Aaron Judge', stat_type: 'total_bases', line: 1.5, direction: 'over', grade: 'A+', archetype: 'BOMBER', gradedAt: { line: 1.5, odds: -115, timestamp: '2026-06-18T18:00:00Z' } },
    { player: 'Aaron Judge', stat_type: 'home_runs', line: 0.5, direction: 'over', grade: 'C', archetype: 'BOMBER', gradedAt: { line: 0.5, odds: 120, timestamp: '2026-06-18T18:00:00Z' } },
  ];
  const deltas = [{ player: 'Aaron Judge', stat: 'total_bases', side: 'O', delta: 1.0, direction: 'toward', currentLine: 2.5 }];

  it('indexes grades + deltas for lookup', () => {
    const gi = a.indexGrades(grades);
    expect(gi['aaron judge|total_bases'].grade).toBe('A+');
    const di = a.indexDeltas(deltas);
    expect(di['aaron judge|total_bases|O'].direction).toBe('toward');
  });

  it('overlays grades onto game props → playerStrips (name once, archetype, gradedAt, delta)', () => {
    const gameProps = [
      { player: 'Aaron Judge', stat_type: 'total_bases', line: 1.5 },
      { player: 'Aaron Judge', stat_type: 'home_runs', line: 0.5 },
    ];
    const strips = a.buildPlayerStripsFromProps(gameProps, a.indexGrades(grades), a.indexDeltas(deltas), new Date('2026-06-18T20:00:00Z').getTime());
    expect(strips).toHaveLength(1); // name once
    expect(strips[0].archetype).toEqual({ primary: 'BOMBER' });
    const tb = strips[0].props.find((p) => p.stat === 'TB');
    expect(tb.grade).toBe('A+');
    expect(tb.gradedAt.ago).toBe('2h ago');
    expect(tb.gradedAt.odds).toBe(-115);
    expect(tb.delta).toEqual({ delta: 1.0, direction: 'toward', currentLine: 2.5 });
  });

  it('marks ungraded props as awaiting (no Read button)', () => {
    const strips = a.buildPlayerStripsFromProps(
      [{ player: 'New Guy', stat_type: 'hits', line: 1.5 }],
      a.indexGrades(grades), a.indexDeltas(deltas),
    );
    expect(strips[0].props[0].awaiting).toBe(true);
    expect(strips[0].props[0].grade).toBeNull();
  });

  it('gradedAgo formats relative time', () => {
    const now = new Date('2026-06-18T20:00:00Z').getTime();
    expect(a.gradedAgo('2026-06-18T19:30:00Z', now)).toBe('30m ago');
    expect(a.gradedAgo('2026-06-18T18:00:00Z', now)).toBe('2h ago');
    expect(a.gradedAgo(undefined, now)).toBe('');
  });
});

describe('Slate uses the VYNDR card + pre-graded snapshot (swap is real)', () => {
  const src = read('components/Slate.tsx');
  it('imports the VYNDR GameCard component (legacy only for types)', () => {
    expect(src).toContain("import VyndrGameCard");
    expect(src).toContain("'@/components/vyndr/GameCard'");
    expect(src).toContain('<VyndrGameCard');
    // legacy component default-import is gone (only `import type` remains)
    expect(src).not.toMatch(/^import GameCard /m);
  });
  it('fetches the pre-graded snapshot and builds playerStrips', () => {
    expect(src).toContain('/api/snapshot/');
    expect(src).toContain('buildPlayerStripsFromProps');
    expect(src).toContain('slateGameToCardData');
  });
  it('retired the on-demand Read grade flow', () => {
    expect(src).not.toContain('const onGrade = useCallback');
  });
});

describe('StatStrip renders snapshot states', () => {
  const src = read('components/vyndr/StatStrip.tsx');
  it('renders Awaiting next read + line-delta sub-line', () => {
    expect(src).toContain('Awaiting next read');
    expect(src).toContain('TOWARD');
    expect(src).toContain('Graded ');
  });
});
