import { createClient } from '@/lib/supabase/server'
import { getProducts } from '@/lib/actions/products'
import { OrderWizard } from '@/components/order-wizard'
import { redirect } from 'next/navigation'
import { Suspense } from 'react'

export default async function OrderPage() {
  return (
    <div className="min-h-screen bg-[#0a0a0a] text-white selection:bg-primary/30">
      <div className="container mx-auto py-10">
        <div className="text-center mb-12">
          <h1 className="text-5xl font-extrabold tracking-tight mb-4 text-gradient">
            Konfigurieren Sie Ihre Lösung
          </h1>
          <p className="text-slate-400 text-lg max-w-2xl mx-auto">
            Wählen Sie das passende Paket und die benötigten Module für Ihr Business.
          </p>
        </div>
        
        <Suspense fallback={<div className="h-96 w-full animate-pulse bg-white/5 rounded-2xl" />}>
          <OrderDataWrapper />
        </Suspense>
      </div>
    </div>
  )
}

async function OrderDataWrapper() {
  const supabase = await createClient()
  const { data: { user } } = await supabase.auth.getUser()

  if (!user) {
    redirect('/auth/login')
  }

  const products = await getProducts()
  
  // Fetch profile if exists
  const { data: profile } = await supabase
    .from('profiles')
    .select('*')
    .eq('id', user.id)
    .single()

  return <OrderWizard products={products} initialProfile={profile} />
}
