import type { Metadata } from "next";
import Link from "next/link";
import { redirect } from "next/navigation";
import { getServerSession } from "next-auth";
import { Lock } from "lucide-react";
import { authOptions } from "@/lib/auth";
import { AdminSidebar } from "@/components/admin/admin-sidebar";

export const metadata: Metadata = {
  title: "Admin · SeTrip",
  alternates: { canonical: "/admin" },
  robots: { index: false, follow: false },
};

/**
 * Layout admin — terpisah penuh dari layout user (navbar/footer publik tidak
 * dipakai). Sidebar kiri jadi shell global untuk semua /admin/*.
 *
 * Auth gate di layout ini berlaku ke seluruh sub-page admin sehingga
 * sub-page tidak perlu re-check (boleh disederhanakan di iterasi berikutnya).
 */
export default async function AdminLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const session = await getServerSession(authOptions);
  if (!session?.user) {
    redirect("/login?callbackUrl=/admin");
  }
  if (!session.user.isAdmin) {
    return (
      <div className="flex min-h-screen items-center justify-center bg-neutral-50 px-4">
        <div className="max-w-md rounded-2xl border border-neutral-200 bg-white p-8 text-center shadow-sm">
          <Lock
            size={28}
            strokeWidth={1.75}
            aria-hidden
            className="mx-auto text-neutral-500"
          />
          <h1 className="mt-2 text-base font-bold text-neutral-900">
            Halaman khusus admin
          </h1>
          <p className="mt-1 text-sm text-neutral-500">
            Akun kamu tidak punya akses ke panel admin SeTrip.
          </p>
          <Link
            href="/"
            className="mt-4 inline-block rounded-xl bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700"
          >
            Kembali ke beranda
          </Link>
        </div>
      </div>
    );
  }

  return (
    <div className="flex min-h-screen flex-col bg-neutral-50 lg:flex-row">
      <AdminSidebar
        user={{ name: session.user.name, email: session.user.email }}
      />
      <main className="flex-1 min-w-0">{children}</main>
    </div>
  );
}
