// Session 44 — Phase 2: the grade card's intel sections actually populate.
// The full chain: engine (Object.assign intel) → analyze route (tierGating
// spread) → /api/scan proxy (spread) → scan page → gradeAdapter → card.
// The broken link was the scan page dropping the fields; this locks it.

const fs = require('fs');
const path = require('path');
const ROOT = path.join(__dirname, '..', '..');
const read = (rel) => fs.readFileSync(path.join(ROOT, rel), 'utf8');
const { applyTierGating } = require('../../src/utils/tierGating');

describe('tierGating preserves engine intel fields', () => {
  it('free tier keeps season_avg/form/etc. (spreads ...result)', () => {
    const gated = applyTierGating({ grade: 'A', season_avg: 26.9, form: 92, matchup_grade: 'A', usage: '31%' }, 'free');
    expect(gated.season_avg).toBe(26.9);
    expect(gated.form).toBe(92);
    expect(gated.matchup_grade).toBe('A');
  });
  it('paid tier passes through unchanged', () => {
    const out = applyTierGating({ grade: 'A', season_avg: 26.9, form: 92 }, 'desk');
    expect(out.season_avg).toBe(26.9);
  });
});

describe('scan page forwards intel fields to the grade card', () => {
  const src = read('web/src/app/scan/page.tsx');
  it('passes the engine intel fields into mapScanToGradeResult', () => {
    for (const f of ['season_avg: result.season_avg', 'last10_avg: result.last10_avg', 'form: result.form', 'usage: result.usage', 'matchup_grade: result.matchup_grade', 'rest: result.rest', 'archetype: result.archetype', 'archetype_blend: result.archetype_blend', 'prop_dna: result.prop_dna']) {
      expect(src).toContain(f);
    }
  });
  it('ScanResponse type declares the intel fields', () => {
    expect(src).toContain('season_avg?: number');
    expect(src).toContain('matchup_grade?: string');
  });
});

describe('/api/scan proxy preserves engine fields', () => {
  it('spreads ...data (does not whitelist)', () => {
    const proxy = read('web/src/app/api/scan/route.ts');
    expect(proxy).toContain('...data');
  });
});
