import { getProducts, getCategories } from '@/lib/actions/products'

import { ProductList } from '@/components/admin/product-list'
import { CreateProductDialog } from '@/components/admin/create-product-dialog'
import { Package, Plus } from 'lucide-react'
import { Suspense } from 'react'

export default async function AdminProductsPage() {
  return (
    <div className="flex-1 space-y-8 p-8 pt-6">
      <div className="flex items-center justify-between space-y-2">
        <div>
          <h2 className="text-3xl font-bold tracking-tight text-gradient flex items-center gap-3">
            <Package className="w-8 h-8 text-primary" />
            Produkte & Module
          </h2>
          <p className="text-slate-300">
            Verwalte deine Hauptprodukte und deren zubuchbare Module.
          </p>
        </div>
        <div className="flex items-center space-x-2">
          <Suspense fallback={<div className="h-10 w-32 animate-pulse bg-white/5 rounded-md" />}>
            <CreateProductAction />
          </Suspense>
        </div>
      </div>
      <Suspense fallback={<div className="h-40 w-full animate-pulse bg-white/5 rounded-xl" />}>
        <ProductDataWrapper />
      </Suspense>
    </div>
  )
}

async function ProductDataWrapper() {
  const products = await getProducts()
  const categories = await getCategories()
  return <ProductList initialProducts={products} categories={categories} />
}

async function CreateProductAction() {
  const categories = await getCategories()
  const products = await getProducts()
  return (
    <CreateProductDialog categories={categories} products={products} />
  )
}
