"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { House, Search, Settings, Sparkles, FolderKanban, ShieldCheck, MessageCircle, Brain } from "lucide-react";
import { PRODUCT_NAME } from "@/lib/product-config";
import { isFeatureEnabled } from "@/lib/feature-flags";

const navItems: { href: string; label: string; icon: typeof House; flag?: string }[] = [
  { href: "/dashboard", label: "Dashboard", icon: House },
  { href: "/workspaces", label: "Workspaces", icon: FolderKanban },
  { href: "/reviews", label: "Reviews", icon: ShieldCheck, flag: "mcp_tools_enabled" },
  { href: "/prompts", label: "Prompts", icon: Sparkles },
  { href: "/search", label: "Search", icon: Search },
  { href: "/chat", label: "Workspace chat", icon: MessageCircle },
  { href: "/palace", label: "Palace", icon: Brain },
  { href: "/settings", label: "Settings", icon: Settings },
];

export function Sidebar({ open }: { open?: boolean }) {
  const pathname = usePathname() ?? "";

  return (
    <aside className={`sidebar app-sidebar${open ? ' open' : ''}`} style={{ padding: "var(--nl-space-6)" }} aria-label="Primary">
      <div style={{ display: "grid", gap: "var(--nl-space-6)" }}>
        <div className="surface-card" style={{ padding: "var(--nl-space-5)", display: "grid", gap: "var(--nl-space-3)" }}>
          <div className="badge" style={{ width: "fit-content" }}>
            <Sparkles size={14} />
            Web Agent workstream
          </div>
          <div>
            <div style={{ fontSize: "var(--nl-fs-xl)", fontFamily: "var(--nl-font-display)", fontWeight: 700 }}>{PRODUCT_NAME}</div>
            <div style={{ color: "var(--nl-text-secondary)", marginTop: 6 }}>
              Human-first notes UX with agent-safe structure and review surfaces.
            </div>
          </div>
        </div>

        <nav aria-label="Primary navigation" style={{ display: "grid", gap: "var(--nl-space-2)" }}>
          {navItems.filter((item) => !item.flag || isFeatureEnabled(item.flag)).map((item) => {
            const Icon = item.icon;
            const active = pathname === item.href || pathname.startsWith(`${item.href}/`);
            return (
              <Link
                key={item.href}
                href={item.href}
                className="surface-muted nav-link"
                aria-current={active ? "page" : undefined}
                style={{
                  display: "flex",
                  alignItems: "center",
                  gap: "var(--nl-space-3)",
                  padding: "12px 14px",
                  borderColor: active ? "var(--nl-accent-primary)" : undefined,
                  background: active ? "rgba(90, 140, 255, 0.16)" : undefined,
                }}
              >
                <Icon size={18} />
                <span>{item.label}</span>
              </Link>
            );
          })}
        </nav>

        <div className="surface-card" style={{ padding: "var(--nl-space-5)", display: "grid", gap: "var(--nl-space-2)" }}>
          <strong>Keyboard flow</strong>
          <span style={{ color: "var(--nl-text-secondary)" }}>
            <kbd style={{ opacity: 0.85 }}>⌘K</kbd> / <kbd style={{ opacity: 0.85 }}>Ctrl+K</kbd> command palette
          </span>
          <span style={{ color: "var(--nl-text-secondary)" }}>Use Tab to move between navigation, filters, and dense result surfaces.</span>
          <span style={{ color: "var(--nl-text-secondary)" }}>Use the skip link to jump directly into page content.</span>
        </div>
      </div>
    </aside>
  );
}
