import { notFound } from "next/navigation";
import { isLocale, type Locale } from "@/lib/i18n/config";
import { getDictionary } from "@/lib/i18n";
import { requireUser } from "@/lib/auth-helpers";
import { prisma } from "@/lib/prisma";
import { buildMetadata } from "@/lib/seo";
import { ProfileForm } from "@/components/store/profile-form";

export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }) {
  const { locale: raw } = await params;
  if (!isLocale(raw)) return {};
  const locale = raw as Locale;
  const dict = getDictionary(locale);
  return buildMetadata({
    locale,
    title: `${dict.account.profile} — DAR ZELLIGE`,
    description: dict.account.profileDesc,
    path: "/account/profile",
    noIndex: true,
  });
}

export default async function AccountProfilePage({ params }: { params: Promise<{ locale: string }> }) {
  const { locale: raw } = await params;
  if (!isLocale(raw)) notFound();
  const locale = raw as Locale;
  const dict = getDictionary(locale);
  const sessionUser = await requireUser(locale, `/${locale}/account/profile`);
  const user = await prisma.user.findUnique({ where: { id: sessionUser.id } });
  if (!user) notFound();

  return (
    <ProfileForm
      locale={locale}
      user={{ name: user.name, email: user.email, phone: user.phone ?? "" }}
      labels={{
        title: dict.account.profileTitle,
        desc: dict.account.profileDesc,
        name: dict.account.name,
        email: dict.account.email,
        phone: dict.account.phone,
        save: dict.common.save,
        updated: dict.account.profileUpdated,
      }}
    />
  );
}
