import type { Metadata } from "next";
import { headers } from "next/headers";
import { auth } from "@/lib/auth/server";
import { db, accounts, users, eq, and } from "@epicure/db";
import { SecurityForm } from "@/components/settings/security-form";

export const metadata: Metadata = {};

export default async function SecurityPage() {
  const session = await auth.api.getSession({ headers: await headers() });
  if (!session) return null;

  const [dbUser, credentialAccount] = await Promise.all([
    db.query.users.findFirst({ where: eq(users.id, session.user.id), columns: { twoFactorEnabled: true } }),
    db.query.accounts.findFirst({ where: and(eq(accounts.userId, session.user.id), eq(accounts.providerId, "credential")) }),
  ]);

  return (
    <SecurityForm
      currentEmail={session.user.email}
      twoFactorEnabled={dbUser?.twoFactorEnabled ?? false}
      hasPassword={!!credentialAccount}
    />
  );
}
