import { getStandingRepository, getLeagueRepository, getTeamMembershipRepository } from '@/lib/di-container'; export interface TeamLeagueStandingViewModel { leagueId: string; leagueName: string; position: number; points: number; wins: number; racesCompleted: number; } export interface TeamStandingsViewModel { standings: TeamLeagueStandingViewModel[]; } /** * Compute team standings across the given leagues for a team. * Mirrors the previous TeamStandings component logic but keeps it out of the UI layer. */ export async function loadTeamStandings( teamId: string, leagues: string[], ): Promise { const standingRepo = getStandingRepository(); const leagueRepo = getLeagueRepository(); const teamMembershipRepo = getTeamMembershipRepository(); const members = await teamMembershipRepo.getTeamMembers(teamId); const memberIds = members.map((m) => m.driverId); const teamStandings: TeamLeagueStandingViewModel[] = []; for (const leagueId of leagues) { const league = await leagueRepo.findById(leagueId); if (!league) continue; const leagueStandings = await standingRepo.findByLeagueId(leagueId); let totalPoints = 0; let totalWins = 0; let totalRaces = 0; for (const standing of leagueStandings) { if (memberIds.includes(standing.driverId)) { totalPoints += standing.points; totalWins += standing.wins; totalRaces = Math.max(totalRaces, standing.racesCompleted); } } // Simplified team position based on total points (same spirit as previous logic) const allTeamPoints = leagueStandings .filter((s) => memberIds.includes(s.driverId)) .reduce((sum, s) => sum + s.points, 0); const position = leagueStandings .filter((_, idx, arr) => { const teamPoints = arr .filter((s) => memberIds.includes(s.driverId)) .reduce((sum, s) => sum + s.points, 0); return teamPoints > allTeamPoints; }).length + 1; teamStandings.push({ leagueId, leagueName: league.name, position, points: totalPoints, wins: totalWins, racesCompleted: totalRaces, }); } return { standings: teamStandings }; }