import Link from "next/link";
import { getCurrentUser } from "@/lib/auth";
import LogoutButton from "@/components/LogoutButton";

/**
 * The /op layout intentionally does NOT force-redirect unauthenticated
 * visitors. The scan page (/op/scan/[token]) needs to bounce them to
 * /login/operator?next=<path> so they come back to the same QR card after
 * signing in; a blanket redirect here would lose that context. Each page
 * under /op is responsible for its own auth gate (see requireOperator in
 * lib/auth.ts, or the scan page's custom redirect).
 */
export default async function OperatorLayout({ children }: { children: React.ReactNode }) {
  const user = await getCurrentUser();

  return (
    <div className="min-h-dvh flex flex-col">
      <header className="border-b border-slate-200 bg-white">
        <div className="mx-auto max-w-3xl px-4 py-3 flex items-center gap-3">
          <Link href="/op" className="font-semibold tracking-tight">
            MRP
          </Link>
          {user ? (
            <>
              <span className="ml-auto text-sm text-slate-500">{user.name}</span>
              <LogoutButton />
            </>
          ) : (
            <Link href="/login/operator" className="ml-auto text-sm text-slate-900 hover:underline">
              Sign in
            </Link>
          )}
        </div>
      </header>
      <main className="flex-1">{children}</main>
    </div>
  );
}
