import { getAuthenticatedUser } from "@/auth";
import { getUserEffectiveSettings } from "@/database/settings";
import UserPrivacy from "@/components/account/UserPrivacy";

export default async function PrivacyPage() {
  const user = await getAuthenticatedUser();

  let initialSettings: Record<string, unknown> = {};

  if (user?.gscUserId) {
    try {
      const effective = await getUserEffectiveSettings(
        user.gscUserId,
        user.tenantId,
        user.gscCustomerId,
        ["user.privacy", "user.security"]
      );

      for (const cat of Object.values(effective)) {
        for (const [key, setting] of Object.entries(cat)) {
          initialSettings[key] = setting.value;
        }
      }
    } catch (err) {
      console.warn("[privacy page] Failed to load settings:", err);
    }
  }

  return <UserPrivacy initialSettings={initialSettings} />;
}
