import { notFound, redirect } from "next/navigation";
import { getSession } from "@/lib/auth";
import { prisma } from "@/lib/db";
import { ProductForm } from "../ProductForm";

export const dynamic = "force-dynamic";
interface Props { params: Promise<{ id: string }> }

export default async function EditProductPage({ params }: Props) {
  const session = await getSession();
  if (!session.isLoggedIn) redirect("/admin/login");
  const { id } = await params;
  const product = await prisma.product.findUnique({ where: { id } });
  if (!product) notFound();
  return (
    <div>
      <div className="mb-6">
        <a href="/admin/products" className="text-sm text-text-secondary hover:text-accent">← برگشت به محصولات</a>
        <h1 className="mt-2 text-2xl font-bold text-text-primary">ویرایش محصول</h1>
      </div>
      <ProductForm initialData={{
        id: product.id, title: product.title, slug: product.slug,
        locale: product.locale, description: product.description ?? "",
        price: product.price !== null ? String(product.price) : "",
        currency: product.currency, coverImage: product.coverImage ?? "",
        purchaseUrl: product.purchaseUrl ?? "", productType: product.productType,
        featured: product.featured, sortOrder: product.sortOrder,
        seoTitle: product.seoTitle ?? "", seoDesc: product.seoDesc ?? "",
      }} />
    </div>
  );
}
