import { redirect } from "next/navigation";

import { Badge } from "@/components/ui/badge";
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";

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

  if (!hasPermission(context.permissions, "platform:manage")) {
    redirect("/app/dashboard");
  }

  const settings = await getSystemSettings();

  return (
    <div className="flex flex-col gap-5">
      <section>
        <Badge variant="success">Global</Badge>
        <h2 className="mt-3 text-xl font-semibold">全局配置</h2>
        <p className="mt-1 text-sm text-muted-foreground">平台级注册策略、OIDC 默认配置和用户头像声明映射。</p>
      </section>

      <GlobalSettingsClient settings={settings} />
    </div>
  );
}
