'use client' import { useState } from 'react' import { useRouter, useSearchParams } from 'next/navigation' import { createCourse } from '@/app/actions/courses' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { ChevronLeft } from 'lucide-react' const TEE_COLORS = [ { label: 'Yellow', value: '#EAB308' }, { label: 'Red', value: '#DC2626' }, { label: 'White', value: '#F9FAFB' }, { label: 'Blue', value: '#2563EB' }, { label: 'Black', value: '#111827' }, { label: 'Green', value: '#16A34A' }, { label: 'Gold', value: '#D97706' }, ] type Tee = { name: string; color: string; course_rating: string; slope_rating: string } type Hole = { hole_number: number; par: number; stroke_index: number } function defaultHoles(count: number): Hole[] { return Array.from({ length: count }, (_, i) => ({ hole_number: i + 1, par: 4, stroke_index: i + 1, })) } export default function NewCoursePage() { const router = useRouter() const searchParams = useSearchParams() const returnTo = searchParams.get('returnTo') ?? '/rounds/new' const [step, setStep] = useState(1) const [error, setError] = useState(null) const [loading, setLoading] = useState(false) // Step 1 const [name, setName] = useState('') const [par, setPar] = useState('72') const [holesCount, setHolesCount] = useState<9 | 18>(18) // Step 2 const [tees, setTees] = useState([]) const [teeForm, setTeeForm] = useState({ name: '', color: '#2563EB', course_rating: '', slope_rating: '', }) // Step 3 const [holes, setHoles] = useState(defaultHoles(18)) const parSum = holes.slice(0, holesCount).reduce((s, h) => s + h.par, 0) const parTarget = parseInt(par) || 0 const parValid = parSum === parTarget function addTee() { if (!teeForm.name || !teeForm.course_rating || !teeForm.slope_rating) return setTees([...tees, teeForm]) setTeeForm({ name: '', color: '#2563EB', course_rating: '', slope_rating: '' }) } function removeTee(i: number) { setTees(tees.filter((_, idx) => idx !== i)) } function updateHole(index: number, field: 'par' | 'stroke_index', value: number) { setHoles(holes.map((h, i) => (i === index ? { ...h, [field]: value } : h))) } async function handleSubmit() { setLoading(true) setError(null) const result = await createCourse({ name, par: parseInt(par), tees: tees.map((t) => ({ name: t.name, color: t.color, courseRating: parseFloat(t.course_rating), slopeRating: parseInt(t.slope_rating), })), holes: holes.slice(0, holesCount).map((h) => ({ holeNumber: h.hole_number, par: h.par, strokeIndex: h.stroke_index, })), }) if (result.error) { setError(result.error) setLoading(false) return } router.push(`${returnTo}?course_id=${result.courseId}`) } const steps = ['Course', 'Tees', 'Holes'] return (
{/* Header */}

New course

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

Course details

setName(e.target.value)} placeholder="e.g. Augusta National" autoFocus />
{([9, 18] as const).map((n) => ( ))}
setPar(e.target.value)} min={27} max={90} />
)} {/* Step 2: Tees */} {step === 2 && (

Tees

{/* Existing tees */} {tees.length > 0 && (
{tees.map((t, i) => (
{t.name}
Rating {t.course_rating} · Slope {t.slope_rating}
))}
)} {/* Add tee form */}

Add tee

{TEE_COLORS.map((c) => (
setTeeForm({ ...teeForm, name: e.target.value })} placeholder="e.g. White" />
setTeeForm({ ...teeForm, course_rating: e.target.value })} placeholder="71.5" step="0.1" />
setTeeForm({ ...teeForm, slope_rating: e.target.value })} placeholder="125" />
)} {/* Step 3: Holes */} {step === 3 && (

Holes

Par sum: {parSum} / {parTarget}
{holes.slice(0, holesCount).map((hole, i) => ( ))}
Hole Par SI
{hole.hole_number}
{[3, 4, 5].map((p) => ( ))}
updateHole(i, 'stroke_index', parseInt(e.target.value) || 1)} min={1} max={holesCount} className="h-8 w-14 rounded border border-border bg-background px-2 text-sm" />
{error &&

{error}

}
)}
) }