import { redirect } from "next/navigation";
import { requirePortalRole } from "@/lib/auth/profile";
import { MasterShell } from "@/components/layout/MasterShell";

export default async function MasterLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const gate = await requirePortalRole("league_master");
  if (!gate.ok) {
    redirect(gate.reason === "auth" ? "/login/master" : "/login/manager");
  }

  const user = {
    name: gate.profile?.display_name ?? gate.user.email?.split("@")[0] ?? "Master",
    email: gate.user.email ?? "",
  };

  return <MasterShell user={user}>{children}</MasterShell>;
}
