import { getServerSession } from "next-auth";
import { AuthOptions } from "@/app/api/auth/[...nextauth]/authOptions";
import { SettingsSidebarLink } from "./SettingsSidebarLink";
import { Settings2, Users, Building2, ScrollText, Tag } from "lucide-react";

export default async function SettingsLayout({
  children,
  params,
}: {
  children: React.ReactNode;
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  const session = await getServerSession(AuthOptions);
  const isDomnaUser = !!session?.user?.email?.endsWith("@domna.homes");

  return (
    <div className="flex max-w-8xl mx-auto mt-6 px-4 gap-8 mb-8">
      <aside className="w-56 shrink-0">
        <p className="px-3 mb-1 text-xs font-semibold text-gray-400 uppercase tracking-widest">
          Settings
        </p>
        <nav className="space-y-0.5">
          <SettingsSidebarLink
            href={`/portfolio/${slug}/settings/general`}
            icon={<Settings2 size={16} />}
          >
            General
          </SettingsSidebarLink>
          <SettingsSidebarLink
            href={`/portfolio/${slug}/settings/user-access`}
            icon={<Users size={16} />}
          >
            User Access
          </SettingsSidebarLink>
          <SettingsSidebarLink
            href={`/portfolio/${slug}/settings/tags`}
            icon={<Tag size={16} />}
          >
            Tags
          </SettingsSidebarLink>
          {isDomnaUser && (
            <SettingsSidebarLink
              href={`/portfolio/${slug}/settings/connected-organisation`}
              icon={<Building2 size={16} />}
            >
              Connected Organisation
            </SettingsSidebarLink>
          )}
          {isDomnaUser && (
            <SettingsSidebarLink
              href={`/portfolio/${slug}/settings/logs`}
              icon={<ScrollText size={16} />}
            >
              Logs
            </SettingsSidebarLink>
          )}
        </nav>
      </aside>

      <div className="flex-1 min-w-0">{children}</div>
    </div>
  );
}
