"use client";

import { useState, useEffect } from "react";
import Link from "next/link";
import Image from "next/image";
import { usePathname } from "next/navigation";
import { motion, AnimatePresence } from "framer-motion";
import { Menu, X, Globe } from "lucide-react";

const navLinksEn = [
  { name: "Rooms", href: "/en/odalar" },
  { name: "Activities", href: "/en/aktiviteler" },
  { name: "Restaurant", href: "/en/restoran" },
  { name: "Events", href: "/en/etkinlikler" },
  { name: "About Us", href: "/en/hakkimizda" },
  { name: "Contact", href: "/en/iletisim" },
];

const navLinksTr = [
  { name: "Odalar", href: "/tr/odalar" },
  { name: "Aktiviteler", href: "/tr/aktiviteler" },
  { name: "Restoran", href: "/tr/restoran" },
  { name: "Etkinlikler", href: "/tr/etkinlikler" },
  { name: "Hakkımızda", href: "/tr/hakkimizda" },
  { name: "İletişim", href: "/tr/iletisim" },
];

export default function Navbar({ lang }: { lang?: string }) {
  const [isOpen, setIsOpen] = useState(false);
  const [scrolled, setScrolled] = useState(false);
  const pathname = usePathname();

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 60);
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  useEffect(() => { setIsOpen(false); }, [pathname]);

  const currentLang = lang || (pathname.startsWith("/en") ? "en" : "tr");
  const toggleLang = currentLang === "en" ? "tr" : "en";
  const navLinks = currentLang === "en" ? navLinksEn : navLinksTr;

  const isHome = pathname === "/" || pathname === "/tr" || pathname === "/en";

  const getToggleHref = () => {
    if (pathname === "/") return `/${toggleLang}`;
    if (pathname.startsWith("/tr/")) return pathname.replace("/tr/", `/${toggleLang}/`);
    if (pathname === "/tr") return `/${toggleLang}`;
    if (pathname.startsWith("/en/")) return pathname.replace("/en/", `/${toggleLang}/`);
    if (pathname === "/en") return `/${toggleLang}`;
    return `/${toggleLang}${pathname}`;
  };

  return (
    <nav
      className={`fixed top-0 left-0 right-0 z-50 transition-all duration-500 ${scrolled
          ? "bg-white/95 backdrop-blur-md shadow-sm py-4"
          : "bg-transparent py-6"
        }`}
    >
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div className="flex justify-between items-center">

          {/* Logo */}
          <Link href={`/${currentLang}`} className="flex-shrink-0 flex items-center group">
            <Image
              src="/logo.png"
              alt="Kite Beach Logo"
              width={140}
              height={48}
              className={`w-auto h-auto object-contain transition-all duration-300 ${!scrolled && isHome ? 'brightness-0 invert' : ''}`}
              priority
            />
          </Link>

          {/* Desktop links */}
          <div className="hidden md:flex md:items-center md:gap-8">
            {navLinks.map((link) => {
              const active = pathname === link.href;
              return (
                <Link
                  key={link.name}
                  href={link.href}
                  className={`text-[13px] font-medium transition-colors relative group ${scrolled
                      ? active ? "text-primary" : "text-foreground hover:text-primary"
                      : isHome
                        ? active ? "text-accent" : "text-white/85 hover:text-white"
                        : active ? "text-primary" : "text-foreground hover:text-primary"
                    }`}
                >
                  {link.name}
                  <span
                    className={`absolute -bottom-0.5 left-0 h-px bg-current transition-all duration-300 ${active ? "w-full" : "w-0 group-hover:w-full"
                      }`}
                  />
                </Link>
              );
            })}

            <Link
              href={`/${currentLang}/iletisim`}
              className="btn-shimmer bg-accent text-white px-6 py-2.5 rounded-full text-[11px] font-bold tracking-[0.12em] uppercase hover:bg-accent/90 transition-colors shadow-md"
            >
              {currentLang === "en" ? "Contact Us" : "Bize Ulaşın"}
            </Link>

            {/* Lang Toggle */}
            <Link
              href={getToggleHref()}
              className={`flex items-center gap-1.5 text-[11px] font-bold tracking-[0.12em] uppercase transition-colors hover:text-accent ${scrolled
                  ? "text-dark"
                  : isHome
                    ? "text-white"
                    : "text-dark"
                }`}
            >
              <Globe size={14} />
              <span>{currentLang === "tr" ? "EN" : "TR"}</span>
            </Link>
          </div>

          {/* Mobile toggle */}
          <button
            onClick={() => setIsOpen(!isOpen)}
            className={`flex md:hidden p-1 transition-colors ${scrolled ? "text-dark" : isHome ? "text-white" : "text-dark"
              }`}
            aria-label="Menü"
          >
            {isOpen ? <X size={26} /> : <Menu size={26} />}
          </button>
        </div>
      </div>

      {/* Mobile Menu */}
      <AnimatePresence>
        {isOpen && (
          <motion.div
            key="mobile-menu"
            initial={{ opacity: 0, height: 0 }}
            animate={{ opacity: 1, height: "auto" }}
            exit={{ opacity: 0, height: 0 }}
            transition={{ duration: 0.3, ease: [0.16, 1, 0.3, 1] }}
            className="md:hidden overflow-hidden bg-white border-t border-sand"
          >
            <div className="px-5 py-5 flex flex-col gap-1">
              {navLinks.map((link, i) => (
                <motion.div
                  key={link.name}
                  initial={{ opacity: 0, x: -10 }}
                  animate={{ opacity: 1, x: 0 }}
                  transition={{ delay: i * 0.05 }}
                >
                  <Link
                    href={link.href}
                    className={`block py-3 text-base font-medium border-b border-sand/60 transition-colors ${pathname === link.href ? "text-primary" : "text-dark hover:text-primary"
                      }`}
                  >
                    {link.name}
                  </Link>
                </motion.div>
              ))}
              <Link
                href={getToggleHref()}
                className="mt-2 flex items-center justify-center gap-2 text-sm font-bold uppercase text-dark border border-sand py-3 rounded-full hover:bg-sand/20 transition-colors"
              >
                <Globe size={16} />
                {currentLang === "tr" ? "English" : "Türkçe"}
              </Link>
              <Link
                href={`/${currentLang}/iletisim`}
                className="mt-4 block w-full text-center bg-accent text-white px-6 py-3.5 rounded-full text-sm font-bold uppercase tracking-wider"
              >
                {currentLang === "en" ? "Contact Us" : "Bize Ulaşın"}
              </Link>
            </div>
          </motion.div>
        )}
      </AnimatePresence>
    </nav>
  );
}
