/**
 * Pure command-registry builder for the ⌘K command palette (UX-5).
 *
 * Kept free of React/Next imports so it is unit-testable in a node
 * environment and reusable by the client `CommandMenu` shell.
 *
 * @see docs/roadmaps/UX_INTEGRATION_BYTELYST.md (UX-5)
 */

import type { Command } from '@bytelyst/command-palette';

export interface CommandMenuDeps {
  /** Navigate to an in-app route. */
  navigate: (href: string) => void;
  /** Open the "create item" flow. */
  newItem: () => void;
  /** Flip the colour theme. */
  toggleTheme: () => void;
  /** Sign the current user out. */
  signOut: () => void;
  /** Switch the active product (wires ProductSwitcher). */
  setProduct: (id: string) => void;
  /** Known products to expose as switch targets. */
  products: ReadonlyArray<{ id: string; name: string }>;
}

const navCommand = (id: string, label: string, href: string): Command => ({
  id,
  label,
  mode: 'navigate',
  href,
  section: 'Navigate',
  keywords: ['go', 'open', label.toLowerCase()],
});

/** Build the full command list for the palette from the supplied callbacks. */
export function buildCommands(deps: CommandMenuDeps): Command[] {
  return [
    navCommand('nav-overview', 'Overview', '/dashboard'),
    navCommand('nav-items', 'Items', '/dashboard/items'),
    navCommand('nav-board', 'Board', '/dashboard/board'),
    navCommand('nav-roadmap', 'Roadmap', '/roadmap'),
    {
      id: 'new-item',
      label: 'New item',
      section: 'Actions',
      keywords: ['create', 'add', 'item'],
      run: deps.newItem,
    },
    {
      id: 'toggle-theme',
      label: 'Toggle theme',
      section: 'Actions',
      keywords: ['dark', 'light', 'appearance'],
      run: deps.toggleTheme,
    },
    {
      id: 'sign-out',
      label: 'Sign out',
      section: 'Actions',
      keywords: ['logout', 'log out'],
      run: deps.signOut,
    },
    ...deps.products.map(
      (p): Command => ({
        id: `switch-product-${p.id}`,
        label: `Switch product: ${p.name}`,
        section: 'Product',
        keywords: ['product', 'switch', p.name.toLowerCase()],
        run: () => deps.setProduct(p.id),
      })
    ),
  ];
}
