import { redirect } from "next/navigation"; import { createClient } from "@/lib/supabase/server"; import { FormDonut, GoalsTrendChart, TopScorersChart, StatCards, } from "@/components/manager/TeamCharts"; import { GlassCard } from "@/components/ui/glass-card"; import { TeamBadge } from "@/components/teams/TeamBadge"; export default async function MyTeamPage({ params, }: { params: Promise<{ leagueId: string; competitionId: string }>; }) { const { leagueId, competitionId } = await params; const supabase = await createClient(); const { data: { user }, } = await supabase.auth.getUser(); if (!user) redirect("/login"); const { data: teamsInComp } = await supabase .from("teams") .select("id") .eq("competition_id", competitionId); const teamIds = teamsInComp?.map((t) => t.id) ?? []; const { data: membership } = await supabase .from("team_members") .select("team_id, teams(*)") .eq("user_id", user.id) .eq("role", "manager") .in("team_id", teamIds.length ? teamIds : ["00000000-0000-0000-0000-000000000000"]) .limit(1) .maybeSingle(); if (!membership) { return (

You are not a team manager in this competition.

); } const team = membership.teams as { id: string; name: string; logo_path: string | null; }; const { data: results } = await supabase .from("team_match_results") .select("*") .eq("team_id", team.id) .order("matchday", { ascending: true }); const { data: playerStats } = await supabase .from("player_competition_stats") .select("*") .eq("team_id", team.id) .eq("competition_id", competitionId) .order("goals", { ascending: false }); const formCounts = { W: 0, D: 0, L: 0 }; results?.slice(-10).forEach((r) => { if (r.result in formCounts) formCounts[r.result as keyof typeof formCounts]++; }); const formData = Object.entries(formCounts).map(([name, value]) => ({ name, value, })); const goalsTrend = results?.map((r, i) => ({ matchday: r.matchday ?? i + 1, gf: r.goals_for ?? 0, ga: r.goals_against ?? 0, })) ?? []; const topScorers = playerStats?.slice(0, 8).map((p) => ({ name: p.player_name, goals: p.goals, })) ?? []; const topAssists = playerStats ?.slice() .sort((a, b) => b.assists - a.assists) .slice(0, 8) .map((p) => ({ name: p.player_name, assists: p.assists, })) ?? []; const played = results?.length ?? 0; const won = results?.filter((r) => r.result === "W").length ?? 0; const drawn = results?.filter((r) => r.result === "D").length ?? 0; const lost = results?.filter((r) => r.result === "L").length ?? 0; const gf = results?.reduce((s, r) => s + (r.goals_for ?? 0), 0) ?? 0; const ga = results?.reduce((s, r) => s + (r.goals_against ?? 0), 0) ?? 0; return (
Team settings →
d.value > 0)} />
{playerStats?.map((p) => ( ))}
Player Apps G A G+A
{p.player_name} {p.appearances} {p.goals} {p.assists} {p.goals + p.assists}
    {results ?.slice() .reverse() .slice(0, 5) .map((r) => (
  • vs {r.opponent_name} {r.goals_for}–{r.goals_against}{" "} {r.result}
  • ))}
); }