import { redirect } from "next/navigation";

import { getCurrentAuthContext } from "@/modules/core/server/auth";
import { hasPermission } from "@/modules/core/server/permissions";
import { listHealthAdminData } from "@/modules/health/server/operations";
import { HealthAdminClient } from "@/modules/health/components/HealthAdminClient";
import { getWorkspaceHref } from "@/modules/shared/lib/workspace-routing";

export default async function HealthSettingsPage(): Promise<React.ReactElement> {
  const context = await getCurrentAuthContext();
  if (!context) {
    redirect("/login");
  }

  if (!hasPermission(context.permissions, "health:read")) {
    redirect(getWorkspaceHref(context.organization?.slug, "/dashboard"));
  }

  const organizationId = context.organization?.id;
  if (!organizationId) {
    redirect(getWorkspaceHref(context.organization?.slug, "/dashboard"));
  }

  const data = await listHealthAdminData(organizationId);

  return <HealthAdminClient canManage={hasPermission(context.permissions, "health:manage")} initialData={data} />;
}
