"use client"

import { useState, useEffect } from 'react'
import { createClient } from '@/lib/supabase/client'
import { Button } from '@/components/ui/button'
import Link from 'next/link'
import { motion } from 'framer-motion'
import { type User } from '@supabase/supabase-js'
import {
  ArrowRight,
  CheckCircle2,
  MessageSquare,
  BookOpen,
  Layers,
  LifeBuoy,
  Users,
  ClipboardList,
  Zap,
  ArrowUpRight,
  ShieldCheck,
  Sparkles,
  Activity
} from 'lucide-react'

interface HomeClientProps {
  initialUser: User | null
}

export function HomeClient({ initialUser }: HomeClientProps) {
  const [user, setUser] = useState<User | null>(initialUser)
  const [mounted, setMounted] = useState(false)

  useEffect(() => {
    setMounted(true)
    const supabase = createClient()

    // Listen to client-side auth state changes to stay in sync
    const { data: { subscription } } = supabase.auth.onAuthStateChange((event, session) => {
      setUser(session?.user ?? null)
    })

    return () => {
      subscription.unsubscribe()
    }
  }, [])

  const containerVariants = {
    hidden: { opacity: 0 },
    visible: {
      opacity: 1,
      transition: {
        staggerChildren: 0.15
      }
    }
  }

  const itemVariants = {
    hidden: { y: 20, opacity: 0 },
    visible: {
      y: 0,
      opacity: 1,
      transition: {
        type: "spring" as const,
        stiffness: 100,
        damping: 15
      }
    }
  }

  return (
    <div className="min-h-screen bg-slate-950 text-slate-100 overflow-hidden relative selection:bg-blue-500/30 selection:text-blue-200">



      {/* Hero Section */}
      <section className="w-full pt-16 pb-12 md:pt-28 md:pb-20 px-4 relative flex flex-col items-center">
        <motion.div
          initial="hidden"
          animate="visible"
          variants={containerVariants}
          className="container max-w-6xl mx-auto flex flex-col items-center text-center space-y-8"
        >

          {/* Headline */}
          <div className="space-y-4 max-w-4xl">
            <motion.h1
              variants={itemVariants}
              className="text-4xl font-extrabold tracking-tight sm:text-5xl md:text-6xl lg:text-7xl/tight text-white"
            >
              CASPOS Software <span className="text-blue-500 font-black">Lizenzen & Module</span>
            </motion.h1>
            <motion.p
              variants={itemVariants}
              className="mx-auto max-w-[750px] text-slate-400 text-base sm:text-lg md:text-xl font-normal leading-relaxed"
            >
              Erwerben und verwalten Sie Lizenzen für CASPOS Kassensysteme.
            </motion.p>
          </div>

          {/* Hero CTAs */}
          <motion.div
            variants={itemVariants}
            className="flex flex-col sm:flex-row items-center justify-center gap-4 w-full max-w-md pt-4"
          >
            <Link href="/order" className="w-full sm:w-auto">
              <Button size="lg" className="w-full sm:w-auto h-12 px-8 text-base font-semibold bg-blue-600 hover:bg-blue-500 text-white border border-blue-700/50 rounded-xl transition-all duration-200">
                Lizenz anfragen <ArrowRight className="ml-2 h-4 w-4" />
              </Button>
            </Link>
            <Link href="https://caspos.de/kontakt/" target="_blank" rel="noopener noreferrer" className="w-full sm:w-auto">
              <Button size="lg" variant="outline" className="w-full sm:w-auto h-12 px-8 text-base font-semibold transition-all duration-300 hover:bg-white/5 border-slate-800 hover:border-slate-700 text-slate-300 hover:text-white rounded-xl backdrop-blur-sm">
                Fragen? Kontakt
              </Button>
            </Link>
          </motion.div>

          {/* Quick Access Actions Bar */}
          {mounted && user && (
            <motion.div
              variants={itemVariants}
              className="w-full max-w-3xl pt-12"
            >
              <div className="relative p-6 rounded-2xl border border-slate-800 bg-slate-900/40 backdrop-blur-md shadow-lg">
                <p className="text-xs font-semibold uppercase tracking-widest text-slate-500 mb-5 text-left sm:text-center px-2">
                  Mein Dashboard Schnellzugriff
                </p>

                <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
                  <Link href="/my-customers" className="group">
                    <motion.div
                      whileHover={{ y: -2 }}
                      whileTap={{ scale: 0.98 }}
                      className="flex items-center justify-between p-4 rounded-xl border border-slate-800 bg-slate-950 hover:bg-slate-900 hover:border-slate-700 transition-all duration-200"
                    >
                      <div className="flex items-center gap-4">
                        <div className="p-2.5 rounded-lg bg-blue-500/10 text-blue-400 group-hover:bg-blue-500/20 group-hover:text-blue-300 transition-colors">
                          <Users className="w-5.5 h-5.5" />
                        </div>
                        <div className="text-left">
                          <span className="block font-bold text-sm text-slate-200 group-hover:text-white transition-colors">Kundenliste</span>
                          <span className="block text-xs text-slate-500 group-hover:text-slate-400 transition-colors">Kunden & Lizenzen verwalten</span>
                        </div>
                      </div>
                      <ArrowUpRight className="w-4.5 h-4.5 text-slate-600 group-hover:text-blue-400 group-hover:translate-x-0.5 group-hover:-translate-y-0.5 transition-all" />
                    </motion.div>
                  </Link>

                  <Link href="/my-orders" className="group">
                    <motion.div
                      whileHover={{ y: -2 }}
                      whileTap={{ scale: 0.98 }}
                      className="flex items-center justify-between p-4 rounded-xl border border-slate-800 bg-slate-950 hover:bg-slate-900 hover:border-slate-700 transition-all duration-200"
                    >
                      <div className="flex items-center gap-4">
                        <div className="p-2.5 rounded-lg bg-blue-500/10 text-blue-400 group-hover:bg-blue-500/20 group-hover:text-blue-300 transition-colors">
                          <ClipboardList className="w-5.5 h-5.5" />
                        </div>
                        <div className="text-left">
                          <span className="block font-bold text-sm text-slate-200 group-hover:text-white transition-colors">Bestellungen</span>
                          <span className="block text-xs text-slate-500 group-hover:text-slate-400 transition-colors">Aufträge & Status einsehen</span>
                        </div>
                      </div>
                      <ArrowUpRight className="w-4.5 h-4.5 text-slate-600 group-hover:text-blue-400 group-hover:translate-x-0.5 group-hover:-translate-y-0.5 transition-all" />
                    </motion.div>
                  </Link>
                </div>
              </div>
            </motion.div>
          )}

        </motion.div>
      </section>

      {/* Grid of modules */}
      <section className="w-full py-16 md:py-24 border-t border-slate-900 bg-slate-950/50 relative">
        <div className="container max-w-6xl mx-auto px-4 relative">

          <div className="absolute top-1/4 right-0 w-[400px] h-[400px] bg-indigo-600/5 blur-[120px] rounded-full -z-10 pointer-events-none" />

          <div className="text-center space-y-3 mb-16">
            <h2 className="text-3xl sm:text-4xl font-bold tracking-tight text-white">
              Workspace-Zentrale
            </h2>
            <p className="text-slate-400 max-w-[600px] mx-auto text-sm sm:text-base">
              Die zentralen Plattformen für unsere tägliche Zusammenarbeit, Dokumentation und den Support.
            </p>
          </div>

          <div className="grid gap-6 sm:grid-cols-2 lg:grid-cols-3">
            {/* Card 1 */}
            <a href="https://support.teamzone.softengine.de" target="_blank" rel="noopener noreferrer"><motion.div
              whileHover={{ y: -4 }}
              className="group relative flex flex-col p-6 rounded-2xl border border-slate-800 bg-slate-900/40 hover:bg-slate-900/80 hover:border-slate-700 transition-all duration-200 shadow-md overflow-hidden"
            >
              <div className="space-y-4 relative">
                <div className="inline-flex p-3 bg-blue-500/10 rounded-xl text-blue-400 group-hover:bg-blue-500/20 group-hover:scale-110 transition-all duration-300">
                  <MessageSquare className="h-6 w-6" />
                </div>
                <h3 className="text-xl font-bold text-slate-200 group-hover:text-white transition-colors">Zulip</h3>
                <p className="text-slate-400 text-sm leading-relaxed">
                  Treten Sie unserem Partner-Chat bei! tauschen Sie sich mit anderen CASPOS Partnern aus.
                </p>
              </div>
            </motion.div></a>

            {/* Card 2 */}
            <a href="https://wiki.caspos.de" target="_blank" rel="noopener noreferrer"><motion.div
              whileHover={{ y: -4 }}
              className="group relative flex flex-col p-6 rounded-2xl border border-slate-800 bg-slate-900/40 hover:bg-slate-900/80 hover:border-slate-700 transition-all duration-200 shadow-md overflow-hidden"
            >
              <div className="space-y-4 relative">
                <div className="inline-flex p-3 bg-indigo-500/10 rounded-xl text-indigo-400 group-hover:bg-indigo-500/20 group-hover:scale-110 transition-all duration-300">
                  <BookOpen className="h-6 w-6" />
                </div>
                <h3 className="text-xl font-bold text-slate-200 group-hover:text-white transition-colors">Wiki</h3>
                <p className="text-slate-400 text-sm leading-relaxed">
                  Ihr Wissens-Portal für die CASPOS Produktfamilie.
                </p>
              </div>
            </motion.div></a>

            {/* Card 3 */}
            <a href="https://support.caspos.de" target="_blank" rel="noopener noreferrer"><motion.div
              whileHover={{ y: -4 }}
              className="group relative flex flex-col p-6 rounded-2xl border border-slate-800 bg-slate-900/40 hover:bg-slate-900/80 hover:border-slate-700 transition-all duration-200 shadow-md overflow-hidden"
            >
              <div className="space-y-4 relative">
                <div className="inline-flex p-3 bg-purple-500/10 rounded-xl text-purple-400 group-hover:bg-purple-500/20 group-hover:scale-110 transition-all duration-300">
                  <LifeBuoy className="h-6 w-6" />
                </div>
                <h3 className="text-xl font-bold text-slate-200 group-hover:text-white transition-colors">Support</h3>
                <p className="text-slate-400 text-sm leading-relaxed">
                  Sie haben ein technisches Problem in der CASPOS? Erstellen Sie jetzt ein Ticket!
                </p>
              </div>
            </motion.div></a>
          </div>
        </div>
      </section>

      {/* Stepper info */}
      <section className="w-full py-16 md:py-24 border-t border-slate-900 bg-slate-950/30">
        <div className="container max-w-5xl mx-auto px-4">
          <div className="text-center space-y-3 mb-16">
            <h2 className="text-2xl sm:text-3xl font-bold tracking-tight text-center text-white">
              Wie funktioniert die Bereitstellung?
            </h2>
          </div>

          <div className="relative">
            {/* Connection line for Desktop */}
            <div className="hidden md:block absolute top-[28px] left-[15%] right-[15%] h-[1px] border-t border-dashed border-slate-800 z-0" />

            <div className="grid gap-8 md:grid-cols-3 relative z-10">
              {/* Step 1 */}
              <div className="flex flex-col items-center text-center space-y-4 p-4 group">
                <div className="flex items-center justify-center w-14 h-14 rounded-2xl bg-slate-900 border border-slate-800 text-blue-400 font-black text-lg shadow-lg group-hover:border-blue-500/40 group-hover:shadow-[0_0_15px_rgba(59,130,246,0.15)] transition-all duration-300">
                  1
                </div>
                <h4 className="text-lg font-bold text-slate-200 group-hover:text-white transition-colors">Kunde wählen</h4>
                <p className="text-sm text-slate-400 leading-relaxed max-w-[250px]">
                  Gewünschten Kunden auswählen, der die Lizenz erhalten soll.
                </p>
              </div>

              {/* Step 2 */}
              <div className="flex flex-col items-center text-center space-y-4 p-4 group">
                <div className="flex items-center justify-center w-14 h-14 rounded-2xl bg-slate-900 border border-slate-800 text-blue-400 font-black text-lg shadow-lg group-hover:border-blue-500/40 group-hover:shadow-[0_0_15px_rgba(59,130,246,0.15)] transition-all duration-300">
                  2
                </div>
                <h4 className="text-lg font-bold text-slate-200 group-hover:text-white transition-colors">Lizenz wählen</h4>
                <p className="text-sm text-slate-400 leading-relaxed max-w-[250px]">
                  Gewünschte Lizenzen für den Kunden auswählen und den Bestellung abschließen.
                </p>
              </div>

              {/* Step 3 */}
              <div className="flex flex-col items-center text-center space-y-4 p-4 group">
                <div className="flex items-center justify-center w-14 h-14 rounded-2xl bg-slate-900 border border-slate-800 text-blue-400 font-black text-lg shadow-lg group-hover:border-blue-500/40 group-hover:shadow-[0_0_15px_rgba(59,130,246,0.15)] transition-all duration-300">
                  3
                </div>
                <h4 className="text-lg font-bold text-slate-200 group-hover:text-white transition-colors">Live gehen</h4>
                <p className="text-sm text-slate-400 leading-relaxed max-w-[250px]">
                  Nach dem Erhalt der Lizenz Live gehen.
                </p>
              </div>
            </div>
          </div>
        </div>
      </section>


      {/* Footer */}
      <footer className="w-full border-t border-slate-900 bg-slate-950 py-8 px-4 md:px-6 relative z-10">
        <div className="container max-w-6xl mx-auto flex flex-col sm:flex-row items-center justify-between gap-4">
          <p className="text-xs text-slate-500">© 2026 CASPOS GmbH. Alle Rechte vorbehalten.</p>
          <nav className="flex gap-6">
            <Link className="text-xs text-slate-500 hover:text-slate-300 transition-colors" href="/impressum">Impressum</Link>
            <Link className="text-xs text-slate-500 hover:text-slate-300 transition-colors" href="/datenschutz">Datenschutz</Link>
          </nav>
        </div>
      </footer>
    </div>
  )
}
