import type { LucideIcon } from 'lucide-react';
import { usePanelChannel } from 'hooks/usePanelChannel';
import { LayoutGrid, Search, ArrowDownToLine, ArrowUpFromLine, Hash, MessageCircle, Users, Server } from 'lucide-react';
import { SOULSEEK_SECTION_CHANNEL, SOULSEEK_SECTIONS, type SoulseekSectionId } from './shared';

// Left panel of the /soulseek workspace — a vertical section menu mirroring slskd's top nav. Publishes
// the active section to the 'soulseek:section' channel; SoulseekView (right) renders the matching UI.

const ICONS: Record<SoulseekSectionId, LucideIcon> = {
  dashboard: LayoutGrid,
  search: Search,
  downloads: ArrowDownToLine,
  uploads: ArrowUpFromLine,
  rooms: Hash,
  chat: MessageCircle,
  users: Users,
  system: Server,
};

export const SoulseekNav = () => {
  const [section, setSection] = usePanelChannel<SoulseekSectionId>(SOULSEEK_SECTION_CHANNEL, 'dashboard');

  return (
    <div className="flex h-full flex-col overflow-y-auto bg-muted/30">
      <div className="flex items-center gap-3 px-4 py-4">
        <div className="flex h-9 w-9 items-center justify-center rounded-xl bg-white shadow-sm ring-1 ring-black/5">
          <img src="/slskd.png" alt="" className="h-5 w-5 object-contain" />
        </div>
        <div className="min-w-0">
          <div className="truncate text-sm font-semibold leading-tight">Soulseek</div>
          <div className="truncate text-xs text-muted-foreground">slskd client</div>
        </div>
      </div>

      <nav className="flex flex-col gap-0.5 px-2 pb-3">
        {SOULSEEK_SECTIONS.map(({ id, label }) => {
          const Icon = ICONS[id];
          const active = section === id;
          return (
            <button
              key={id}
              type="button"
              onClick={() => setSection(id)}
              className={`group relative flex items-center gap-3 rounded-lg px-3 py-2 text-left text-sm transition-colors ${
                active
                  ? 'bg-primary/10 font-medium text-primary'
                  : 'text-muted-foreground hover:bg-muted hover:text-foreground'
              }`}
            >
              {active && (
                <span className="absolute left-0 top-1/2 h-5 w-1 -translate-y-1/2 rounded-r-full bg-primary" />
              )}
              <Icon className={`h-4 w-4 shrink-0 ${active ? 'text-primary' : 'text-muted-foreground group-hover:text-foreground'}`} />
              {label}
            </button>
          );
        })}
      </nav>
    </div>
  );
};
