'use client'; import { useState, useEffect } from 'react'; import { useParams, useRouter } from 'next/navigation'; import Link from 'next/link'; import { AlertTriangle, Clock, CheckCircle, Flag, Calendar, MapPin, AlertCircle, Video, Gavel, ArrowLeft, Scale, ChevronRight, Users, Trophy, } from 'lucide-react'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Breadcrumbs from '@/components/layout/Breadcrumbs'; import { getGetRaceProtestsUseCase, getGetRacePenaltiesUseCase, getRaceRepository, getLeagueRepository, getLeagueMembershipRepository, } from '@/lib/di-container'; import { useEffectiveDriverId } from '@/lib/currentDriver'; import { isLeagueAdminOrHigherRole } from '@/lib/leagueRoles'; import { RaceProtestsPresenter } from '@/lib/presenters/RaceProtestsPresenter'; import { RacePenaltiesPresenter } from '@/lib/presenters/RacePenaltiesPresenter'; import type { RaceProtestViewModel } from '@gridpilot/racing/application/presenters/IRaceProtestsPresenter'; import type { RacePenaltyViewModel } from '@gridpilot/racing/application/presenters/IRacePenaltiesPresenter'; import type { League } from '@gridpilot/racing/domain/entities/League'; import type { Race } from '@gridpilot/racing/domain/entities/Race'; export default function RaceStewardingPage() { const params = useParams(); const router = useRouter(); const raceId = params.id as string; const currentDriverId = useEffectiveDriverId(); const driversById: Record = {}; const [race, setRace] = useState(null); const [league, setLeague] = useState(null); const [protests, setProtests] = useState([]); const [penalties, setPenalties] = useState([]); const [loading, setLoading] = useState(true); const [isAdmin, setIsAdmin] = useState(false); const [activeTab, setActiveTab] = useState<'pending' | 'resolved' | 'penalties'>('pending'); useEffect(() => { async function loadData() { setLoading(true); try { const raceRepo = getRaceRepository(); const leagueRepo = getLeagueRepository(); const membershipRepo = getLeagueMembershipRepository(); const protestsUseCase = getGetRaceProtestsUseCase(); const penaltiesUseCase = getGetRacePenaltiesUseCase(); const raceData = await raceRepo.findById(raceId); if (!raceData) { setLoading(false); return; } setRace(raceData); const leagueData = await leagueRepo.findById(raceData.leagueId); setLeague(leagueData); if (leagueData) { const membership = await membershipRepo.getMembership( leagueData.id, currentDriverId, ); setIsAdmin(membership ? isLeagueAdminOrHigherRole(membership.role) : false); } const protestsPresenter = new RaceProtestsPresenter(); await protestsUseCase.execute({ raceId }, protestsPresenter); const protestsViewModel = protestsPresenter.getViewModel(); setProtests(protestsViewModel?.protests ?? []); const penaltiesPresenter = new RacePenaltiesPresenter(); await penaltiesUseCase.execute({ raceId }, penaltiesPresenter); const penaltiesViewModel = penaltiesPresenter.getViewModel(); setPenalties(penaltiesViewModel?.penalties ?? []); } catch (err) { console.error('Failed to load data:', err); } finally { setLoading(false); } } loadData(); }, [raceId, currentDriverId]); const pendingProtests = protests.filter( (p) => p.status === 'pending' || p.status === 'under_review', ); const resolvedProtests = protests.filter( (p) => p.status === 'upheld' || p.status === 'dismissed' || p.status === 'withdrawn', ); const getStatusBadge = (status: string) => { switch (status) { case 'pending': case 'under_review': return ( Pending ); case 'upheld': return ( Upheld ); case 'dismissed': return ( Dismissed ); case 'withdrawn': return ( Withdrawn ); default: return null; } }; const formatDate = (date: Date | string) => { const d = typeof date === 'string' ? new Date(date) : date; return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', }); }; if (loading) { return (
); } if (!race) { return (

Race not found

The race you're looking for doesn't exist.

); } const breadcrumbItems = [ { label: 'Races', href: '/races' }, { label: race.track, href: `/races/${race.id}` }, { label: 'Stewarding' }, ]; return (
{/* Navigation */}
{/* Header */}

Stewarding

{race.track} • {formatDate(race.scheduledAt)}

{/* Stats */}
Pending
{pendingProtests.length}
Resolved
{resolvedProtests.length}
Penalties
{penalties.length}
{/* Tab Navigation */}
{/* Content */} {activeTab === 'pending' && (
{pendingProtests.length === 0 ? (

All Clear!

No pending protests to review

) : ( pendingProtests.map((protest) => { const protester = driversById[protest.protestingDriverId]; const accused = driversById[protest.accusedDriverId]; const daysSinceFiled = Math.floor( (Date.now() - new Date(protest.filedAt).getTime()) / (1000 * 60 * 60 * 24) ); const isUrgent = daysSinceFiled > 2; return (
{protester?.name || 'Unknown'} vs {accused?.name || 'Unknown'} {getStatusBadge(protest.status)} {isUrgent && ( {daysSinceFiled}d old )}
Lap {protest.incident.lap} Filed {formatDate(protest.filedAt)} {protest.proofVideoUrl && ( <> )}

{protest.incident.description}

{isAdmin && league && ( )}
); }) )}
)} {activeTab === 'resolved' && (
{resolvedProtests.length === 0 ? (

No Resolved Protests

Resolved protests will appear here

) : ( resolvedProtests.map((protest) => { const protester = driversById[protest.protestingDriverId]; const accused = driversById[protest.accusedDriverId]; return (
{protester?.name || 'Unknown'} vs {accused?.name || 'Unknown'} {getStatusBadge(protest.status)}
Lap {protest.incident.lap} Filed {formatDate(protest.filedAt)}

{protest.incident.description}

{protest.decisionNotes && (

Steward Decision

{protest.decisionNotes}

)}
); }) )}
)} {activeTab === 'penalties' && (
{penalties.length === 0 ? (

No Penalties

Penalties issued for this race will appear here

) : ( penalties.map((penalty) => { const driver = driversById[penalty.driverId]; return (
{driver?.name || 'Unknown'} {penalty.type.replace('_', ' ')}

{penalty.reason}

{penalty.notes && (

{penalty.notes}

)}
{penalty.type === 'time_penalty' && `+${penalty.value}s`} {penalty.type === 'grid_penalty' && `+${penalty.value} grid`} {penalty.type === 'points_deduction' && `-${penalty.value} pts`} {penalty.type === 'disqualification' && 'DSQ'} {penalty.type === 'warning' && 'Warning'} {penalty.type === 'license_points' && `${penalty.value} LP`}
); }) )}
)}
); }