"use client";
import Image from "next/image";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { ChevronDown, Lock, Menu, X } from "lucide-react";
import { useEffect, useState } from "react";
import { usePathname } from "next/navigation";
import { AnimatePresence, motion } from "framer-motion";

export default function Navbar() {
  const pathname = usePathname();
  const [showProdukMenu, setShowProdukMenu] = useState(false);
  const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);

  const isActive = (path: string) =>
    pathname === path || pathname.startsWith(path + "/");

  useEffect(() => {
    const handleClickOutside = () => {
      setShowProdukMenu(false);
    };

    window.addEventListener("click", handleClickOutside);
    return () => window.removeEventListener("click", handleClickOutside);
  }, []);

  const produkList = [
    {
      name: "JAECOO J7 AWD",
      img: "/j7awd.png",
      link: "/produk/jaecoo7",
    },
    {
      name: "JAECOO J7 SHS",
      img: "/j7shs.png",
      link: "/produk/jaecoo7",
    },
    {
      name: "JAECOO J8 AWD",
      img: "/j8awd.png",
      link: "/produk/jaecoo8",
    },
  ];

  return (
    <nav className="relative w-full flex items-center justify-between py-4 px-6 sm:px-10 bg-white z-50">
      <div className="flex items-center gap-4">
        <Link href="/" className="flex items-center space-x-2">
          <Image
            src="/masjaecoonav.png"
            alt="MAS JAECOO Logo"
            width={300}
            height={30}
            className=" object-fill"
          />
        </Link>
      </div>

      <button
        className="sm:hidden absolute right-6 text-[#1F3D4A]"
        onClick={() => setIsMobileMenuOpen((prev) => !prev)}
      >
        {isMobileMenuOpen ? (
          <X className="w-6 h-6" />
        ) : (
          <Menu className="w-6 h-6" />
        )}
      </button>

      <ul className="hidden sm:flex mx-auto items-center gap-4 sm:gap-6 text-sm font-medium">
        <li>
          <Link href="/">
            <Button
              variant="ghost"
              className={`hover:cursor-pointer rounded-full px-5 ${
                isActive("/") ? "bg-[#C2D8E2] text-[#1F3D4A]" : ""
              }`}
            >
              BERANDA
            </Button>
          </Link>
        </li>

        <li className="">
          <Button
            variant="ghost"
            onClick={(e) => {
              e.stopPropagation();
              setShowProdukMenu((prev) => !prev);
            }}
            className={`flex items-center gap-1 font-bold text-sm focus:outline-none hover:cursor-pointer rounded-full px-5 py-2 ${
              isActive("/product") || isActive("/produk")
                ? "bg-[#C2D8E2] text-[#1F3D4A]"
                : ""
            }`}
          >
            PRODUK <ChevronDown className="w-4 h-4" />
          </Button>

          <AnimatePresence>
            {showProdukMenu && (
              <motion.div
                key="produk-dropdown"
                initial={{ opacity: 0, y: 20 }}
                animate={{ opacity: 1, y: 0 }}
                exit={{ opacity: 0, y: 20 }}
                transition={{ duration: 0.3 }}
                className="absolute left-0 right-0 top-[calc(100%+1rem)] z-50 bg-white shadow-xl px-6 sm:px-10 py-6 rounded-xl w-full"
                onClick={(e) => e.stopPropagation()}
              >
                <div className="max-w-screen-xl mx-auto w-full flex flex-col sm:flex-row items-center sm:items-start justify-between gap-y-10 sm:gap-y-0 sm:gap-x-6 text-center sm:text-left">
                  {produkList.map((car, i) => (
                    <motion.div
                      key={i}
                      initial={{ opacity: 0, y: 30 }}
                      animate={{ opacity: 1, y: 0 }}
                      exit={{ opacity: 0, y: 30 }}
                      transition={{ delay: 0.2 + i * 0.2, duration: 0.5 }}
                      className="flex flex-col items-center text-center w-full sm:w-auto"
                    >
                      <Image
                        src={car.img}
                        alt={car.name}
                        width={250}
                        height={150}
                        className="object-contain"
                      />
                      <p className="font-bold mt-4 text-center">{car.name}</p>
                      <div className="flex flex-col sm:flex-row gap-2 mt-2 items-center">
                        <Link href="/about" className="w-[200px]">
                          <Button
                            variant="default"
                            className="bg-[#0C5B71] text-white px-4 rounded-full w-full hover:cursor-pointer"
                          >
                            TEST DRIVE
                          </Button>
                        </Link>
                        <Link href="/product" className="w-[200px]">
                          <Button
                            variant="outline"
                            className="rounded-full px-4 w-full hover:cursor-pointer hover:bg-amber-50"
                          >
                            JELAJAHI
                          </Button>
                        </Link>
                      </div>
                    </motion.div>
                  ))}
                </div>
              </motion.div>
            )}
          </AnimatePresence>
        </li>

        <li>
          <Link href="/service">
            <Button
              variant="ghost"
              className={`hover:cursor-pointer rounded-full px-5 ${
                isActive("/service") ? "bg-[#C2D8E2] text-[#1F3D4A]" : ""
              }`}
            >
              SERVIS
            </Button>
          </Link>
        </li>

        <li>
          <Link href="/about">
            <Button
              variant="ghost"
              className={`hover:cursor-pointer rounded-full px-5 ${
                isActive("/about") ? "bg-[#C2D8E2] text-[#1F3D4A]" : ""
              }`}
            >
              TENTANG DEALER JAECOO
            </Button>
          </Link>
        </li>
      </ul>

      <div className="hidden sm:block">
        <Link href="/auth">
          <Button className="bg-[#1F6779]">
            <Lock className="w-3 h-3 mr-1" />
            Login
          </Button>
        </Link>
      </div>

      {isMobileMenuOpen && (
        <div className="absolute top-full left-0 right-0 bg-white px-6 py-4 shadow-md flex flex-col gap-4 text-sm font-medium sm:hidden z-40">
          <Link href="/" onClick={() => setIsMobileMenuOpen(false)}>
            <Button
              variant="ghost"
              className={`w-full justify-start ${
                isActive("/") ? "bg-[#C2D8E2] text-[#1F3D4A]" : ""
              }`}
            >
              BERANDA
            </Button>
          </Link>
          <Link href="/product" onClick={() => setIsMobileMenuOpen(false)}>
            <Button
              variant="ghost"
              className={`flex items-center gap-1 font-bold text-sm focus:outline-none rounded-full px-5 py-2 ${
                isActive("/product") || isActive("/produk")
                  ? "bg-[#C2D8E2] text-[#1F3D4A]"
                  : ""
              }`}
            >
              PRODUK
            </Button>
          </Link>
          <Link href="/service" onClick={() => setIsMobileMenuOpen(false)}>
            <Button
              variant="ghost"
              className={`w-full justify-start ${
                isActive("/service") ? "bg-[#C2D8E2] text-[#1F3D4A]" : ""
              }`}
            >
              SERVIS
            </Button>
          </Link>
          <Link href="/about" onClick={() => setIsMobileMenuOpen(false)}>
            <Button
              variant="ghost"
              className={`w-full justify-start ${
                isActive("/about") ? "bg-[#C2D8E2] text-[#1F3D4A]" : ""
              }`}
            >
              TENTANG DEALER JAECOO
            </Button>
          </Link>
          <Link href="/auth" onClick={() => setIsMobileMenuOpen(false)}>
            <Button className="bg-[#1F6779] w-full justify-start">
              <Lock className="w-3 h-3 mr-1" />
              Login
            </Button>
          </Link>
        </div>
      )}
    </nav>
  );
}
