"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import { ChevronDown, Phone, Menu, X } from "lucide-react";
import Image from "next/image";
import LanguageSelectRadix from "../languageSwitcher";
import { useTranslations } from "next-intl";

export function Navbar() {
  const t = useTranslations();
  const [isDropdownOpen, setIsDropdownOpen] = useState(false);
  const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
  const [scrolled, setScrolled] = useState(false);

  useEffect(() => {
    const handleScroll = () => {
      setScrolled(window.scrollY > 50);
    };
    window.addEventListener("scroll", handleScroll);
    return () => window.removeEventListener("scroll", handleScroll);
  }, []);

  // Prevent body scroll when mobile menu is open
  useEffect(() => {
    if (isMobileMenuOpen) {
      document.body.style.overflow = "hidden";
    } else {
      document.body.style.overflow = "unset";
    }
  }, [isMobileMenuOpen]);

  return (
    <>
      <nav
        className={`fixed top-0 left-0 right-0 z-50 border-b border-gray-400/50 ${scrolled && "bg-black"} transition`}
      >
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
          <div className="flex justify-between items-center h-20">
            {/* Logo */}
            <Link href="/" className="hover:cursor-pointer">
              <div className="flex items-center gap-2">
                <div className=" flex items-center justify-center">
                  <Image
                    src="/images/IGNUM/PNG/1.@6x.png"
                    alt="logo image"
                    width={70}
                    height={70}
                    className=""
                  />
                </div>
              </div>
            </Link>

            {/* Desktop Navigation Menu */}
            <div className="hidden h-full lg:flex items-center gap-8">
              <Link
                href="/"
                className="font-unbounded uppercase text-white text-sm h-full flex items-center font-semibold hover:cursor-pointer hover:text-red-500 transition"
              >
                {t("navbar.home")}
              </Link>
              <Link
                href="/about"
                className="font-unbounded uppercase text-white text-sm h-full flex items-center font-semibold hover:cursor-pointer hover:text-red-500 transition"
              >
                {t("navbar.about")}
              </Link>

              {/* Pages Dropdown */}
              <div className="relative group h-full">
                <button
                  className="font-unbounded uppercase text-white text-sm h-full font-semibold hover:text-red-500 
               transition-colors flex items-center gap-1"
                >
                  {t("navbar.pages")}
                  <ChevronDown
                    size={16}
                    className="transition-transform group-hover:rotate-180"
                  />
                </button>

                {/* Dropdown Menu */}
                <div
                  className="absolute top-full left-0 w-40 bg-white rounded-b-md shadow-lg 
               font-semibold opacity-0 invisible group-hover:opacity-100 
               group-hover:visible transition-all duration-300 
               transform translate-y-2 group-hover:translate-y-0
               pointer-events-none group-hover:pointer-events-auto overflow-hidden"
                >
                  <Link
                    href="/faq"
                    className="font-unbounded uppercase block px-4 py-2 text-black text-sm hover:bg-red-600 
                 hover:text-white transition-colors"
                  >
                    {t("navbar.faq")}
                  </Link>
                  <Link
                    href="/services"
                    className="font-unbounded uppercase block px-4 py-2 text-black text-sm hover:bg-red-600 
                 hover:text-white transition-colors"
                  >
                    {t("navbar.services")}
                  </Link>
                  {/* <Link
                    href="/blog"
                    className="font-unbounded uppercase block px-4 py-2 text-black text-sm hover:bg-red-600 
                 hover:text-white transition-colors rounded-b-md"
                  >
                    Blog
                  </Link> */}
                </div>
              </div>

              <Link
                href="/products"
                className="font-unbounded uppercase text-white text-sm h-full flex items-center font-semibold hover:cursor-pointer hover:text-red-500 transition"
              >
                {t("navbar.products")}
              </Link>
              <Link
                href="/contact"
                className="font-unbounded uppercase text-white text-sm h-full flex items-center font-semibold hover:cursor-pointer hover:text-red-500 transition"
              >
                {t("navbar.contact")}
              </Link>
            </div>

            <div className="flex items-center gap-5">
              {/* Language Switcher */}
              <LanguageSelectRadix />

              {/* Emergency Call Button - Hidden on mobile */}
              <div className="hidden lg:flex items-center gap-3 bg-red-600 px-4 py-2 rounded-full">
                <Phone size={20} className="text-white" />
                <div>
                  <div className="font-unbounded uppercase text-white text-xs font-semibold">
                    {t("navbar.emergency")}
                  </div>
                  <div className="text-white text-sm font-bold">
                    +123-456-7890
                  </div>
                </div>
              </div>

              {/* Mobile Menu Button */}
              <button
                onClick={() => setIsMobileMenuOpen(true)}
                className="lg:hidden text-white p-2 hover:bg-red-600 rounded-md transition"
              >
                <Menu size={24} />
              </button>
            </div>
          </div>
        </div>
      </nav>

      {/* Mobile Menu Overlay */}
      <div
        className={`fixed inset-0 bg-black/50 z-50 transition-opacity duration-300 lg:hidden ${
          isMobileMenuOpen
            ? "opacity-100 pointer-events-auto"
            : "opacity-0 pointer-events-none"
        }`}
        onClick={() => setIsMobileMenuOpen(false)}
      />

      {/* Mobile Menu Sidebar */}
      <div
        className={`fixed top-0 left-0 bottom-0 w-80 bg-black z-50 transform transition-transform duration-300 ease-in-out lg:hidden ${
          isMobileMenuOpen ? "translate-x-0" : "-translate-x-full"
        }`}
      >
        {/* Mobile Menu Header */}
        <div className="flex justify-between items-center p-6 border-b border-gray-700">
          {/* Logo */}
          <Link href="/" className="hover:cursor-pointer">
            <div className="font-unbounded uppercase flex flex-col items-center text-2xl gap-2 text-white font-bold">
              <div className=" flex items-center justify-center">
                <Image
                  src="/images/IGNUM/PNG/1.@6x.png"
                  alt="logo image"
                  width={50}
                  height={50}
                  className=""
                />
              </div>
            </div>
          </Link>
          <button
            onClick={() => setIsMobileMenuOpen(false)}
            className="text-white p-2 hover:bg-red-600 rounded-md transition"
          >
            <X size={24} />
          </button>
        </div>

        {/* Mobile Menu Links */}
        <div className="flex flex-col p-6 gap-4">
          <Link
            href="/"
            className="font-unbounded uppercase text-white text-base font-semibold hover:text-red-500 transition py-2"
            onClick={() => setIsMobileMenuOpen(false)}
          >
            {t("navbar.home")}
          </Link>
          <Link
            href="/about"
            className="font-unbounded uppercase text-white text-base font-semibold hover:text-red-500 transition py-2"
            onClick={() => setIsMobileMenuOpen(false)}
          >
            {t("navbar.about")}
          </Link>

          {/* Mobile Pages Dropdown */}
          <div>
            <button
              onClick={() => setIsDropdownOpen(!isDropdownOpen)}
              className="font-unbounded uppercase text-white text-base font-semibold hover:text-red-500 transition flex items-center gap-1 py-2 w-full"
            >
              {t("navbar.pages")}
              <ChevronDown
                size={16}
                className={`transition-transform ${isDropdownOpen ? "rotate-180" : ""}`}
              />
            </button>
            {isDropdownOpen && (
              <div className="ml-4 mt-2 flex flex-col gap-2">
                <Link
                  href="/faq"
                  className="font-unbounded uppercase text-white/80 text-sm hover:text-red-500 transition py-2"
                  onClick={() => setIsMobileMenuOpen(false)}
                >
                  {t("navbar.faq")}
                </Link>
                <Link
                  href="/services"
                  className="font-unbounded uppercase text-white/80 text-sm hover:text-red-500 transition py-2"
                  onClick={() => setIsMobileMenuOpen(false)}
                >
                  {t("navbar.services")}
                </Link>
              </div>
            )}
          </div>

          <Link
            href="/products"
            className="font-unbounded uppercase text-white text-base font-semibold hover:text-red-500 transition py-2"
            onClick={() => setIsMobileMenuOpen(false)}
          >
            {t("navbar.products")}
          </Link>
          <Link
            href="/contact"
            className="font-unbounded uppercase text-white text-base font-semibold hover:text-red-500 transition py-2"
            onClick={() => setIsMobileMenuOpen(false)}
          >
            {t("navbar.contact")}
          </Link>
        </div>
      </div>
    </>
  );
}
