"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { useState } from "react";
import { BrandLockup } from "@/components/BrandLockup";
import type { NavService } from "@/lib/types";

const links = [
  ["/", "صفحه اصلی"],
  ["/about", "درباره ما"],
  ["/domains", "حوزه‌های فناوری"],
  ["/innovation-finance", "تأمین مالی نوآوری"],
  ["/experience", "پروژه‌ها و تجارب"],
  ["/insights", "دانش و تحلیل"],
  ["/collaborate", "همکاری با ما"],
  ["/contact", "تماس با ما"],
] as const;

export function Header({ services, experienceTitle }: { services: NavService[]; experienceTitle: string }) {
  const [open, setOpen] = useState(false);
  const pathname = usePathname();
  const menu = links.map(([href, label]) => (href === "/experience" ? [href, experienceTitle] : [href, label])) as [string, string][];

  const navClass = (href: string) => {
    const active = href === "/" ? pathname === "/" : pathname.startsWith(href);
    return `nav-link ${active ? "is-active" : ""}`;
  };

  return (
    <header className="header-glass sticky top-0 z-50">
      <div className="shell flex items-center gap-4 py-4">
        <BrandLockup compact href="/" />
        <div className="ms-auto flex shrink-0 items-center gap-3">
          <Link href="/request" className="btn btn-solid hidden shrink-0 px-5 text-[0.95rem] md:inline-flex">
            درخواست جلسه مشاوره
          </Link>
          <button
            type="button"
            className="min-h-11 rounded-full border border-line bg-white px-4 text-base font-semibold text-navy transition hover:border-navy hover:bg-navy hover:text-white lg:hidden"
            aria-expanded={open}
            aria-controls="mobile-nav"
            onClick={() => setOpen((value) => !value)}
          >
            {open ? "بستن" : "منو"}
          </button>
        </div>
      </div>

      <div className="hidden border-t border-line/80 lg:block">
        <nav className="shell flex flex-wrap items-center gap-x-6 gap-y-2.5 py-3.5" aria-label="اصلی">
          {menu.slice(0, 2).map(([href, label]) => (
            <Link key={href} href={href} className={navClass(href)}>{label}</Link>
          ))}
          <div className="group relative">
            <Link href="/services" className={navClass("/services")}>خدمات تخصصی</Link>
            <div className="invisible absolute top-full z-30 mt-3 w-[24rem] border border-line bg-white p-2 opacity-0 shadow-[var(--shadow-soft)] transition group-hover:visible group-hover:opacity-100 group-focus-within:visible group-focus-within:opacity-100">
              {services.map((service) => (
                <Link
                  key={service.slug}
                  href={`/services/${service.slug}`}
                  className="block border-b border-line/60 px-3.5 py-3 text-[0.95rem] leading-7 text-ink last:border-0 hover:bg-mist hover:text-navy"
                >
                  {service.title}
                </Link>
              ))}
            </div>
          </div>
          {menu.slice(2).map(([href, label]) => (
            <Link key={href} href={href} className={navClass(href)}>{label}</Link>
          ))}
        </nav>
      </div>
      <div className="h-[2px] bg-gradient-to-l from-transparent via-gold to-teal/70" />

      {open && (
        <nav id="mobile-nav" className="space-y-1 border-t border-line bg-white px-4 py-5 lg:hidden" aria-label="موبایل">
          {menu.map(([href, label]) => (
            <Link key={href} href={href} className="block px-2 py-3 text-base font-semibold" onClick={() => setOpen(false)}>
              {label}
            </Link>
          ))}
          <p className="px-2 pt-4 text-sm font-semibold tracking-wide text-muted">خدمات تخصصی</p>
          {services.map((service) => (
            <Link key={service.slug} href={`/services/${service.slug}`} className="block px-2 py-2.5 text-base" onClick={() => setOpen(false)}>
              {service.title}
            </Link>
          ))}
          <Link href="/request" className="btn btn-solid mt-4 w-full" onClick={() => setOpen(false)}>
            درخواست جلسه مشاوره و ارزیابی نیاز فناورانه
          </Link>
        </nav>
      )}
    </header>
  );
}
