'use client' import { useState } from 'react' import { useRouter, useSearchParams } from 'next/navigation' import Link from 'next/link' import { createRoundWithInvites } from '@/app/actions/rounds' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { ChevronLeft, Plus, X } from 'lucide-react' type Tee = { id: string name: string color: string course_rating: number slope_rating: number } type Course = { id: string name: string par: number tees: Tee[] } type WizardData = { courseId: string teeId: string date: string holesCount: 9 | 18 name: string emails: string[] } export function NewRoundWizard({ courses }: { courses: Course[] }) { const router = useRouter() const searchParams = useSearchParams() const preselectedCourseId = searchParams.get('course_id') ?? '' const [step, setStep] = useState(preselectedCourseId ? 2 : 1) const [error, setError] = useState(null) const [loading, setLoading] = useState(false) const [emailInput, setEmailInput] = useState('') const [data, setData] = useState({ courseId: preselectedCourseId, teeId: '', date: new Date().toISOString().split('T')[0], holesCount: 18, name: '', emails: [], }) const selectedCourse = courses.find((c) => c.id === data.courseId) const steps = ['Course', 'Tee', 'Details', 'Invite'] function addEmail() { const email = emailInput.trim().toLowerCase() if (!email || data.emails.includes(email)) return setData({ ...data, emails: [...data.emails, email] }) setEmailInput('') } function removeEmail(email: string) { setData({ ...data, emails: data.emails.filter((e) => e !== email) }) } async function handleCreate() { setLoading(true) setError(null) const result = await createRoundWithInvites({ course_id: data.courseId, tee_id: data.teeId, date: data.date, holes_count: data.holesCount, name: data.name || undefined, emails: data.emails, }) if ('error' in result && result.error) { setError(result.error) setLoading(false) return } if (result.failedInvites?.length) { setError(`Round created. Could not invite: ${result.failedInvites.join(', ')}`) } router.push(`/rounds/${result.roundId}`) } return (
{/* Header */}

New round

{steps.map((s, i) => (
))}
{/* Step 1: Course selection */} {step === 1 && (

Select course

{courses.length === 0 ? (

No courses yet.{' '} Create one first.

) : (
{courses.map((course) => ( ))}
)} Add new course
)} {/* Step 2: Tee selection */} {step === 2 && selectedCourse && (

Select tees

{selectedCourse.name}

{selectedCourse.tees.map((tee) => ( ))}
)} {/* Step 3: Round details */} {step === 3 && (

Round details

{([9, 18] as const).map((n) => ( ))}
setData({ ...data, date: e.target.value })} />
setData({ ...data, name: e.target.value })} placeholder="e.g. Sunday morning four-ball" />
)} {/* Step 4: Invite players */} {step === 4 && (

Invite players

Each player will receive a magic link by email.{' '} You can skip this and invite later from the lobby.

{/* Email input */}
setEmailInput(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && addEmail()} placeholder="player@example.com" className="flex-1" />
{/* Email list */} {data.emails.length > 0 && (
{data.emails.map((email) => (
{email}
))}
)} {error &&

{error}

}
)}
) }