import { ReactNode } from "react"
import { redirect } from "next/navigation"

import { requireAuthenticatedSession } from "@/lib/auth-server"
import { isAgentOrAdmin, isPortalUser, isStaff } from "@/lib/authz"

export const dynamic = "force-dynamic"
export const runtime = "nodejs"

export default async function AutomationsLayout({ children }: { children: ReactNode }) {
  const session = await requireAuthenticatedSession()
  const role = session.user.role ?? "agent"

  if (!isAgentOrAdmin(role)) {
    if (isPortalUser(role)) {
      redirect("/portal")
    }
    if (isStaff(role)) {
      redirect("/dashboard")
    }
    redirect("/login")
  }

  return <>{children}</>
}

