import type { ReactNode } from "react";
import Link from "next/link";
import { requireAdmin } from "@/lib/auth";
import { logoutAction } from "../actions";

// Guarded chrome for every authenticated admin page. Login lives outside this
// group so it never inherits the nav or the auth gate.
export default async function PanelLayout({
  children,
}: {
  children: ReactNode;
}) {
  await requireAdmin();

  return (
    <div className="rb-admin-shell">
      <header className="rb-admin-bar">
        <Link href="/admin" className="rb-admin-brand">
          RetroBlog Admin
        </Link>
        <nav className="rb-admin-nav">
          <Link href="/admin">Dashboard</Link>
          <Link href="/admin/posts">Posts</Link>
          <Link href="/admin/settings">Settings</Link>
          <Link href="/" target="_blank">
            View site ↗
          </Link>
        </nav>
        <form action={logoutAction} className="rb-admin-logout">
          <button className="rb-btn rb-btn-ghost" type="submit">
            Sign out
          </button>
        </form>
      </header>
      <main className="rb-admin-main">{children}</main>
    </div>
  );
}
