/**
 * Command registry contributions for the admin ⌘K palette (UX-3).
 *
 * Kept framework-free (pure data + callbacks) so the command set is
 * unit-testable in the node vitest env without rendering anything.
 */
import type { Command } from '@bytelyst/command-palette';

/** Curated navigate targets — the major admin surfaces. */
export const NAV_COMMANDS: ReadonlyArray<{
  id: string;
  label: string;
  href: string;
  keywords?: string[];
}> = [
  { id: 'nav-dashboard', label: 'Dashboard', href: '/', keywords: ['home', 'overview'] },
  { id: 'nav-users', label: 'Users', href: '/users', keywords: ['accounts', 'people'] },
  {
    id: 'nav-subscriptions',
    label: 'Subscriptions',
    href: '/subscriptions',
    keywords: ['plans', 'billing'],
  },
  { id: 'nav-licenses', label: 'Licenses', href: '/licenses', keywords: ['seats'] },
  { id: 'nav-billing', label: 'Billing', href: '/billing', keywords: ['invoices', 'payments'] },
  { id: 'nav-usage', label: 'Usage Analytics', href: '/usage', keywords: ['metrics', 'tokens'] },
  { id: 'nav-broadcasts', label: 'Broadcasts', href: '/broadcasts', keywords: ['announcements'] },
  { id: 'nav-surveys', label: 'Surveys', href: '/surveys', keywords: ['feedback', 'nps'] },
  { id: 'nav-flags', label: 'Feature Flags', href: '/flags', keywords: ['toggles', 'rollout'] },
  {
    id: 'nav-experiments',
    label: 'Experiments',
    href: '/experiments',
    keywords: ['ab', 'a/b', 'tests'],
  },
  { id: 'nav-audit', label: 'Audit Log', href: '/audit', keywords: ['history', 'events'] },
  { id: 'nav-products', label: 'Products', href: '/products' },
  { id: 'nav-invitations', label: 'Invitations', href: '/invitations', keywords: ['invites'] },
  { id: 'nav-promos', label: 'Promo Codes', href: '/promos', keywords: ['coupons', 'discounts'] },
  { id: 'nav-referrals', label: 'Referrals', href: '/referrals' },
  {
    id: 'nav-notifications',
    label: 'Notifications',
    href: '/notifications',
    keywords: ['alerts'],
  },
  { id: 'nav-organizations', label: 'Organizations', href: '/organizations', keywords: ['orgs'] },
  { id: 'nav-ops', label: 'Mission Control', href: '/ops', keywords: ['ops', 'operations'] },
  {
    id: 'nav-client-logs',
    label: 'Client Logs',
    href: '/ops/client-logs',
    keywords: ['telemetry', 'errors'],
  },
  { id: 'nav-extraction', label: 'Extraction', href: '/extraction' },
  { id: 'nav-settings', label: 'Settings', href: '/settings', keywords: ['config', 'preferences'] },
];

export interface AdminCommandActions {
  /** Theme-aware label uses this to read the current mode. */
  resolvedTheme?: 'light' | 'dark';
  /** Toggle light/dark theme. */
  toggleTheme: () => void;
  /** Sign the admin out and return to /login. */
  signOut: () => void;
}

/**
 * Build the full admin command set: navigate-mode entries for the major
 * surfaces plus the app-chrome actions (theme toggle, sign out).
 */
export function buildAdminCommands(actions: AdminCommandActions): Command[] {
  const navigate: Command[] = NAV_COMMANDS.map(n => ({
    id: n.id,
    label: n.label,
    mode: 'navigate',
    href: n.href,
    section: 'Go to',
    keywords: n.keywords,
  }));

  const chrome: Command[] = [
    {
      id: 'action-toggle-theme',
      label: actions.resolvedTheme === 'dark' ? 'Switch to light mode' : 'Switch to dark mode',
      mode: 'actions',
      section: 'Preferences',
      keywords: ['theme', 'dark', 'light', 'appearance'],
      run: actions.toggleTheme,
    },
    {
      id: 'action-sign-out',
      label: 'Sign out',
      mode: 'actions',
      section: 'Account',
      keywords: ['logout', 'log out', 'exit'],
      run: actions.signOut,
    },
  ];

  return [...navigate, ...chrome];
}
