"use client"; import { useCallback, useState } from "react"; import { useRouter } from "next/navigation"; import { api } from "@/lib/api/client"; import { GlassCard } from "@/components/ui/glass-card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { TeamIconPicker } from "@/components/teams/team-icon-picker"; import { TeamIcon } from "@/components/teams/TeamIcon"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { Trash2 } from "lucide-react"; export type DraftTeam = { id: string; name: string; nickname: string | null; icon: string | null; logo_path: string | null; }; export function CompetitionDraftPanel({ competitionId, initialTeams, }: { competitionId: string; initialTeams: DraftTeam[]; }) { const router = useRouter(); const [teams, setTeams] = useState(initialTeams); const [name, setName] = useState(""); const [nickname, setNickname] = useState(""); const [icon, setIcon] = useState("shield"); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [confirmAction, setConfirmAction] = useState< | { type: "activate" } | { type: "fixtures" } | { type: "delete-team"; team: DraftTeam } | null >(null); const refreshTeams = useCallback(async () => { const list = (await api.competitions.listTeams(competitionId)) as DraftTeam[]; setTeams(list); }, [competitionId]); async function run(fn: () => Promise, refresh = true) { setLoading(true); setError(null); try { await fn(); if (refresh) { await refreshTeams(); router.refresh(); } } catch (e) { setError(e instanceof Error ? e.message : "Error"); } finally { setLoading(false); setConfirmAction(null); } } async function handleAddTeam(e: React.FormEvent) { e.preventDefault(); if (loading) return; const trimmed = name.trim(); if (!trimmed) return; setLoading(true); setError(null); try { const created = (await api.competitions.createTeam(competitionId, { name: trimmed, nickname: nickname.trim() || undefined, icon, })) as DraftTeam; setTeams((prev) => [...prev, created]); setName(""); setNickname(""); setIcon("shield"); router.refresh(); } catch (err) { setError(err instanceof Error ? err.message : "Failed to add team"); } finally { setLoading(false); } } return (
{error &&

{error}

}
setName(e.target.value)} placeholder="FC Example" required className="mt-1" />
setNickname(e.target.value)} placeholder="The Blues" className="mt-1" />
{teams.length === 0 ? (

No teams yet. Add your first team above.

) : (
{teams.map((t) => ( ))}
Team Nickname
{t.name}
{t.nickname || "—"}
)}
!o && setConfirmAction(null)} title="Activate competition?" description="This snapshots league rules and opens the season. Teams cannot be added casually after activation without master access." confirmLabel="Activate" loading={loading} variant="primary" onConfirm={() => run(() => api.competitions.activate(competitionId), true) } /> !o && setConfirmAction(null)} title="Generate fixtures?" description="This creates all matches for every team. Make sure your team list is final." confirmLabel="Generate" loading={loading} variant="primary" onConfirm={() => run(() => api.competitions.generateFixtures(competitionId), true) } /> !o && setConfirmAction(null)} title="Remove team?" description={`Delete "${confirmAction?.type === "delete-team" ? confirmAction.team.name : ""}" from this competition? This cannot be undone.`} confirmLabel="Delete team" loading={loading} onConfirm={() => { if (confirmAction?.type !== "delete-team") return; const id = confirmAction.team.id; return run(async () => { await api.teams.delete(id); setTeams((prev) => prev.filter((t) => t.id !== id)); }, true); }} />
); }