import { notFound } from "next/navigation"
import { ProductDetail } from "@/components/product-detail"
import { SAMPLE_PRODUCTS } from "@/lib/sample-products"
import { cookies } from "next/headers"

async function getProduct(id: string, isDraft: boolean) {
  const product = SAMPLE_PRODUCTS.find((p) => p.id === Number.parseInt(id))

  if (!product) {
    return null
  }

  if (isDraft) {
    return {
      ...product,
      title: `[Draft] ${product.title}`,
      price: product.price * 0.9,
    }
  }

  return product
}

interface ProductPageProps {
  params: { id: string }
}

export default async function ProductPage({ params }: ProductPageProps) {
  // Get cookies first
  const cookieStore = await cookies()
  const isDraftMode = cookieStore.get("__prerender_bypass")?.value === process.env.PRERENDER_BYPASS_TOKEN

  // Then get product
  const product = await getProduct(params.id, isDraftMode)

  if (!product) {
    notFound()
  }

  return (
    <div className="container mx-auto px-4 py-8">
      <ProductDetail product={product} />
 
    </div>
  )
}

