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 const dynamic = "force-dynamic";

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">
      {/* Background Image */}
      <div
        className="absolute inset-0 bg-cover bg-center bg-no-repeat"
        style={{
          backgroundImage: `url('/got-light.jpg')`,
        }}
      >
        {/* Dark overlay for readability */}
        <div className="absolute inset-0 bg-gradient-to-b from-black/70 via-black/60 to-black/80"></div>
      </div>
      <NavigationWrapper />
      <AdminPanel initialPreferences={sitePreferences} />
    </main>
  );
}
