import { redirect } from "next/navigation";
import { getCurrentUser } from "@/lib/auth";
import SettingsForm from "@/components/settings/SettingsForm";
import ChangePasswordForm from "@/components/settings/ChangePasswordForm";
import SessionsList from "@/components/settings/SessionsList";
import AIIntegration from "@/components/settings/AIIntegration";
import ExportMyData from "@/components/settings/ExportMyData";
import DangerZone from "@/components/settings/DangerZone";
import AdminInstanceSettings from "@/components/settings/AdminInstanceSettings";
import { getInstanceSettings } from "@/lib/instanceSettings";

export default async function SettingsPage() {
  const user = await getCurrentUser();

  if (!user) {
    redirect("/auth/login");
  }

  const instanceSettings = user.isAdmin ? await getInstanceSettings() : null;

  return (
    <div className="min-h-screen bg-[#0A0A0A]">
      <div className="border-b border-zinc-800 px-4 py-4 sm:px-6">
        <div className="max-w-2xl mx-auto">
          <h1 className="text-2xl font-bold text-white">Settings</h1>
          <p className="text-zinc-500 text-sm mt-1">
            Manage your preferences and account
          </p>
        </div>
      </div>

      <div className="max-w-2xl mx-auto px-4 py-6 sm:px-6 space-y-8">
        <div id="general"><SettingsForm user={user} /></div>
        <div id="password"><ChangePasswordForm /></div>
        <div id="sessions"><SessionsList /></div>
        <div id="ai"><AIIntegration /></div>
        <div id="data"><ExportMyData /></div>
        {user.isAdmin && instanceSettings && (
          <div id="instance">
            <AdminInstanceSettings
              initialSignupsOpen={instanceSettings.signupsOpen}
            />
          </div>
        )}
        <div id="danger"><DangerZone /></div>
      </div>
    </div>
  );
}
