import { redirect } from "next/navigation";

import { getCurrentAuthContext } from "@/modules/core/server/auth";
import { hasPermission } from "@/modules/core/server/permissions";
import { getSystemSettings } from "@/modules/core/server/system-settings";
import { GlobalSettingsClient } from "@/modules/settings/components/GlobalSettingsClient";
import { getWorkspaceHref } from "@/modules/shared/lib/workspace-routing";

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

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

  const settings = await getSystemSettings();
  const organizationSettingsHref = getWorkspaceHref(context.organization?.slug, "/settings/organizations");

  return (
    <div className="flex flex-col gap-5">
      <GlobalSettingsClient organizationSettingsHref={organizationSettingsHref} settings={settings} />
    </div>
  );
}
