import { redirect } from "next/navigation";
import { auth } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { Role } from "@/prisma/generated/prisma/client";
import NavigationWrapper from "@/components/NavigationWrapper";
import AdminPanel from "@/components/AdminPanel";

export default async function AdminPage() {
  const session = await auth();

  if (!session?.user) {
    redirect("/login");
  }

  if (session.user.role !== Role.ADMIN) {
    redirect("/");
  }

  // Récupérer les préférences globales du site
  let sitePreferences = await prisma.sitePreferences.findUnique({
    where: { id: "global" },
  });

  // Si elles n'existent pas, créer une entrée par défaut
  if (!sitePreferences) {
    sitePreferences = await prisma.sitePreferences.create({
      data: {
        id: "global",
        homeBackground: null,
        eventsBackground: null,
        leaderboardBackground: null,
      },
    });
  }

  return (
    <main className="min-h-screen bg-black relative">
      <NavigationWrapper />
      <AdminPanel initialPreferences={sitePreferences} />
    </main>
  );
}
