import Link from 'next/link'
import { LayoutDashboard, Package, Settings, Users, LayoutGrid, ShoppingCart, Wrench, Database, Building2, Sparkles } from 'lucide-react'
import { redirect } from 'next/navigation'
import { createClient } from '@/lib/supabase/server'
import { AdminLogoutButton } from '@/components/admin/logout-menu-item'
import { DemoWrapper } from '@/components/DemoWrapper'
import { AdminNavLink } from '@/components/admin/admin-nav-link'

export default async function AdminLayout({
  children,
}: {
  children: React.ReactNode
}) {
  const supabase = await createClient()
  const { data: { user } } = await supabase.auth.getUser()
  if (!user) redirect('/auth/login')

  const { data: userData } = await supabase
    .from('users')
    .select('role')
    .eq('id', user.id)
    .single()

  const userRole = userData?.role
  if (!userData || (userRole !== 'admin' && userRole !== 'verwaltung')) {
    redirect('/')
  }

  const isVerwaltung = userRole === 'verwaltung'

  return (
    <div className="flex min-h-screen bg-slate-50 dark:bg-[#020617] text-slate-900 dark:text-white">
      {/* Sidebar */}
      <aside className="w-64 border-r border-slate-200 dark:border-white/5 bg-white dark:bg-black/20 backdrop-blur-xl flex flex-col">
        <div className="p-6">
          <Link href="/" className="flex items-center gap-2">
            <div className="w-8 h-8 bg-primary rounded-lg flex items-center justify-center">
              <Package className="h-5 w-5 text-white" />
            </div>
            <span className="font-bold text-xl tracking-tighter">Admin Panel</span>
          </Link>
        </div>

        <nav className="flex-1 px-4 space-y-2 py-4">
          <AdminNavLink href="/admin">
            <LayoutDashboard className="w-5 h-5" />
            Dashboard
          </AdminNavLink>
          <AdminNavLink href="/admin/products">
            <Package className="w-5 h-5" />
            Produkte
          </AdminNavLink>
          <AdminNavLink href="/admin/categories">
            <LayoutGrid className="w-5 h-5" />
            Kategorien
          </AdminNavLink>
          <AdminNavLink href="/admin/wysiwyg">
            <Sparkles className="w-5 h-5" />
            Order-Wizard
          </AdminNavLink>
          <AdminNavLink href="/admin/orders">
            <ShoppingCart className="w-5 h-5" />
            Bestellungen
          </AdminNavLink>
          <AdminNavLink href="/admin/users">
            <Users className="w-5 h-5" />
            Benutzer
          </AdminNavLink>

          <AdminNavLink href="/admin/companies">
            <Building2 className="w-5 h-5" />
            Firmen
          </AdminNavLink>

          {!isVerwaltung && (
            <>
              <div className="pt-2 pb-1 px-3">
                <span className="text-[10px] font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-600">Einstellungen</span>
              </div>
              <AdminNavLink href="/admin/einstellungen">
                <Settings className="w-5 h-5" />
                Allgemein
              </AdminNavLink>
              <AdminNavLink href="/admin/settings">
                <Wrench className="w-5 h-5" />
                SMTP
              </AdminNavLink>
              <AdminNavLink href="/admin/tools">
                <Database className="w-5 h-5" />
                DB-Tools
              </AdminNavLink>
            </>
          )}
        </nav>


        <div className="p-4 border-t border-slate-200 dark:border-white/5 space-y-4">
          <AdminLogoutButton />
          <div className="px-3 pt-2 flex items-center gap-2 border-t border-slate-200 dark:border-white/5 opacity-60">
            <span className="font-bold text-sm tracking-tighter text-slate-800 dark:text-white">CASPOS Store</span>
            <DemoWrapper>
              <span className="text-[10px] bg-amber-500/20 text-amber-600 dark:text-amber-400 px-2 py-0.5 rounded-full font-semibold uppercase tracking-wider border border-amber-500/30">
                Demo
              </span>
            </DemoWrapper>
          </div>
        </div>
      </aside>

      {/* Main Content */}
      <main className="flex-1 overflow-auto">
        <div className="container mx-auto max-w-7xl">
          {children}
        </div>
      </main>
    </div>
  )
}
