import React, { useState, useEffect } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { ArrowLeft, Users, Trophy, Crown, Copy, Check } from 'lucide-react'; import { toast } from 'sonner'; import apiclient from '../apiclient'; import type { CreateTableRequest } from '../apiclient/data-contracts'; import { useUser } from '@stackframe/react'; interface VariantConfig { id: string; title: string; wildJokerMode: 'no_joker' | 'close_joker' | 'open_joker'; description: string; color: string; } const variantConfigs: Record = { no_wildcard: { id: 'no_wildcard', title: 'No Wild Card', wildJokerMode: 'no_joker', description: 'Pure classic rummy with printed jokers only', color: 'from-blue-500 to-cyan-500' }, open_wildcard: { id: 'open_wildcard', title: 'Open Wild Card', wildJokerMode: 'open_joker', description: 'Traditional rummy with wild card revealed at start', color: 'from-green-500 to-emerald-500' }, close_wildcard: { id: 'close_wildcard', title: 'Close Wild Card', wildJokerMode: 'close_joker', description: 'Advanced variant - wild card reveals after first sequence', color: 'from-purple-500 to-pink-500' } }; export default function CreateTable() { const navigate = useNavigate(); const [sp] = useSearchParams(); const user = useUser(); const variantId = sp.get('variant') || 'open_wildcard'; const variant = variantConfigs[variantId] || variantConfigs.open_wildcard; const [playerName, setPlayerName] = useState('Player'); const [maxPlayers, setMaxPlayers] = useState(4); const [disqualifyScore, setDisqualifyScore] = useState(200); const [aceValue, setAceValue] = useState<1 | 10>(10); const [creating, setCreating] = useState(false); const [generatedCode, setGeneratedCode] = useState(''); const [tableId, setTableId] = useState(null); const [copied, setCopied] = useState(false); useEffect(() => { if (user?.displayName) { setPlayerName(user.displayName); } }, [user]); const handleCreateRoom = async () => { if (!playerName.trim()) { toast.error('Enter your name'); return; } // STRICT VALIDATION - prevent constraint violations if (aceValue !== 1 && aceValue !== 10) { toast.error(`Invalid ace value: ${aceValue}. Please refresh your browser (Ctrl+Shift+R)`); console.error('❌ INVALID ACE VALUE:', aceValue, 'Expected: 1 or 10'); return; } if (!variant.wildJokerMode || variant.wildJokerMode === '') { toast.error('Invalid game mode. Please refresh your browser (Ctrl+Shift+R)'); console.error('❌ INVALID WILD JOKER MODE:', variant.wildJokerMode); return; } setCreating(true); try { const body: CreateTableRequest = { max_players: maxPlayers, disqualify_score: disqualifyScore, wild_joker_mode: variant.wildJokerMode, ace_value: aceValue, }; // 🔍 DETAILED FRONTEND LOGGING - Check console! console.log('🎯 [CREATE TABLE DEBUG]', { variantId, variantTitle: variant.title, wildJokerMode: variant.wildJokerMode, wildJokerModeType: typeof variant.wildJokerMode, aceValue, aceValueType: typeof aceValue, fullBody: body, timestamp: new Date().toISOString() }); const res = await apiclient.create_table(body); const data = await res.json(); setGeneratedCode(data.code); setTableId(data.table_id); toast.success('Room created successfully!'); } catch (e: any) { console.error('❌ [CREATE TABLE ERROR]', e); const errorMsg = e?.error?.detail || e?.message || 'Failed to create room'; toast.error(`Error: ${errorMsg}. Try refreshing (Ctrl+Shift+R)`); } finally { setCreating(false); } }; const handleStartGame = () => { if (tableId) { navigate(`/Table?tableId=${tableId}`); } }; const copyToClipboard = () => { if (!generatedCode) return; navigator.clipboard.writeText(generatedCode); setCopied(true); toast.success('Code copied to clipboard!'); setTimeout(() => setCopied(false), 2000); }; if (generatedCode && tableId) { return (
{/* Header */}

{variant.title}

{variant.description}

Room Created!

Share this code with your friends

{/* Room Code Display */}

Room Code

{generatedCode}

{/* Room Details */}
Host: {playerName}
Game Mode: {variant.title}
Max Players: {maxPlayers}
Disqualify Score: {disqualifyScore} pts
Ace Value: {aceValue} pt{aceValue === 1 ? '' : 's'}
{/* Actions */}
); } return (
{/* Header */}

{variant.title}

{variant.description}

Create Private Room

{/* Player Name */}
setPlayerName(e.target.value)} placeholder="Enter your name" className="bg-slate-900/50 border-slate-600 text-white placeholder:text-slate-500" />
{/* Max Players */}
{/* Disqualification Score */}

Players reaching this score will be disqualified

{/* Ace Value */}
{/* Create Button */}
); }