import { describe, it, expect } from 'vitest';
import { buildBoard, TIERS, TIER_KEYS } from './board.js';
import { COLOR_BY_KEY, deltaE } from './palette.js';
import { ICON_KEYS } from './icons.js';

describe('match board', () => {
  it('is deterministic for a given seed', () => {
    const a = buildBoard({ format: 'icons', tier: 'standard', seed: '2026-06-16' });
    const b = buildBoard({ format: 'icons', tier: 'standard', seed: '2026-06-16' });
    expect(a.cards).toEqual(b.cards);
  });

  it('different days give different boards', () => {
    const a = buildBoard({ format: 'icons', tier: 'standard', seed: '2026-06-16' });
    const b = buildBoard({ format: 'icons', tier: 'standard', seed: '2026-06-17' });
    expect(a.cards).not.toEqual(b.cards);
  });

  for (const tier of TIER_KEYS) {
    it(`${tier}: correct card count and exactly matchN of each face`, () => {
      const t = TIERS[tier];
      const { cards, faces, matchN } = buildBoard({ tier, seed: 's' });
      expect(cards.length).toBe(t.cols * t.rows);
      expect(faces.length).toBe(t.faces);
      expect(matchN).toBe(t.matchN);
      for (const f of faces) {
        expect(cards.filter((c) => c.key === f).length).toBe(matchN);
      }
      // every card is one of the chosen faces
      expect(cards.every((c) => faces.includes(c.key))).toBe(true);
    });
  }

  it('icon boards draw from the icon set', () => {
    const { faces } = buildBoard({ format: 'icons', tier: 'expert', seed: 'x' });
    expect(faces.every((k) => ICON_KEYS.includes(k))).toBe(true);
  });

  it('color boards are perceptually spaced (no near-identical pair)', () => {
    // sweep many seeds; every board's colors must be comfortably distinct
    for (let i = 0; i < 80; i++) {
      const { faces } = buildBoard({ format: 'colors', tier: 'standard', seed: 'd' + i });
      const labs = faces.map((k) => COLOR_BY_KEY[k].lab);
      let min = Infinity;
      for (let a = 0; a < labs.length; a++)
        for (let b = a + 1; b < labs.length; b++)
          min = Math.min(min, deltaE(labs[a], labs[b]));
      expect(min).toBeGreaterThan(15);   // never confusably close
    }
  });
});
