"use client";

import { useEffect, useState } from "react";
import { navigationData } from "@/data/navigation";
import { SearchIcon } from "@/components/shared/icons/SearchIcon";
import { SiteLogo } from "@/components/shared/SiteLogo";

interface NavbarProps {
  /**
   * "overlay" — transparent, sits over a dark hero (home page).
   * "solid"   — dark background, for inner pages that open on a light
   *             surface so the white logo and links stay legible.
   */
  variant?: "overlay" | "solid";
}

export function Navbar({ variant = "solid" }: NavbarProps) {
  const [isSticky, setIsSticky] = useState(false);
  const [openDropdown, setOpenDropdown] = useState<string | null>(null);

  useEffect(() => {
    const onScroll = () => setIsSticky(window.scrollY > 120);

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

  const idleBg = variant === "overlay" ? "bg-transparent" : "bg-tenext-dark";

  return (
    <header
      className={`tenext-navbar z-30 mx-auto grid h-[100px] max-w-[2520px] grid-cols-[auto_1fr_auto] items-center px-[34px] transition-[background-color,border-radius,box-shadow,transform] duration-500 ${
        isSticky
          ? "fixed left-5 right-5 top-0 rounded-b-[25px] bg-[#111111] shadow-[0_12px_35px_rgba(0,0,0,0.18)]"
          : `relative ${idleBg}`
      }`}
    >
      <div className="flex items-center gap-[24px] xl:gap-[64px]">
        <button
          className="navbar-reveal grid h-6 w-6 grid-cols-2 gap-[5px] text-white transition-colors hover:text-tenext-teal"
          aria-label="Open menu"
        >
          <span className="h-[7px] w-[7px] rounded-[2px] border-2 border-current" />
          <span className="h-[7px] w-[7px] rounded-[2px] border-2 border-current" />
          <span className="h-[7px] w-[7px] rounded-[2px] border-2 border-current" />
          <span className="h-[7px] w-[7px] rounded-[2px] border-2 border-current" />
        </button>

        {/* Logo */}
        <a
          href="/"
          className="navbar-reveal flex items-center"
          style={{ animationDelay: "120ms" }}
          aria-label="Tenext Technologies — Home"
        >
          <SiteLogo size={24} />
        </a>
      </div>

      {/* Navigation */}
      <nav
        className="hidden items-center justify-center gap-[4px] lg:flex xl:gap-[11px]"
        aria-label="Primary navigation"
      >
        {navigationData.map((item, index) => (
          <div
            key={item.label}
            className="navbar-reveal relative"
            style={{ animationDelay: `${140 + index * 70}ms` }}
            onMouseEnter={() => setOpenDropdown(item.label)}
            onMouseLeave={() => setOpenDropdown(null)}
          >
            <a
              href={item.href}
              className={`nav-link-swap flex h-10 items-center whitespace-nowrap rounded-[10px] px-[12px] text-[13px] font-bold uppercase leading-none text-white transition-colors hover:bg-tenext-dark xl:px-[21px] ${
                item.label === "Home" ? "bg-tenext-dark" : ""
              }`}
              style={{ fontFamily: "var(--font-sora)" }}
            >
              <span className="text-active">{item.label}</span>
              <span aria-hidden>{item.label}</span>
              {item.children && (
                <svg
                  className="ml-[12px] h-[5px] w-[5px] opacity-90"
                  viewBox="0 0 5 5"
                  fill="none"
                >
                  <path
                    d="M4.35 0.9L3.75 4.05L0.65 3.45"
                    stroke="currentColor"
                    strokeWidth="1.1"
                    strokeLinecap="round"
                    strokeLinejoin="round"
                  />
                </svg>
              )}
            </a>

            {/* Dropdown */}
            {item.children && openDropdown === item.label && (
              <div className="absolute left-0 top-full z-50 min-w-[286px] origin-top rounded-[20px] border border-tenext-card-border bg-[#111111] px-[45px] py-[30px] shadow-2xl">
                {item.children.map((child, childIndex) => (
                  <a
                    key={child.label}
                    href={child.href}
                    className="dropdown-reveal block py-[10px] pr-5 text-[14px] font-medium text-white/80 transition-all hover:pl-5 hover:text-tenext-teal"
                    style={{
                      animationDelay: `${childIndex * 45}ms`,
                      fontFamily: "var(--font-sora)",
                    }}
                  >
                    {child.label}
                  </a>
                ))}
              </div>
            )}
          </div>
        ))}
      </nav>

      {/* Right Icons */}
      <div
        className="navbar-reveal flex items-center justify-end gap-[10px]"
        style={{ animationDelay: "620ms" }}
      >
        <button
          className="hidden p-[8px] text-white transition-colors hover:text-tenext-teal md:block"
          aria-label="Search"
        >
          <SearchIcon className="h-[18px] w-[18px]" />
        </button>

        <a
          href="#"
          className="hidden items-center gap-[8px] p-[8px] text-[13px] font-bold uppercase leading-none text-white transition-colors hover:text-tenext-teal md:flex"
          style={{ fontFamily: "var(--font-sora)" }}
        >
          <svg className="h-4 w-4" viewBox="0 0 16 16" fill="none">
            <circle
              cx="8"
              cy="5"
              r="3.5"
              stroke="currentColor"
              strokeWidth="1.3"
            />
            <path
              d="M2 14.5C2 11.5 4.5 9.5 8 9.5C11.5 9.5 14 11.5 14 14.5"
              stroke="currentColor"
              strokeWidth="1.3"
              strokeLinecap="round"
            />
          </svg>
          Log in
        </a>

        <a
          href="/contacts"
          className="tenext-button tenext-button-gradient-border min-w-[188px] justify-center rounded-full px-[24px] py-[21px] text-[16px] font-semibold leading-none text-white"
          style={{ fontFamily: "var(--font-sora)" }}
        >
          Talk to an expert
          <span className="button-inner" />
        </a>

        {/* Mobile menu button */}
        <button className="flex flex-col gap-[5px] lg:hidden" aria-label="Menu">
          <span className="h-[2px] w-[22px] bg-white" />
          <span className="h-[2px] w-[22px] bg-white" />
          <span className="h-[2px] w-[22px] bg-white" />
        </button>
      </div>
    </header>
  );
}
