"use client";

import { useEffect, useState } from "react";
import { usePathname } from "next/navigation";
import { Menu, X, Phone } from "lucide-react";
import { NAV_ITEMS } from "@/lib/content";

export default function MobileMenu() {
  const [open, setOpen] = useState(false);
  const pathname = usePathname();
  const [lastPath, setLastPath] = useState(pathname);

  // Close the mobile panel on route change (render-time adjustment, no effect)
  if (lastPath !== pathname) {
    setLastPath(pathname);
    setOpen(false);
  }

  useEffect(() => {
    if (!open) return;
    const onKey = (e: KeyboardEvent) => {
      if (e.key === "Escape") setOpen(false);
    };
    document.addEventListener("keydown", onKey);
    return () => document.removeEventListener("keydown", onKey);
  }, [open ]);

  // (route-change close handled via render-time adjustment above)

  const isActive = (href: string) =>
    href === "/" ? pathname === "/" : pathname.startsWith(href);

  return (
    <>
      <nav
        id="primary-nav"
        aria-label="Main"
        className={`${
          open ? "flex" : "hidden"
        } absolute inset-x-0 top-[72px] flex-col gap-0 border-b border-line bg-white px-[24px] pb-6 pt-3 md:static md:flex md:flex-row md:items-center md:gap-7 md:border-0 md:bg-transparent md:p-0 max-md:shadow-card`}
      >
        <ul className="flex flex-col md:flex-row md:items-center md:gap-7">
          {NAV_ITEMS.map((item) => {
            const active = isActive(item.href);
            return (
              <li key={item.label}>
                <a
                  href={item.href}
                  onClick={() => setOpen(false)}
                  aria-current={active ? "page" : undefined}
                  className={`block whitespace-nowrap border-b border-line py-3.5 text-[13.5px] hover:text-brand md:border-0 md:py-1 ${
                    active
                      ? "font-semibold text-brand md:relative md:after:absolute md:after:-bottom-[6px] md:after:left-0 md:after:h-[2px] md:after:w-full md:after:rounded-full md:after:bg-brand"
                      : "font-medium text-ink"
                  }`}
                >
                  {item.label}
                </a>
              </li>
            );
          })}
        </ul>
        {/* Single CTA instance — row on desktop, stacked on mobile */}
        <div className="mt-4 flex flex-col gap-3 md:ml-9 md:mt-0 md:flex-row md:items-center md:gap-5">
          <a
            href="/request-demo"
            onClick={() => setOpen(false)}
            className="inline-flex min-h-[42px] items-center justify-center rounded-lg bg-brand px-5 text-sm font-semibold text-white hover:bg-brand-dark"
          >
            Request a Demo
          </a>
          {/* Mobile-only direct-call rows */}
          <div className="grid grid-cols-2 gap-2.5 md:hidden">
            <a
              href="tel:+919818993537"
              className="flex min-h-[48px] items-center justify-center gap-2 rounded-xl border border-line bg-white px-3 text-[13px] font-semibold text-ink"
            >
              <span className="grid size-7 shrink-0 place-items-center rounded-full bg-brand text-white" aria-hidden="true">
                <Phone size={14} />
              </span>
              <span className="leading-tight">
                +91-9818993537
                <span className="block font-mono text-[9px] font-medium tracking-[0.12em] text-muted">SALES</span>
              </span>
            </a>
            <a
              href="tel:+919899745220"
              className="flex min-h-[48px] items-center justify-center gap-2 rounded-xl border border-line bg-white px-3 text-[13px] font-semibold text-ink"
            >
              <span className="grid size-7 shrink-0 place-items-center rounded-full bg-navy text-white" aria-hidden="true">
                <Phone size={14} />
              </span>
              <span className="leading-tight">
                +91-9899745220
                <span className="block font-mono text-[9px] font-medium tracking-[0.12em] text-muted">SUPPORT</span>
              </span>
            </a>
          </div>
        </div>
      </nav>

      <button
        type="button"
        className="ml-auto grid size-11 place-items-center rounded-lg border border-line text-navy md:hidden"
        aria-label={open ? "Close menu" : "Open menu"}
        aria-expanded={open}
        aria-controls="primary-nav"
        onClick={() => setOpen((v) => !v)}
      >
        {open ? <X size={20} /> : <Menu size={20} />}
      </button>
    </>
  );
}
