import { createClient } from "@/lib/supabase/server";
import { getCurrentProfile } from "@/lib/auth/profile";
import { IssuesPanel } from "@/components/issues/issues-panel";

export default async function ManagerIssuesPage() {
  const supabase = await createClient();
  const ctx = await getCurrentProfile();

  const { data: memberships } = await supabase
    .from("team_members")
    .select("teams(competitions(league_id, leagues(id, name)))")
    .eq("user_id", ctx!.user.id);

  const leagueMap = new Map<string, string>();
  memberships?.forEach((m) => {
    const team = m.teams as {
      competitions: { leagues: { id: string; name: string } | null } | null;
    } | null;
    const league = team?.competitions?.leagues;
    if (league) leagueMap.set(league.id, league.name);
  });

  const leagues = [...leagueMap.entries()].map(([id, name]) => ({ id, name }));

  return <IssuesPanel leagues={leagues} asMaster={false} />;
}
