import { auth, signOut } from "@/auth";
import Link from "next/link";
import { redirect } from "next/navigation";
import { AdminNav } from "./admin-nav";

const navItems = [
  { href: "/admin", label: "Dashboard" },
  { href: "/admin/users", label: "Users" },
  { href: "/admin/demand", label: "Demand" },
  { href: "/admin/policy", label: "Policy" },
  { href: "/admin/inventory", label: "Inventory" },
  { href: "/admin/verify", label: "Verify Links" },
  { href: "/admin/calendar", label: "Calendar" },
  { href: "/admin/downloads", label: "Downloads" },
  { href: "/admin/corrupt", label: "Corrupt Files" },
  { href: "/admin/storage", label: "Storage" },
  { href: "/admin/jobs", label: "Jobs" },
];

export default async function AdminLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const session = await auth();
  if (!session) redirect("/login");
  if (!session.user.isAdmin) redirect("/");

  return (
    <div className="ampelos-admin flex min-h-screen text-admin-text">
      <nav className="ampelos-admin-sidebar flex w-60 shrink-0 flex-col px-4 py-6">
        <Link href="/" className="mb-7 block">
          <p className="text-xs font-semibold uppercase tracking-[0.32em] text-admin-accent">Sticknife</p>
          <p className="mt-2 font-serif text-2xl font-semibold">Ampelos Admin</p>
        </Link>
        <AdminNav items={navItems} />
        <div className="mt-auto pt-6">
          <form action={async () => { "use server"; await signOut({ redirectTo: "/login" }); }}>
            <button type="submit" className="admin-nav-button w-full px-3 py-2 text-left text-sm font-medium">
              Sign out
            </button>
          </form>
        </div>
      </nav>
      <main className="min-w-0 flex-1 overflow-x-auto px-6 py-8 lg:px-10">{children}</main>
    </div>
  );
}
