"use client";

import { useState } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { 
  Home, 
  Search, 
  Zap, 
  User, 
  X, 
  Compass, 
  Plus, 
  Smartphone, 
  Film,
  Crown,
  Sparkles
} from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";
import { LiveSearch } from "@/components/LiveSearch";

export function MobileBottomNav({ session }: { session?: any }) {
  const pathname = usePathname();
  const [searchOpen, setSearchOpen] = useState(false);
  const [uploadMenuOpen, setUploadMenuOpen] = useState(false);

  const isShortsRoute = pathname?.startsWith("/shorts");
  const isLiveRoute = pathname?.startsWith("/live") || pathname?.startsWith("/cam") || pathname?.startsWith("/mtbrcam");
  if (isLiveRoute) return null;

  return (
    <>
      {/* 📱 BARRE DE NAVIGATION MOBILE NATIVE (iOS/Android App Dock) */}
      <nav 
        aria-label="Navigation mobile"
        className="md:hidden fixed bottom-0 left-0 right-0 z-50 bg-[#0c0608]/95 backdrop-blur-2xl border-t border-[#2e1217] px-2 py-1.5 flex items-center justify-around shadow-[0_-10px_35px_rgba(0,0,0,0.9)] select-none pb-[calc(0.375rem+env(safe-area-inset-bottom,0px))]"
      >
        
        {/* 1. Accueil */}
        <Link
          href="/"
          prefetch={true}
          className={`flex-1 flex flex-col items-center justify-center gap-1 py-1 rounded-2xl transition-all active:scale-95 ${
            pathname === "/" ? "text-[#e05a68] font-black" : "text-[#9e737a] hover:text-white"
          }`}
        >
          <div className="relative">
            <Home size={20} className={pathname === "/" ? "text-[#e05a68]" : "text-[#9e737a]"} />
            {pathname === "/" && (
              <motion.div 
                layoutId="activeMobileDot"
                className="absolute -bottom-1 left-1/2 -translate-x-1/2 w-1.5 h-1.5 rounded-full bg-[#e05a68] shadow-[0_0_8px_#e05a68]" 
              />
            )}
          </div>
          <span className="text-[10px] tracking-tight">Accueil</span>
        </Link>

        {/* 2. Shorts (TikTok Style) */}
        <Link
          href="/shorts"
          prefetch={true}
          className={`flex-1 flex flex-col items-center justify-center gap-1 py-1 rounded-2xl transition-all active:scale-95 relative ${
            isShortsRoute ? "text-[#e05a68] font-black" : "text-[#9e737a] hover:text-white"
          }`}
        >
          <div className="relative">
            <Smartphone size={20} className={isShortsRoute ? "text-[#e05a68]" : "text-[#9e737a]"} />
            <span className="absolute -top-1 -right-1.5 w-2 h-2 bg-[#e05a68] rounded-full animate-ping" />
            <span className="absolute -top-1 -right-1.5 w-2 h-2 bg-[#e05a68] rounded-full shadow-[0_0_8px_#e05a68]" />
            {isShortsRoute && (
              <motion.div 
                layoutId="activeMobileDot"
                className="absolute -bottom-1 left-1/2 -translate-x-1/2 w-1.5 h-1.5 rounded-full bg-[#e05a68] shadow-[0_0_8px_#e05a68]" 
              />
            )}
          </div>
          <span className="text-[10px] tracking-tight flex items-center gap-0.5">
            Shorts
          </span>
        </Link>

        {/* 3. Bouton Central : Action Rapide / Publier (+) */}
        <button
          onClick={() => setUploadMenuOpen(true)}
          className="flex-1 flex flex-col items-center justify-center -mt-4 active:scale-90 transition-transform"
          aria-label="Publier un contenu"
        >
          <div className="w-12 h-12 rounded-full bg-gradient-to-tr from-[#5c1922] via-[#8c2432] to-[#e05a68] flex items-center justify-center shadow-[0_0_20px_rgba(224,90,104,0.6)] border-2 border-white/20">
            <Plus size={22} className="text-white font-black" />
          </div>
          <span className="text-[9.5px] font-black text-white mt-1">Créer</span>
        </button>

        {/* 4. Explorer / Vidéos */}
        <Link
          href="/videos"
          prefetch={true}
          className={`flex-1 flex flex-col items-center justify-center gap-1 py-1 rounded-2xl transition-all active:scale-95 ${
            pathname?.startsWith("/videos") || pathname?.startsWith("/categories") 
              ? "text-[#e05a68] font-black" 
              : "text-[#9e737a] hover:text-white"
          }`}
        >
          <div className="relative">
            <Compass size={20} className={pathname?.startsWith("/videos") || pathname?.startsWith("/categories") ? "text-[#e05a68]" : "text-[#9e737a]"} />
            {(pathname?.startsWith("/videos") || pathname?.startsWith("/categories")) && (
              <motion.div 
                layoutId="activeMobileDot"
                className="absolute -bottom-1 left-1/2 -translate-x-1/2 w-1.5 h-1.5 rounded-full bg-[#e05a68] shadow-[0_0_8px_#e05a68]" 
              />
            )}
          </div>
          <span className="text-[10px] tracking-tight">Explorer</span>
        </Link>

        {/* 5. Profil / Compte */}
        <Link
          href={session?.user ? "/profile" : "/login"}
          prefetch={true}
          className={`flex-1 flex flex-col items-center justify-center gap-1 py-1 rounded-2xl transition-all active:scale-95 ${
            pathname === "/profile" || pathname === "/login" 
              ? "text-[#e05a68] font-black" 
              : "text-[#9e737a] hover:text-white"
          }`}
        >
          <div className="relative w-6 h-6 rounded-full bg-[#180a0f] border border-[#3e1620] flex items-center justify-center overflow-hidden">
            {session?.user?.image ? (
              <img src={session.user.image} alt="Avatar" className="w-full h-full object-cover" />
            ) : (
              <User size={13} className={pathname === "/profile" ? "text-[#e05a68]" : "text-white"} />
            )}
            {session?.user?.isPremium && (
              <span className="absolute -top-1 -right-1 bg-amber-500 w-2 h-2 rounded-full" />
            )}
          </div>
          <span className="text-[10px] tracking-tight">{session?.user ? "Profil" : "Compte"}</span>
        </Link>

      </nav>

      {/* 🚀 MODAL BOTTOM-SHEET RAPIDE POUR CRÉER/PUBLIER */}
      <AnimatePresence>
        {uploadMenuOpen && (
          <>
            <motion.div
              initial={{ opacity: 0 }}
              animate={{ opacity: 1 }}
              exit={{ opacity: 0 }}
              onClick={() => setUploadMenuOpen(false)}
              className="fixed inset-0 bg-black/80 backdrop-blur-md z-[100] md:hidden"
            />
            <motion.div
              initial={{ y: "100%" }}
              animate={{ y: 0 }}
              exit={{ y: "100%" }}
              transition={{ type: "spring", stiffness: 350, damping: 30 }}
              className="fixed bottom-0 left-0 right-0 bg-[#12080b] border-t border-[#2e1217] rounded-t-3xl p-6 z-[101] md:hidden space-y-4 pb-[calc(1.5rem+env(safe-area-inset-bottom,0px))]"
            >
              <div className="w-12 h-1 bg-white/20 rounded-full mx-auto mb-2" />
              
              <div className="flex items-center justify-between">
                <div>
                  <h3 className="text-base font-black text-white">Studio de Création</h3>
                  <p className="text-xs text-[#a38890]">Que souhaitez-vous publier aujourd'hui ?</p>
                </div>
                <button 
                  onClick={() => setUploadMenuOpen(false)}
                  className="p-1.5 rounded-full bg-white/10 text-[#a38890] hover:text-white"
                >
                  <X size={18} />
                </button>
              </div>

              <div className="grid grid-cols-2 gap-3 pt-2">
                {/* Option 1: Short TikTok */}
                <Link
                  href="/shorts/upload"
                  prefetch={true}
                  onClick={() => setUploadMenuOpen(false)}
                  className="p-4 rounded-2xl bg-gradient-to-br from-[#e05a68]/20 to-[#5c1922]/20 border border-[#e05a68]/40 hover:border-[#e05a68] flex flex-col items-center text-center space-y-2 group transition-all"
                >
                  <div className="w-12 h-12 rounded-2xl bg-[#e05a68]/20 text-[#e05a68] flex items-center justify-center group-hover:scale-110 transition-transform">
                    <Smartphone size={24} />
                  </div>
                  <div>
                    <h4 className="text-xs font-black text-white">Short Vertical</h4>
                    <p className="text-[10px] text-rose-300 font-medium">9:16 · Moins de 3 min</p>
                  </div>
                </Link>

                {/* Option 2: Vidéo Longue Catalogue */}
                <Link
                  href="/upload"
                  prefetch={true}
                  onClick={() => setUploadMenuOpen(false)}
                  className="p-4 rounded-2xl bg-gradient-to-br from-[#c93f52]/20 to-[#5c1922]/20 border border-[#c93f52]/40 hover:border-[#c93f52] flex flex-col items-center text-center space-y-2 group transition-all"
                >
                  <div className="w-12 h-12 rounded-2xl bg-[#c93f52]/20 text-[#e05a68] flex items-center justify-center group-hover:scale-110 transition-transform">
                    <Film size={24} />
                  </div>
                  <div>
                    <h4 className="text-xs font-black text-white">Vidéo Catalogue</h4>
                    <p className="text-[10px] text-rose-300 font-medium">16:9 · Qualité HD/4K</p>
                  </div>
                </Link>
              </div>
            </motion.div>
          </>
        )}
      </AnimatePresence>
    </>
  );
}
