/**
 * Room lookup for /play (server only). Only ACTIVE rooms of non-cancelled venues can be played;
 * the venue's join code builds the "back to join" link used when this phone holds no token.
 */
import { cache } from 'react';
import { getDb, isUuid } from '@pintrush/db';

export interface PlayRoom {
  id: string;
  name: string;
  venue: { name: string; joinCode: string };
}

/** Cached per request so generateMetadata and the page share one query. */
export const findPlayRoom = cache(async (roomId: string): Promise<PlayRoom | null> => {
  if (!isUuid(roomId)) return null;
  return getDb().room.findFirst({
    where: { id: roomId, status: 'ACTIVE', venue: { status: { not: 'CANCELLED' } } },
    select: { id: true, name: true, venue: { select: { name: true, joinCode: true } } },
  });
});
