"use client" import { useState } from "react" import { useRouter } from "next/navigation" export function RecalculateEloButton() { const router = useRouter() const [isLoading, setIsLoading] = useState(false) const handleClick = async () => { if (!confirm('Are you sure you want to recalculate all ELO ratings? This will reset all player stats and recalculate them from match history. This operation is idempotent and safe to run multiple times.')) { return } setIsLoading(true) try { const response = await fetch('/api/admin/recalculate-elo', { method: 'POST', headers: { 'Content-Type': 'application/json', }, }) if (!response.ok) { try { const errorData = await response.json() alert(`Error: ${errorData.error || 'Failed to recalculate'}`) } catch { alert(`Error: ${response.status} ${response.statusText}`) } return } const data = await response.json() if (data.success) { alert(`Recalculation completed: ${JSON.stringify(data.data)}`) router.refresh() } else { alert(`Error: ${data.error}`) } } catch (err) { alert(`Error: ${err instanceof Error ? err.message : 'Unknown error occurred'}`) } finally { setIsLoading(false) } } return ( ) }