import { describe, expect, it } from 'vitest';
import { gamesPlayed, hintSharePercent, winRate } from './stats';
import type { Stats } from './model';

const s = (wins: number, losses: number, draws: number): Stats => ({
  wins,
  losses,
  draws,
  maxGamePoints: 0,
  maxWordPoints: 0,
  moves: 0,
  hintsUsed: 0,
  bestMoves: [],
});

// withCounts overrides moves/hintsUsed on the zero fixture for the hint-share cases.
const withCounts = (moves: number, hintsUsed: number): Stats => ({ ...s(0, 0, 0), moves, hintsUsed });

describe('stats', () => {
  it('sums games played', () => {
    expect(gamesPlayed(s(7, 4, 1))).toBe(12);
  });

  it('computes a rounded win rate', () => {
    expect(winRate(s(7, 4, 1))).toBe(58); // 7/12 = 58.33 -> 58
    expect(winRate(s(1, 1, 0))).toBe(50);
  });

  it('win rate is 0 with no games', () => {
    expect(winRate(s(0, 0, 0))).toBe(0);
  });

  it('computes the hint share (hints / plays)', () => {
    expect(hintSharePercent(withCounts(200, 10))).toBe(5); // 10/200 = 5%
    expect(hintSharePercent(withCounts(248, 12))).toBeCloseTo(4.8387, 3);
  });

  it('hint share is 0 with no plays (no division by zero)', () => {
    expect(hintSharePercent(withCounts(0, 0))).toBe(0);
    expect(hintSharePercent(withCounts(0, 5))).toBe(0);
  });
});
