import { Clipboard, LogOut, Monitor, Moon, Sun } from 'lucide-react';
import type { LucideIcon } from 'lucide-react';

import { getVisibleAdminGroups } from '@/config/admin-navigation';
import { getVisibleNavItems } from '@/config/navigation';
import { getSiteNavItems } from '@/config/site-navigation';
import type { Features, SiteSummary } from '@/types';

export interface CommandItem {
  id: string;
  label: string;
  icon: LucideIcon;
  group: string;
  shortcut?: string;
  action: () => void;
}

interface BuildCommandsOptions {
  features: Features;
  resolvedTheme: 'light' | 'dark';
  setTheme: (theme: 'light' | 'dark' | 'system') => void;
  navigate: (href: string) => void;
  close: () => void;
  currentSite?: SiteSummary | null;
}

export function buildCommands({
  features,
  resolvedTheme: _resolvedTheme,
  setTheme,
  navigate,
  close,
  currentSite,
}: BuildCommandsOptions): CommandItem[] {
  const commands: CommandItem[] = [];

  // Navigation commands from shared config
  const navItems = getVisibleNavItems(features);
  for (const item of navItems) {
    commands.push({
      id: `nav-${item.href}`,
      label: item.label,
      icon: item.icon,
      group: 'Navigation',
      shortcut: item.shortcut,
      action: () => {
        navigate(item.href);
        close();
      },
    });
  }

  // Site-scoped commands — Cmd+K destinations for the current site's nav items.
  // Disabled items (e.g. analysis-dependent before first run) are excluded so
  // the palette only routes to functional pages.
  if (currentSite) {
    const siteId = currentSite.id;
    const siteGroups = getSiteNavItems(currentSite);
    for (const group of siteGroups) {
      for (const item of group.items) {
        if (item.disabled) continue;
        commands.push({
          id: `site-${item.key}`,
          label: `${currentSite.name}: ${item.label}`,
          icon: item.icon,
          group: 'Site',
          action: () => {
            navigate(route(item.routeName, siteId));
            close();
          },
        });
      }
    }
  }

  // Admin navigation commands (only for admin users who can see admin features)
  if (features.admin) {
    const adminGroups = getVisibleAdminGroups(features);
    for (const group of adminGroups) {
      for (const item of group.items) {
        commands.push({
          id: `admin-${item.href}`,
          label: `Admin: ${item.label}`,
          icon: item.icon,
          group: 'Admin',
          action: () => {
            navigate(item.href);
            close();
          },
        });
      }
    }
  }

  // Action commands
  commands.push({
    id: 'action-copy-url',
    label: 'Copy Current URL',
    icon: Clipboard,
    group: 'Actions',
    action: () => {
      navigator.clipboard.writeText(window.location.href);
      close();
    },
  });

  commands.push({
    id: 'action-logout',
    label: 'Log Out',
    icon: LogOut,
    group: 'Actions',
    action: () => {
      // This will be handled by the component using router.post
      close();
    },
  });

  // Theme commands
  commands.push({
    id: 'theme-light',
    label: 'Light Mode',
    icon: Sun,
    group: 'Theme',
    action: () => {
      setTheme('light');
      close();
    },
  });

  commands.push({
    id: 'theme-dark',
    label: 'Dark Mode',
    icon: Moon,
    group: 'Theme',
    action: () => {
      setTheme('dark');
      close();
    },
  });

  commands.push({
    id: 'theme-system',
    label: 'System Theme',
    icon: Monitor,
    group: 'Theme',
    action: () => {
      setTheme('system');
      close();
    },
  });

  return commands;
}
