import Link from "next/link"
import { redirect } from "next/navigation"
import {
  getAdminUserFromCookies,
  isAdminUiEnabled,
} from "@/lib/server/admin-auth"

export default async function AdminLayout({
  children,
}: {
  children: React.ReactNode
}) {
  if (!isAdminUiEnabled()) {
    redirect("/")
  }

  const adminUser = await getAdminUserFromCookies()
  if (!adminUser) {
    redirect("/sign-in")
  }

  return (
    <div className="min-h-screen bg-muted/30">
      <div className="border-b bg-background">
        <div className="container mx-auto flex items-center justify-between px-4 py-3 text-sm">
          <div className="flex items-center gap-3">
            <Link href="/admin" className="font-semibold hover:text-primary">
              Rocky Admin
            </Link>
            <span className="text-muted-foreground">{adminUser.email}</span>
          </div>
          <form action="/api/admin/auth/logout" method="post">
            <button className="text-muted-foreground hover:text-foreground">
              Sign out
            </button>
          </form>
        </div>
      </div>
      {children}
    </div>
  )
}
