const { validateBattlemapLayout, coverForPoint } = require('../shared/battlemapEngine');
const {
  __ZONES: ZONES,
  __COVER_OBJECTS: COVER_OBJECTS,
  __tokenAnchorPositions: tokenAnchorPositions,
} = require('../components/BattlemapTab');

describe('BattlemapTab layout (frontend copy)', () => {
  test('rooms do not overlap', () => {
    expect(validateBattlemapLayout(ZONES)).toEqual([]);
  });

  test('player and enemy anchors always land inside a room, for any roster size', () => {
    for (let count = 0; count <= 6; count += 1) {
      const players = Array.from({ length: count }, (_, i) => `player-${i}`);
      const enemies = Array.from({ length: count }, (_, i) => `enemy-${i}`);
      const anchors = tokenAnchorPositions(players, enemies);
      const points = [...anchors.players, ...anchors.enemies];
      const issues = validateBattlemapLayout(ZONES, points);
      expect(issues).toEqual([]);
    }
  });

  test('at least one default player anchor and one enemy anchor sit on cover, but not all of them', () => {
    const layout = { cover: COVER_OBJECTS };
    const anchors = tokenAnchorPositions(
      ['p1', 'p2', 'p3', 'p4', 'p5'],
      ['e1', 'e2', 'e3', 'e4', 'e5'],
    );
    const coveredPlayers = anchors.players.filter(([x, y]) => coverForPoint(layout, { x, y }));
    const coveredEnemies = anchors.enemies.filter(([x, y]) => coverForPoint(layout, { x, y }));

    expect(coveredPlayers.length).toBeGreaterThan(0);
    expect(coveredPlayers.length).toBeLessThan(anchors.players.length);
    expect(coveredEnemies.length).toBeGreaterThan(0);
    expect(coveredEnemies.length).toBeLessThan(anchors.enemies.length);
  });
});
