import { describe, expect, it } from 'vitest';

import { extractEspnPlayerStat, normalizeResolverPlayerName } from '../clv-player-stats';

describe('clv-player-stats', () => {
  it('normalizes diacritics and suffixes for player matching', () => {
    expect(normalizeResolverPlayerName('Luka Dončić')).toBe('luka doncic');
    expect(normalizeResolverPlayerName('Derrick Jones Jr.')).toBe('derrick jones');
  });

  it('extracts basketball direct stats', () => {
    const summary = {
      header: { competitions: [{ competitors: [{ team: { displayName: 'Dallas Mavericks' } }, { team: { displayName: 'Phoenix Suns' } }] }] },
      boxscore: {
        players: [{
          team: { displayName: 'Dallas Mavericks' },
          statistics: [{
            keys: ['minutes', 'points', 'assists', 'rebounds'],
            athletes: [{
              athlete: { id: '77', displayName: 'Luka Doncic' },
              stats: ['35', '31', '8', '10'],
            }],
          }],
        }],
      },
    };

    expect(extractEspnPlayerStat(summary, 'nba', 'Luka Dončić', 'points')).toEqual({
      playerExternalId: '77',
      playerName: 'Luka Doncic',
      team: 'Dallas Mavericks',
      opponent: 'Phoenix Suns',
      value: 31,
    });
  });

  it('extracts hockey points by combining goals and assists', () => {
    const summary = {
      header: { competitions: [{ competitors: [{ team: { displayName: 'New York Rangers' } }, { team: { displayName: 'Florida Panthers' } }] }] },
      boxscore: {
        players: [{
          team: { displayName: 'Florida Panthers' },
          statistics: [{
            keys: ['goals', 'assists', 'shotsTotal'],
            athletes: [{
              athlete: { id: '16', displayName: 'Aleksander Barkov' },
              stats: ['1', '2', '4'],
            }],
          }],
        }],
      },
    };

    expect(extractEspnPlayerStat(summary, 'nhl', 'Aleksander Barkov', 'points')?.value).toBe(3);
    expect(extractEspnPlayerStat(summary, 'nhl', 'Aleksander Barkov', 'shots')?.value).toBe(4);
  });

  it('extracts mlb pitching outs from innings pitched', () => {
    const summary = {
      header: { competitions: [{ competitors: [{ team: { displayName: 'St. Louis Cardinals' } }, { team: { displayName: 'Tampa Bay Rays' } }] }] },
      boxscore: {
        players: [{
          team: { displayName: 'Tampa Bay Rays' },
          statistics: [{
            keys: ['fullInnings.partInnings', 'hits', 'strikeouts'],
            athletes: [{
              athlete: { id: '4298087', displayName: 'Joe Boyle' },
              stats: ['6.1', '3', '4'],
            }],
          }],
        }],
      },
    };

    expect(extractEspnPlayerStat(summary, 'mlb', 'Joe Boyle', 'pitching_outs')?.value).toBe(19);
    expect(extractEspnPlayerStat(summary, 'mlb', 'Joe Boyle', 'pitching_strikeouts')?.value).toBe(4);
  });
});
