import type { Metadata } from "next";
import AdminSidebar from "@/app/admin/AdminSidebar";
import { headers } from "next/headers";
import { redirect } from "next/navigation";

export const metadata: Metadata = {
  title: "Admin | ASSM",
};

export default async function AdminLayout({ children }: { children: React.ReactNode }) {
  // 서버에서 쿠키 기반 접근 제어 (미들웨어 보조)
  const h = await headers();
  const cookieHeader = h.get("cookie") || "";
  const uid = cookieHeader
    .split(";")
    .map((s) => s.trim())
    .find((pair) => pair.startsWith("uid="))
    ?.split("=")[1];
  const isAdmin = cookieHeader
    .split(";")
    .map((s) => s.trim())
    .find((pair) => pair.startsWith("isAdmin="))
    ?.split("=")[1];
  if (!uid) {
    redirect("/login");
  }
  if (isAdmin !== "1") {
    redirect("/");
  }
  return (
    <div className="min-h-[calc(100vh-0px)] flex">
      <AdminSidebar />
      <main className="flex-1 min-w-0 bg-[#F7F7F7]">
        <div className="max-w-[1920px] mx-auto px-4 py-6">
          {children}
        </div>
      </main>
    </div>
  );
}


