import { redirect } from "next/navigation";
import { getPortalSession } from "@/lib/get-session";
import { hasOrgRole } from "@/lib/session";
import { loadTenantForShell } from "@/lib/portal-data";
import { NotAllowed } from "@/components/portal/NotAllowed";
import { WorkflowEditor } from "@/components/portal/workflows/WorkflowEditor";

export default async function WorkflowsPage({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  const session = await getPortalSession();

  if (!session) redirect(`/${slug}/dashboard`);
  if (!hasOrgRole(session, "IT_ADMIN")) {
    return <NotAllowed need="IT_ADMIN" />;
  }

  const t = await loadTenantForShell(slug);
  if (!t) return null;

  return <WorkflowEditor frozen={t.status === "frozen"} />;
}
