// Arbiter — centralized command registry bootstrap.
// Registers navigation + domain commands on the global ⌘K palette backend
// (Arbiter.CommandRegistry) at page load. Individual Platform components can
// still register their own context-specific commands via Arbiter.useCommand().
//
// Zero UX loss: this file only adds commands. The existing CommandPalette UI
// and ⌘K keybinding keep working unchanged; they simply gain more targets.

(function () {
  const A = window.Arbiter;
  if (!A || !A.CommandRegistry) return;
  const R = A.CommandRegistry;

  // Navigation targets (all top-level views from Arbiter.html)
  const nav = [
    ['dashboard',     'Go to Dashboard'],
    ['matters',       'Go to Matters'],
    ['documents',     'Go to Documents'],
    ['calendar',      'Go to Calendar'],
    ['research',      'Go to Research'],
    ['billing',       'Go to Billing'],
    ['discovery',     'Go to Discovery'],
    ['nexus',         'Go to Nexus'],
    ['persona',       'Go to Persona'],
    ['criminal',      'Go to Criminal'],
    ['motions',       'Go to Motions'],
    ['studio',        'Go to Studio'],
    ['auto',          'Go to Automation'],
    ['tasks',         'Go to Tasks'],
    ['rulebook',      'Go to Rulebook'],
    ['jurisdictions', 'Go to Jurisdictions'],
    ['esign',         'Go to SignDesk'],
    ['risk',          'Go to Risk'],
    ['settings',      'Go to Settings'],
    ['comms',         'Go to Communications'],
    ['team',          'Go to Team'],
    ['clients',       'Go to Clients'],
  ];
  nav.forEach(([id, label]) => R.register(`nav.${id}`, {
    label, group: 'Navigation', keywords: id,
    run: () => {
      window.dispatchEvent(new CustomEvent('arbiter:navigate', { detail: { view: id } }));
      A.track('command.run', { id: `nav.${id}` });
    },
  }));

  // Create-new actions
  const creates = [
    ['matter',   'New matter',    { view: 'matters' }],
    ['task',     'New task',      { view: 'tasks' }],
    ['document', 'New document',  { view: 'documents' }],
    ['event',    'New calendar event', { view: 'calendar' }],
    ['risk',     'New risk',      { view: 'risk' }],
    ['motion',   'New motion',    { view: 'motions' }],
  ];
  creates.forEach(([id, label, payload]) => R.register(`new.${id}`, {
    label, group: 'Create', keywords: `create add ${id}`,
    run: () => {
      window.dispatchEvent(new CustomEvent('arbiter:create', { detail: { kind: id, ...payload } }));
      A.track('command.run', { id: `new.${id}` });
    },
  }));

  // Theme & density
  R.register('theme.toggle', {
    label: 'Toggle dark / light mode', group: 'Preferences', keywords: 'theme dark light',
    run: () => window.ArbiterTheme.setMode(window.ArbiterTheme.mode === 'dark' ? 'light' : 'dark'),
  });
  ['compact', 'comfortable', 'touch'].forEach(d => R.register(`density.${d}`, {
    label: `Density: ${d[0].toUpperCase()}${d.slice(1)}`, group: 'Preferences', keywords: `density ${d}`,
    run: () => window.ArbiterTheme.setDensity(d),
  }));

  // Utility
  R.register('export.csv', {
    label: 'Export current view as CSV', group: 'Export', keywords: 'download csv export',
    run: () => window.dispatchEvent(new CustomEvent('arbiter:export', { detail: { format: 'csv' } })),
  });
  R.register('print.page', {
    label: 'Print current view', group: 'Export', keywords: 'print pdf',
    run: () => window.print(),
  });
  R.register('help.shortcuts', {
    label: 'Show keyboard shortcuts', group: 'Help', keywords: 'help shortcuts keys',
    run: () => A.toast({ title: 'Keyboard shortcuts',
      message: 'cmd+K palette · esc closes · ? opens help · tab navigates', duration: 6000 }),
  });

  // Expose a small adapter so the existing App can subscribe without refactor.
  // App wires: window.addEventListener('arbiter:navigate', e => setActiveView(e.detail.view))
  window.ArbiterCommands = { navEvents: 'arbiter:navigate', createEvents: 'arbiter:create' };
})();
