"use client";

import { useState } from "react";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { useUnconfiguredNodes } from "@/lib/useNodes";
import { useUnacknowledgedAlerts } from "@/lib/useAlerts";
import { useAuth } from "@/components/AuthProvider";
import { useTheme } from "@/components/ThemeProvider";

const links = [
  { href: "/dashboard",  label: "Dashboard" },
  { href: "/nodes",      label: "Nodes" },
  { href: "/systems",    label: "Systems" },
  { href: "/calls",      label: "Calls" },
  { href: "/incidents",  label: "Incidents" },
  { href: "/map",        label: "Map" },
  { href: "/alerts",     label: "Alerts" },
  { href: "/trips",      label: "Trips" },
];

const adminLinks = [
  { href: "/tokens", label: "Tokens" },
  { href: "/admin",  label: "Admin" },
];

function SunIcon() {
  return (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="5"/>
      <line x1="12" y1="1" x2="12" y2="3"/>
      <line x1="12" y1="21" x2="12" y2="23"/>
      <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/>
      <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/>
      <line x1="1" y1="12" x2="3" y2="12"/>
      <line x1="21" y1="12" x2="23" y2="12"/>
      <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/>
      <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/>
    </svg>
  );
}

function MoonIcon() {
  return (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
    </svg>
  );
}

export function Nav() {
  const { user, isAdmin } = useAuth();
  const pathname = usePathname();
  const router = useRouter();
  const { nodes: pending } = useUnconfiguredNodes();
  const unackedAlerts = useUnacknowledgedAlerts();
  const { theme, toggle } = useTheme();
  const [mobileOpen, setMobileOpen] = useState(false);

  if (!user) return null;

  const allLinks = [...links, ...(isAdmin ? adminLinks : [])];

  function navLinkClass(href: string) {
    return `text-sm font-mono transition-colors shrink-0 ${
      pathname.startsWith(href) ? "text-white" : "text-gray-500 hover:text-gray-300"
    }`;
  }

  return (
    <nav className="sticky top-0 z-40 border-b border-gray-800 bg-gray-950/95 backdrop-blur">
      {/* Main bar */}
      <div className="px-4 md:px-6 py-3 flex items-center gap-4 md:gap-6">
        <span className="font-mono font-bold text-white tracking-tight shrink-0">DRB</span>

        {/* Desktop links */}
        <div className="hidden md:flex items-center gap-6 overflow-x-auto">
          {allLinks.map(({ href, label }) => (
            <Link key={href} href={href} className={navLinkClass(href)}>
              {label}
              {label === "Nodes" && pending.length > 0 && (
                <span className="ml-1.5 inline-flex items-center justify-center w-4 h-4 rounded-full bg-yellow-500 text-gray-950 text-xs font-bold">
                  {pending.length}
                </span>
              )}
              {label === "Alerts" && unackedAlerts.length > 0 && (
                <span className="ml-1.5 inline-flex items-center justify-center min-w-[1rem] h-4 rounded-full bg-red-600 text-white text-xs font-bold px-1">
                  {unackedAlerts.length}
                </span>
              )}
            </Link>
          ))}
        </div>

        <div className="ml-auto flex items-center gap-3 shrink-0">
          {/* Theme toggle */}
          <button
            onClick={toggle}
            className="text-gray-500 hover:text-gray-300 transition-colors"
            title={theme === "dark" ? "Switch to light mode" : "Switch to dark mode"}
          >
            {theme === "dark" ? <SunIcon /> : <MoonIcon />}
          </button>

          {/* Profile avatar (desktop) */}
          <button
            onClick={() => router.push("/profile")}
            className={`hidden md:flex items-center justify-center w-7 h-7 rounded-full text-xs font-bold transition-colors ${
              pathname.startsWith("/profile")
                ? "bg-indigo-600 text-white"
                : "bg-gray-800 text-gray-300 hover:bg-gray-700"
            }`}
            title="Profile"
          >
            {(user?.displayName || user?.email || "?")[0].toUpperCase()}
          </button>

          {/* Hamburger (mobile) */}
          <button
            onClick={() => setMobileOpen((v) => !v)}
            className="md:hidden text-gray-400 hover:text-gray-200 transition-colors p-1"
            aria-label="Toggle menu"
          >
            {mobileOpen ? (
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
                <line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>
              </svg>
            ) : (
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
                <line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="18" x2="21" y2="18"/>
              </svg>
            )}
          </button>
        </div>
      </div>

      {/* Mobile drawer */}
      {mobileOpen && (
        <div className="md:hidden border-t border-gray-800 bg-gray-950 px-4 py-3 flex flex-col gap-1">
          {allLinks.map(({ href, label }) => (
            <Link
              key={href}
              href={href}
              onClick={() => setMobileOpen(false)}
              className={`py-2 text-sm font-mono transition-colors flex items-center gap-2 ${
                pathname.startsWith(href) ? "text-white" : "text-gray-500"
              }`}
            >
              {label}
              {label === "Nodes" && pending.length > 0 && (
                <span className="inline-flex items-center justify-center w-4 h-4 rounded-full bg-yellow-500 text-gray-950 text-xs font-bold">
                  {pending.length}
                </span>
              )}
              {label === "Alerts" && unackedAlerts.length > 0 && (
                <span className="inline-flex items-center justify-center min-w-[1rem] h-4 rounded-full bg-red-600 text-white text-xs font-bold px-1">
                  {unackedAlerts.length}
                </span>
              )}
            </Link>
          ))}
          <div className="border-t border-gray-800 pt-3 mt-1">
            <Link
              href="/profile"
              onClick={() => setMobileOpen(false)}
              className={`py-2 text-sm font-mono transition-colors flex items-center gap-2 ${
                pathname.startsWith("/profile") ? "text-white" : "text-gray-500"
              }`}
            >
              Profile
            </Link>
          </div>
        </div>
      )}
    </nav>
  );
}
