import { redirect } from "next/navigation";
import { isUserAuthenticated } from "@/lib/server-auth";
import { AdminService } from "@/services/admin-service";
import { TeamsManagementPage } from "@/components/admin/teams";

export default async function TeamsPage() {
  // Vérifier l'authentification
  const isAuthenticated = await isUserAuthenticated();

  // Si pas de cookie d'authentification, rediriger vers login
  if (!isAuthenticated) {
    redirect("/login");
  }

  // Charger les données côté serveur
  try {
    const adminData = await AdminService.getAdminData();

    return (
      <TeamsManagementPage
        teams={adminData.teams}
        teamStats={adminData.teamStats}
        skillCategories={adminData.skillCategories}
      />
    );
  } catch (error) {
    console.error("Failed to load admin data:", error);
    return (
      <div className="container mx-auto p-6">
        <div className="flex items-center justify-center h-64">
          <div className="text-lg text-red-500">
            Erreur lors du chargement des données d'administration
          </div>
        </div>
      </div>
    );
  }
}
