// Session 46 — Phase 2: player name normalization.

const be = require('../../src/utils/playerName');
const fe = require('../../web/src/lib/playerName');
const slate = require('../../web/src/lib/slateAdapter');
const intel = require('../../src/services/playerIntelService');

describe('normalizeName', () => {
  it('collapses period variants to one key', () => {
    expect(be.nameKey('A.J. Ewing')).toBe(be.nameKey('AJ Ewing'));
    expect(be.normalizeName('A.J. Ewing').display).toBe('AJ Ewing');
  });
  it('collapses suffix variants to one key', () => {
    expect(be.nameKey('Jazz Chisholm Jr.')).toBe(be.nameKey('Jazz Chisholm'));
    expect(be.normalizeName('Jazz Chisholm Jr.').display).toBe('Jazz Chisholm Jr');
  });
  it('keeps the accent in the display but folds it in the key', () => {
    const r = be.normalizeName('Ronald Acuña Jr.');
    expect(r.display).toBe('Ronald Acuña Jr');
    expect(r.key).toBe('ronald acuna');
    expect(be.nameKey('Ronald Acuna')).toBe(r.key);
  });
  it('backend + frontend copies agree', () => {
    for (const n of ['A.J. Ewing', 'Jazz Chisholm Jr.', 'Ronald Acuña Jr.', 'Shohei Ohtani']) {
      expect(fe.nameKey(n)).toBe(be.nameKey(n));
      expect(fe.normalizeName(n).display).toBe(be.normalizeName(n).display);
    }
  });
});

describe('sanitizePlayerName normalizes periods/suffix', () => {
  it('strips periods for display', () => {
    expect(intel.sanitizePlayerName('A.J. Ewing')).toBe('AJ Ewing');
    expect(intel.sanitizePlayerName('Jazz%20Chisholm%20Jr.')).toBe('Jazz Chisholm Jr');
  });
});

describe('buildPlayerStripsFromProps merges name variants', () => {
  it('merges "A.J. Ewing" + "AJ Ewing" into one strip', () => {
    const strips = slate.buildPlayerStripsFromProps(
      [
        { player: 'A.J. Ewing', stat_type: 'hits', line: 1.5 },
        { player: 'AJ Ewing', stat_type: 'total_bases', line: 1.5 },
      ],
      {}, {},
    );
    expect(strips).toHaveLength(1);
    expect(strips[0].props).toHaveLength(2);
  });
  it('displays the longer name variant', () => {
    const strips = slate.buildPlayerStripsFromProps(
      [
        { player: 'Jazz Chisholm', stat_type: 'hits', line: 1.5 },
        { player: 'Jazz Chisholm Jr', stat_type: 'runs', line: 0.5 },
      ],
      {}, {},
    );
    expect(strips).toHaveLength(1);
    expect(strips[0].player).toBe('Jazz Chisholm Jr');
  });
});
