import { notFound } from 'next/navigation';
import { prisma } from '@/lib/prisma';
import { ProductForm } from '@/components/admin/product-form';
import type { Locale } from '@/i18n/config';

export default async function EditProductPage({
  params: { locale, id }
}: {
  params: { locale: Locale; id: string };
}) {
  const [product, categories] = await Promise.all([
    prisma.product.findUnique({ where: { id }, include: { translations: true } }),
    prisma.category.findMany({ include: { translations: { where: { locale: 'fr' } } } })
  ]);

  if (!product) notFound();

  const fr = product.translations.find((t) => t.locale === 'fr');
  const ar = product.translations.find((t) => t.locale === 'ar');

  return (
    <div>
      <h1 className="font-display text-2xl font-semibold">Modifier le produit</h1>
      <div className="mt-6">
        <ProductForm
          locale={locale}
          categories={categories.map((c) => ({ id: c.id, name: c.translations[0]?.name ?? c.slug }))}
          initial={{
            id: product.id,
            price: product.price ? Number(product.price) : null,
            available: product.available,
            published: product.published,
            images: product.images,
            fr: fr
              ? { name: fr.name, shortDescription: fr.shortDescription, description: fr.description }
              : undefined,
            ar: ar
              ? { name: ar.name, shortDescription: ar.shortDescription, description: ar.description }
              : undefined
          }}
        />
      </div>
    </div>
  );
}
