'use client' import { useQuery, gql } from '@/lib/graphql/hooks' import { use, useEffect } from 'react' import Link from 'next/link' import { TeamFlag } from '@/components/team-flag' import { MatchCard } from '@/components/match-card' const TEAM_QUERY = gql` query Team($slug: String!) { team(slug: $slug) { id name iso2 slug fifaCode continent confederation stats { appearances wins draws losses goalsFor goalsAgainst goalDiff titles winPct } } } ` const TEAM_MATCHES_QUERY = gql` query TeamMatches($teamName: String!) { topScorers(limit: 100) { playerName goals penalties ownGoals tournaments team { name iso2 } } } ` interface TeamData { id: number; name: string; iso2?: string | null; slug: string fifaCode?: string | null; continent?: string | null; confederation?: string | null stats?: { appearances: number; wins: number; draws: number; losses: number goalsFor: number; goalsAgainst: number; goalDiff: number; titles: number; winPct: number } | null } export default function TeamPage({ params }: { params: Promise<{ slug: string }> }) { const { slug } = use(params) const { data: teamData, loading } = useQuery(TEAM_QUERY, { variables: { slug } }) const team: TeamData | null = teamData?.team ?? null useEffect(() => { document.title = team ? `${team.name} · World Cup` : 'Team · World Cup' }, [team]) // Load all scorers to filter by team const { data: scorerData } = useQuery(gql` query TeamScorers { topScorers(limit: 200) { playerName goals penalties ownGoals tournaments team { id name iso2 } } } `) const allScorers = scorerData?.topScorers ?? [] const teamScorers = team ? allScorers.filter((s: { team?: { id: number } | null }) => s.team?.id === team.id).slice(0, 15) : [] if (loading && !teamData) { return
Loading team…
} if (!team) { return
Team not found.
} const s = team.stats const played = (s?.wins ?? 0) + (s?.draws ?? 0) + (s?.losses ?? 0) const maxScorer = Math.max(...teamScorers.map((sc: { goals: number }) => sc.goals), 1) return (
{/* Hero */}

{team.name}

{team.fifaCode && {team.fifaCode}} {team.confederation && {team.confederation}} {team.continent && {team.continent}} {(s?.titles ?? 0) > 0 && ( {Array.from({ length: s?.titles ?? 0 }).map(() => '🏆').join('')} {s?.titles} title{(s?.titles ?? 0) !== 1 ? 's' : ''} )}
{/* Stats grid */} {s && (

World Cup Record

{[ { label: 'Appearances', value: s.appearances }, { label: 'Matches', value: played }, { label: 'Win %', value: `${s.winPct}%` }, { label: 'Goals For', value: s.goalsFor }, ].map(item => (
{item.label}
{item.value}
))}
TeamWD LGF GAGD
{team.name}
{[s.wins, s.draws, s.losses, s.goalsFor, s.goalsAgainst].map((v, i) => ( {v} ))} {s.goalDiff >= 0 ? `+${s.goalDiff}` : s.goalDiff}
)}
{/* Sidebar: top scorers */}
{teamScorers.length > 0 && (

Top Scorers

{teamScorers.map((sc: { playerName: string; goals: number; penalties: number; tournaments: number }, i: number) => (
{i + 1}
{sc.playerName}
{sc.tournaments} WC{sc.tournaments !== 1 ? 's' : ''}{sc.penalties > 0 ? ` · ${sc.penalties}P` : ''}
{sc.goals}
))}
)}
) }