// Pure mapping from a game view (for the viewer) to a status/result badge: a label key
// and a place-based emoji. Used by the lobby lists.

import type { GameView } from './model';
import type { MessageKey } from './i18n/catalog';

export interface ResultBadge {
  key: MessageKey;
  emoji: string;
}

export function resultBadge(game: GameView, myId: string): ResultBadge {
  const me = game.seats.find((s) => s.accountId === myId);

  if (game.status === 'active' || game.status === 'open') {
    return game.toMove === me?.seat
      ? { key: 'result.yourMove', emoji: '🟢' }
      : { key: 'result.oppMove', emoji: '⏳' };
  }

  if (me?.isWinner) return { key: 'result.victory', emoji: '🏆' };
  if (!game.seats.some((s) => s.isWinner)) return { key: 'result.draw', emoji: '🏅' };

  // Someone else won and it is not me, so I did not win — even when scores are level (a
  // win by resignation or timeout can leave the winner at or below my score). The winner
  // takes rank 1; place me among the remaining seats by score, starting at rank 2.
  const ahead = game.seats.filter((s) => !s.isWinner && s.accountId !== myId && s.score > (me?.score ?? 0)).length;
  const rank = 2 + ahead;
  if (rank === 2) return game.players === 2 ? { key: 'result.defeat', emoji: '🥈' } : { key: 'result.place2', emoji: '🥈' };
  if (rank === 3) return { key: 'result.place3', emoji: '🥉' };
  return { key: 'result.place4', emoji: '🏅' };
}
