"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { useState } from "react";
import ThemeToggle from "./ThemeToggle";

// CUSTOMIZATION HOOK: edit nav labels / routes here.
const navItems = [
  { href: "/", ko: "홈", en: "Home", no: "01" },
  { href: "/members", ko: "구성원", en: "Members", no: "02" },
  { href: "/publications", ko: "논문", en: "Publications", no: "03" },
  { href: "/lectures", ko: "강의", en: "Lectures", no: "04" },
  { href: "/standardization", ko: "표준화", en: "Standardization", no: "05" },
];

export default function Header() {
  const [open, setOpen] = useState(false);
  const pathname = usePathname();

  const isActive = (href: string) =>
    pathname
      ? href === "/"
        ? pathname === "/"
        : pathname === href || pathname.startsWith(`${href}/`)
      : false;

  return (
    <header className="sticky top-0 z-50 border-b border-ink bg-ivory/85 backdrop-blur">
      {/* Journal issue strip — magazine masthead feel. */}
      <div className="hidden border-b border-line desktop:block">
        <div className="container-content flex items-center justify-between py-1.5">
          <span className="kicker">Issue 01 — 2026</span>
          {/* CUSTOMIZATION HOOK: masthead tagline */}
          <span className="kicker text-ink-mute">
            Kyungpook National University · School of Computer Science &amp; Engineering
          </span>
          <span className="kicker">대구 · Daegu</span>
        </div>
      </div>

      <div className="container-content flex h-[4.25rem] items-center justify-between gap-8">
        {/* Brand logo — explicit right margin to guarantee clear separation from 01 HOME */}
        <Link
          href="/"
          className="group flex flex-col leading-none shrink-0 mr-8 xl:mr-16"
          onClick={() => setOpen(false)}
        >
          <span className="headline-ko text-lg text-ink sm:text-xl">
            AI 에이전트 네트워크 연구실
          </span>
          <span className="kicker mt-1 group-hover:text-vermillion">
            AI Agent Networking Lab (ANL)
          </span>
        </Link>

        {/* Desktop nav — small caps, wide tracking (visible at >= 1240px) */}
        <div className="hidden items-center gap-6 desktop:flex ml-auto">
          <nav className="flex items-center gap-2" aria-label="주 메뉴">
            {navItems.map((item) => {
              const active = isActive(item.href);
              return (
                <Link
                  key={item.href}
                  href={item.href}
                  aria-current={active ? "page" : undefined}
                  style={
                    active
                      ? { backgroundColor: "var(--vermillion)", color: "#ffffff" }
                      : undefined
                  }
                  className={`group relative flex items-baseline gap-1.5 text-[0.78rem] font-bold uppercase tracking-[0.18em] transition-all px-3.5 py-1.5 rounded-md ${
                    active
                      ? "shadow-sm font-bold"
                      : "text-ink hover:text-vermillion hover:bg-paper/60"
                  }`}
                >
                  <span
                    style={active ? { color: "#ffffff" } : undefined}
                    className={`font-display text-[0.62rem] ${
                      active ? "opacity-90 font-bold" : "text-ink-mute"
                    }`}
                  >
                    {item.no}
                  </span>
                  <span style={active ? { color: "#ffffff" } : undefined}>
                    {item.en}
                  </span>
                </Link>
              );
            })}
          </nav>
          <div className="h-4 w-px bg-line" aria-hidden="true" />
          <ThemeToggle />
        </div>

        {/* Mobile controls (visible under 1240px) */}
        <div className="flex items-center gap-2 desktop:hidden">
          <ThemeToggle />
          <button
            type="button"
            className="inline-flex items-center justify-center border border-ink p-2 text-ink transition hover:bg-ink hover:text-ivory"
            aria-label={open ? "메뉴 닫기" : "메뉴 열기"}
            aria-expanded={open}
            aria-controls="mobile-menu"
            onClick={() => setOpen((v) => !v)}
          >
            <svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" strokeWidth={1.8} stroke="currentColor">
              {open ? (
                <path strokeLinecap="round" strokeLinejoin="round" d="M6 18 18 6M6 6l12 12" />
              ) : (
                <path strokeLinecap="round" strokeLinejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5M3.75 17.25h16.5" />
              )}
            </svg>
          </button>
        </div>
      </div>

      {/* Mobile menu panel */}
      {open && (
        <nav
          id="mobile-menu"
          className="border-t border-ink bg-ivory desktop:hidden"
          aria-label="모바일 메뉴"
        >
          <ul className="container-content flex flex-col py-2">
            {navItems.map((item) => {
              const active = isActive(item.href);
              return (
                <li key={item.href}>
                  <Link
                    href={item.href}
                    onClick={() => setOpen(false)}
                    aria-current={active ? "page" : undefined}
                    style={
                      active
                        ? { backgroundColor: "var(--vermillion)", color: "#ffffff" }
                        : undefined
                    }
                    className={`flex items-baseline justify-between px-4 py-3.5 transition-colors rounded-md my-1 ${
                      active
                        ? "font-bold shadow-sm"
                        : "text-ink hover:bg-paper/60"
                    }`}
                  >
                    <span className="flex items-baseline gap-3">
                      <span
                        style={active ? { color: "#ffffff" } : undefined}
                        className={`font-display text-xs ${
                          active ? "opacity-90 font-bold" : "text-ink-mute"
                        }`}
                      >
                        {item.no}
                      </span>
                      <span
                        style={active ? { color: "#ffffff" } : undefined}
                        className="headline-ko text-lg font-bold"
                      >
                        {item.ko}
                      </span>
                    </span>
                    <span
                      style={active ? { color: "#ffffff" } : undefined}
                      className={`text-[0.7rem] font-semibold uppercase tracking-[0.18em] ${
                        active ? "opacity-90 font-bold" : "text-ink-mute"
                      }`}
                    >
                      {item.en}
                    </span>
                  </Link>
                </li>
              );
            })}
          </ul>
        </nav>
      )}
    </header>
  );
}
