import prisma from '@/lib/prisma'
import ProductForm from '../../ProductForm'
import { notFound } from 'next/navigation'

export const metadata = {
  title: 'Ürün Düzenle - Admin',
}

export default async function EditProductPage({ params }: { params: Promise<{ id: string }> }) {
  const resolvedParams = await params
  const id = parseInt(resolvedParams.id, 10)

  if (isNaN(id)) {
    notFound()
  }

  const [categories, product] = await Promise.all([
    prisma.categories.findMany({
      orderBy: { order_num: 'asc' },
      select: { id: true, name_tr: true }
    }),
    prisma.products.findUnique({
      where: { id }
    })
  ])

  if (!product) {
    notFound()
  }

  return (
    <div className="space-y-6">
      <h1 className="text-2xl font-bold text-gray-900">Ürün Düzenle</h1>
      
      <div className="bg-white p-6 rounded-xl shadow-sm border border-gray-100 max-w-3xl">
        <ProductForm 
          categories={categories} 
          product={{
            ...product,
            price: product.price.toString() // Convert Decimal to string
          }} 
        />
      </div>
    </div>
  )
}
