"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { useState, useEffect } from "react";
import { Menu, X, Sun, Moon, Zap } from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";
import { useTheme } from "next-themes";

const navLinks = [
    { name: "Accueil", href: "/" },
    { name: "Services", href: "/services" },
    { name: "Projets", href: "/projects" },
    { name: "À Propos", href: "/about" },
    { name: "Blog", href: "/blog" },
    { name: "Me contacter", href: "/contact" },
];

export default function Navbar({ siteName }: { siteName: string }) {
    const pathname = usePathname();
    const [isOpen, setIsOpen] = useState(false);
    const [scrolled, setScrolled] = useState(false);
    const { theme, setTheme, resolvedTheme } = useTheme();
    const [mounted, setMounted] = useState(false);

    useEffect(() => {
        setMounted(true);
        const onScroll = () => setScrolled(window.scrollY > 20);
        window.addEventListener("scroll", onScroll);
        return () => window.removeEventListener("scroll", onScroll);
    }, []);

    const toggleTheme = () => {
        setTheme(resolvedTheme === "dark" ? "light" : "dark");
    };

    if (pathname.startsWith("/admin")) return null;
    const isDark = mounted && resolvedTheme === "dark";

    return (
        <nav style={{
            position: "fixed", top: scrolled ? "1rem" : "0", left: 0, right: 0, zIndex: 100,
            transition: "all 0.4s cubic-bezier(0.4, 0, 0.2, 1)",
            padding: scrolled ? "0 1.5rem" : "0",
        }}>
            <div className={`nav-container container ${scrolled ? 'glass' : ''}`} style={{
                display: "flex",
                alignItems: "center",
                justifyContent: "space-between",
                padding: scrolled ? "0.75rem 2rem" : "1.5rem 1.5rem",
                borderRadius: scrolled ? "var(--radius-lg)" : "0",
                maxWidth: scrolled ? "1000px" : "1200px",
                transition: "all 0.4s cubic-bezier(0.4, 0, 0.2, 1)",
                borderBottom: scrolled ? "1px solid var(--glass-border)" : "1px solid transparent",
            }}>
                {/* Logo */}
                <Link href="/" style={{ display: "flex", alignItems: "center", gap: "0.75rem" }}>
                    <div style={{
                        width: 34, height: 34,
                        background: "var(--primary)",
                        borderRadius: "10px",
                        display: "flex", alignItems: "center", justifyContent: "center",
                        boxShadow: "0 8px 16px -4px rgba(99, 102, 241, 0.4)"
                    }}>
                        <Zap size={18} color="white" fill="white" />
                    </div>
                    <div style={{ lineHeight: 1.1 }}>
                        <span className="nav-logo-text" style={{ fontFamily: "var(--font-heading)", fontWeight: 800, fontSize: "1.1rem", display: "block" }}>
                            {siteName.split(' ')[0]} <span className="text-gradient">{siteName.split(' ')[1] || ''}</span>
                        </span>
                        <span className="nav-tagline" style={{ fontSize: "0.6rem", color: "var(--muted-foreground)", fontWeight: 700, letterSpacing: "0.15em", textTransform: "uppercase" }}>
                            Advanced Engineering
                        </span>
                    </div>
                </Link>

                {/* Desktop Nav */}
                <div style={{ display: "flex", alignItems: "center", gap: "2rem" }} className="desktop-nav">
                    {navLinks.map(link => (
                        <Link key={link.href} href={link.href} style={{
                            position: "relative",
                            fontWeight: 600,
                            fontSize: "0.925rem",
                            color: pathname === link.href ? "var(--foreground)" : "var(--muted-foreground)",
                            transition: "all 0.3s ease"
                        }}>
                            {link.name}
                            {pathname === link.href && (
                                <motion.div layoutId="nav-indicator" style={{
                                    position: "absolute", bottom: -6, left: "20%", right: "20%",
                                    height: 3, background: "var(--primary)", borderRadius: 10
                                }} />
                            )}
                        </Link>
                    ))}
                    <div style={{ width: 1, height: 24, background: "var(--border)", margin: "0 0.5rem" }} />
                    <button onClick={toggleTheme} className="btn-icon" style={{
                        width: 36, height: 36, borderRadius: "50%",
                        background: "var(--muted)", display: "flex", alignItems: "center", justifyContent: "center",
                        transition: "all 0.3s"
                    }}>
                        {isDark ? <Sun size={18} /> : <Moon size={18} />}
                    </button>
                </div>

                {/* Mobile Controls */}
                <div style={{ display: "flex", alignItems: "center", gap: "0.5rem" }} className="mobile-nav">
                    <button onClick={toggleTheme} style={{ width: 36, height: 36, borderRadius: "50%", background: "var(--muted)", display: "flex", alignItems: "center", justifyContent: "center" }}>
                        {isDark ? <Sun size={18} /> : <Moon size={18} />}
                    </button>
                    <button onClick={() => setIsOpen(!isOpen)} style={{ border: "none", background: "none", width: 40, height: 40, display: "flex", alignItems: "center", justifyContent: "center", color: "var(--foreground)" }}>
                        {isOpen ? <X size={26} /> : <Menu size={26} />}
                    </button>
                </div>
            </div>

            <AnimatePresence>
                {isOpen && (
                    <motion.div initial={{ opacity: 0, scale: 0.95, y: -20 }} animate={{ opacity: 1, scale: 1, y: 0 }} exit={{ opacity: 0, scale: 0.95, y: -20 }} transition={{ duration: 0.3, ease: "easeOut" }}
                        style={{
                            position: "absolute", top: scrolled ? "4.5rem" : "5.5rem", left: "1rem", right: "1rem",
                            background: "var(--background)",
                            borderRadius: "2rem",
                            boxShadow: "0 40px 80px -15px rgba(0,0,0,0.3)",
                            border: "1px solid var(--border)",
                            overflowY: "auto",
                            maxHeight: "80vh",
                            zIndex: 101,
                            padding: "2.5rem 1.5rem"
                        }} className="glass">
                        <div style={{ display: "flex", flexDirection: "column", gap: "0.75rem", alignItems: "center" }}>
                            {navLinks.map((link, idx) => (
                                <motion.div key={link.href} initial={{ opacity: 0, y: 10 }} animate={{ opacity: 1, y: 0 }} transition={{ delay: idx * 0.05 }} style={{ width: "100%" }}>
                                    <Link href={link.href} onClick={() => setIsOpen(false)}
                                        style={{
                                            display: "flex", padding: "1.25rem 2rem", borderRadius: "1.25rem",
                                            fontWeight: 800, fontSize: "1.25rem", justifyContent: "center",
                                            color: pathname === link.href ? "white" : "var(--foreground)",
                                            background: pathname === link.href ? "var(--primary)" : "transparent",
                                            transition: "all 0.3s",
                                            boxShadow: pathname === link.href ? "0 10px 20px -5px rgba(99, 102, 241, 0.4)" : "none"
                                        }}>
                                        {link.name}
                                    </Link>
                                </motion.div>
                            ))}
                        </div>
                    </motion.div>
                )}
            </AnimatePresence>

            <style>{`
                .desktop-nav { display: flex; }
                .mobile-nav { display: none; }
                .btn-icon:hover { 
                    background: var(--primary) !important;
                    color: white !important;
                    transform: rotate(15deg);
                }
                @media (max-width: 900px) {
                    .desktop-nav { display: none !important; }
                    .mobile-nav { display: flex !important; }
                    .nav-tagline { display: none !important; }
                }
                @media (max-width: 480px) {
                    .nav-container { padding: 0.75rem 1rem !important; }
                    .nav-logo-text { font-size: 1rem !important; }
                }
            `}</style>
        </nav>
    );
}
