import { requireAuth } from "@/lib/session";
import { db } from "@/lib/db";
import TeamBuilder from "./TeamBuilder";

export default async function TeamPage() {
  const session = await requireAuth();
  const userId = (session.user as any).id;

  const team = await db.team.findUnique({
    where: { userId },
    include: {
      players: {
        include: { player: { include: { country: true } } },
      },
    },
  });

  const allPlayers = await db.player.findMany({
    include: { country: true },
    orderBy: { totalPoints: "desc" },
  });

  const specialCards = await db.goldenCard.findMany({
    where: {
      userId,
      status: "OPENED",
      state: { not: "SOLD" },
    },
    include: {
      player: { include: { country: true } },
      teamPlayer: true,
    },
    orderBy: { acquiredDate: "desc" },
  });

  return <TeamBuilder team={team} allPlayers={allPlayers} initialSpecialCards={specialCards as any} />;
}
