// Session 54 — audit cleanup: name edge cases (hyphen, middle initial, richie)
// + accent-keeping dedup + correlation copy.

const be = require('../../src/utils/playerName');
const fe = require('../../web/src/lib/playerName');
const parlay = require('../../src/services/parlayService');

describe('Phase 1 — name edge cases', () => {
  it('merges hyphenated vs spaced ("Jung-hoo Lee" === "Jung Hoo Lee")', () => {
    expect(be.nameKey('Jung-hoo Lee')).toBe(be.nameKey('Jung Hoo Lee'));
  });
  it('strips middle initials ("Josh H Smith" === "Josh Smith")', () => {
    expect(be.nameKey('Josh H Smith')).toBe(be.nameKey('Josh Smith'));
  });
  it('resolves richie ("Richie Palacios" === "Richard Palacios")', () => {
    expect(be.nameKey('Richie Palacios')).toBe(be.nameKey('Richard Palacios'));
  });
  it('frontend + backend copies agree on the new cases', () => {
    for (const n of ['Jung-hoo Lee', 'Jung Hoo Lee', 'Josh H Smith', 'Richie Palacios', 'José Soriano', 'JC Escarra']) {
      expect(fe.nameKey(n)).toBe(be.nameKey(n));
      expect(fe.normalizeName(n).display).toBe(be.normalizeName(n).display);
    }
  });

  // Guard against over-merging distinct players.
  it('does not strip real middle names ("Juan Carlos Smith" kept)', () => {
    expect(be.nameKey('Juan Carlos Smith')).toBe('juan carlos smith');
  });
  it('keeps distinct players distinct', () => {
    expect(be.nameKey('Aaron Judge')).not.toBe(be.nameKey('Aaron Nola'));
  });
  it('display keeps accents ("José Soriano")', () => {
    expect(be.normalizeName('José Soriano').display).toBe('José Soriano');
  });
});

describe('Phase 2 — correlation copy includes the game id', () => {
  it('same-game (different teams) warning names the game', () => {
    const w = parlay.correlationWarning([
      { player: 'A', team: 'NYY', game: 'NYY @ BOS' },
      { player: 'B', team: 'BOS', game: 'NYY @ BOS' },
    ]);
    expect(w).toBe('⚠ 2 legs from the same game (NYY @ BOS) — correlated');
  });
});
