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 };
}
