import { getProducts, getCategories } from '@/lib/actions/products'
import { WysiwygAdminClient } from './wysiwyg-client'
import { Edit, Sparkles } from 'lucide-react'
import { Suspense } from 'react'

export const dynamic = 'force-dynamic'

export default async function WysiwygAdminPage() {
  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">
            <Edit className="w-8 h-8 text-primary" />
            WYSIWYG Live-Editor
          </h2>
          <p className="text-slate-300">
            Kategorien und Produkte visuell im Wizard-Layout bearbeiten.
          </p>
        </div>
      </div>
      <Suspense fallback={<div className="h-40 w-full animate-pulse bg-white/5 rounded-xl" />}>
        <WysiwygDataWrapper />
      </Suspense>
    </div>
  )
}

import { createClient } from '@/lib/supabase/server'

async function WysiwygDataWrapper() {
  const products = await getProducts().catch(() => [])
  const categories = await getCategories().catch(() => [])
  
  const supabase = await createClient()
  const { data: allModules } = await supabase
    .from('product_modules')
    .select('*')
    .order('name', { ascending: true })

  return (
    <WysiwygAdminClient 
      initialProducts={products} 
      initialCategories={categories} 
      allModules={allModules || []} 
    />
  )
}
