'use client';

import { createContext, useContext, type ReactNode } from 'react';
import type { GameConnection } from '@/lib/game';

/** What every TV scene needs besides the game state: the clock source and how to join. */
export interface TvEnv {
  conn: GameConnection | null;
  /** Server-rendered QR code (an <svg>) pointing at this room's join link. */
  qr: ReactNode;
  joinCode: string;
  /** Short, typeable join link, e.g. "pintrush.com/join/HARBOR". */
  joinUrlText: string;
  venueName: string;
  /** IANA zone of the venue, for scheduled times. */
  timeZone: string;
}

const TvEnvContext = createContext<TvEnv | null>(null);

export function TvEnvProvider({ value, children }: { value: TvEnv; children: ReactNode }) {
  return <TvEnvContext.Provider value={value}>{children}</TvEnvContext.Provider>;
}

export function useTvEnv(): TvEnv {
  const env = useContext(TvEnvContext);
  if (!env) throw new Error('useTvEnv must be used inside <TvEnvProvider>');
  return env;
}
