import type { PublicState, RoundPhase } from '@pintrush/engine';

/** Host-facing phase names. */
export const PHASE_LABELS: Record<RoundPhase, string> = {
  INTRO: 'Round intro',
  PENDING: 'Up next',
  OPEN: 'Answers open',
  LOCKED: 'Answers locked',
  REVEALED: 'Answer revealed',
  ROUND_OVER: 'Round results',
};

/** The game's state from the host's point of view, combining the live session with the stored event status. */
export type ControlStatus = 'CONNECTING' | 'NOT_OPEN' | 'WAITING' | 'RUNNING' | 'PAUSED' | 'COMPLETED' | 'CANCELLED';

export const CONTROL_STATUS_COPY: Record<ControlStatus, { title: string; detail: string; badge: string }> = {
  CONNECTING: { title: 'Connecting…', detail: 'Reaching the game server.', badge: 'WAITING' },
  NOT_OPEN: { title: 'Not open yet', detail: 'Open the lobby so guests can join, then start the show.', badge: 'SCHEDULED' },
  WAITING: { title: 'Lobby open', detail: 'Guests are joining. Start the show when the room is ready.', badge: 'WAITING' },
  RUNNING: { title: 'Live', detail: 'The game runs itself — step in only if you need to.', badge: 'LIVE' },
  PAUSED: { title: 'Paused', detail: 'Timers are frozen on every screen. Resume to carry on.', badge: 'PAUSED' },
  COMPLETED: { title: 'Finished', detail: 'Final standings are locked in.', badge: 'COMPLETED' },
  CANCELLED: { title: 'Cancelled', detail: 'This show was cancelled and can’t be opened.', badge: 'CANCELLED' },
};

/** What the big panel says the room is doing right now. */
export function nowPlaying(pub: PublicState): { headline: string; sub: string | null } {
  const r = pub.round;
  if (!r) {
    if (pub.status === 'WAITING') return { headline: 'Lobby', sub: pub.plan?.label ? `First up: ${pub.plan.label}` : null };
    if (pub.status === 'COMPLETED') return { headline: 'Show over', sub: null };
    const next = pub.plan ? (pub.plan.index < pub.plan.total ? pub.plan.label : null) : 'The Rush';
    return { headline: 'Break between rounds', sub: next ? `Next: ${next}` : null };
  }
  const q = pub.question;
  if (r.phase === 'INTRO') return { headline: r.label, sub: 'Round intro' };
  if (r.phase === 'ROUND_OVER') return { headline: r.label, sub: 'Round results on screen' };
  if (q && r.phase === 'PENDING') return { headline: `Up next: ${q.isWildcard ? 'Wildcard' : q.categoryLabel}`, sub: `Question ${q.index + 1} of ${r.questionCount}` };
  if (q) return { headline: `Question ${q.index + 1} of ${r.questionCount}`, sub: `${q.isWildcard ? 'Wildcard' : q.categoryLabel}${q.difficulty === 'DIFFICULT' ? ' · difficult' : ''}` };
  return { headline: r.label, sub: null };
}

/**
 * Tables for the host's stat card: teams with 2+ members, the same rule the Teams board uses
 * (ARCHITECTURE §2.5). One-member teams rank on the Solo board until a tablemate joins.
 */
export function tableSummary(teams: readonly { name: string; members: number }[]): { tables: number; waiting: string[] } {
  return { tables: teams.filter((t) => t.members >= 2).length, waiting: teams.filter((t) => t.members === 1).map((t) => t.name) };
}

/** Empty-board text for the Teams panel that agrees with the stat card. */
export function emptyTeamsText(waiting: readonly string[]): string {
  if (waiting.length === 0) return 'No teams yet. Guests can enter a team name when they join.';
  const names = waiting.length === 1 ? waiting[0] : waiting.length === 2 ? `${waiting[0]} and ${waiting[1]}` : `${waiting.length} teams`;
  return `No table has 2 players yet — ${names} ${waiting.length === 1 ? 'is' : 'are'} waiting for tablemates.`;
}
