/**
 * Test fixtures for the TV helpers: minimal but type-complete engine views. Test-only module.
 */
import type { PublicState, Standing, TeamStanding } from '@pintrush/engine';
import type { RoomSnapshot } from '@pintrush/protocol';

const SESSION = '5b7d8f2e-1c3a-4e5f-9a0b-1c2d3e4f5a6b';
const EVENT = '6c8e9a3f-2d4b-4f6a-8b1c-2d3e4f5a6b7c';
const ROOM = '7d9fab40-3e5c-4a7b-9c2d-3e4f5a6b7c8d';
const VENUE = '8eafbc51-4f6d-4b8c-8d3e-4f5a6b7c8d9e';

export function standing(rank: number, nickname: string, points: number, playerId = `p-${nickname}`): Standing {
  return { rank, playerId, nickname, teamId: null, points, correct: 0, correctTimeMs: 0, lastCorrectAt: null, lines: 0 };
}

/** A table (2+ members) or, with `isSolo: true`, a solo player's entry on the team board. */
export function team(rank: number, name: string, score: number, over: Partial<TeamStanding> = {}): TeamStanding {
  return { rank, teamId: `t-${name}`, name, score, members: 2, activeMembers: 2, isSolo: false, ...over };
}

type Round = NonNullable<PublicState['round']>;
type Question = NonNullable<PublicState['question']>;

export function makeRound(over: Partial<Round> = {}): Round {
  return {
    id: 'round-1',
    index: 0,
    kind: 'RUSH',
    label: 'The Rush',
    categories: [{ key: 'history', label: 'History' }],
    hasBoard: true,
    questionCount: 10,
    qIndex: 0,
    phase: 'OPEN',
    phaseEndsAt: 1_000_000,
    finalists: null,
    estimatedEndAt: 2_000_000,
    ...over,
  };
}

export function makeQuestion(over: Partial<Question> = {}): Question {
  return {
    id: 'q-1',
    index: 0,
    category: 'history',
    categoryLabel: 'History',
    difficulty: 'STANDARD',
    type: 'MULTIPLE_CHOICE',
    isWildcard: false,
    prompt: 'Who was the first emperor of Rome?',
    options: [
      { id: 'a', text: 'Julius Caesar' },
      { id: 'b', text: 'Caligula' },
      { id: 'c', text: 'Nero' },
      { id: 'd', text: 'Augustus' },
    ],
    opensAt: 900_000,
    openedAt: 900_000,
    closesAt: 915_000,
    ...over,
  };
}

export function makePublic(over: Partial<PublicState> = {}): PublicState {
  return {
    sessionId: SESSION,
    eventId: EVENT,
    mode: 'ALWAYS_ON',
    status: 'RUNNING',
    round: null,
    question: null,
    reveal: null,
    answeredCount: 0,
    participantCount: 0,
    playerCount: 0,
    leaderboard: [],
    teamLeaderboard: [],
    soloLeaderboard: null,
    recentJoins: [],
    nextRoundAt: null,
    plan: null,
    lastRound: null,
    pausedAt: null,
    predictions: null,
    ...over,
  };
}

export function makeSnapshot(pub: PublicState | null, over: Partial<RoomSnapshot> = {}): RoomSnapshot {
  return {
    roomId: ROOM,
    venue: { id: VENUE, name: 'The Harbor Tap', joinCode: 'HARBOR' },
    room: { id: ROOM, name: 'Main Bar' },
    activeSessionId: pub ? SESSION : null,
    activeEventKind: pub ? pub.mode : null,
    activeEventName: pub ? 'Always-on' : null,
    activeEventScheduledAt: null,
    activeSessionStatus: pub ? pub.status : null,
    public: pub,
    me: null,
    mySessionId: null,
    mySessionActive: false,
    attract: { nightlyTop: [], nextChampionship: null, nextRoundAt: null },
    seq: 1,
    serverNow: 1_000_000,
    ...over,
  };
}
