import { useMemo } from 'react';
import {
  Activity,
  ArrowUpRight,
  BellOff,
  BellRing,
  CalendarClock,
  Download,
  Pin,
  PinOff,
  Play,
  RefreshCw,
  RotateCw,
  Square,
  Tag,
  Trash2,
  ArrowDownToLine,
} from 'lucide-react';
import type { MenuGroup, MenuItem, StackMenuCtx } from '@/components/sidebar/sidebar-types';

export function useStackMenuItems(_file: string, ctx: StackMenuCtx): MenuGroup[] {
  const {
    stackStatus, isSelfStack, canOpenApp, isBusy, canDelete, canDeploy, canEditLabels, isPinned, labels,
    openAlertSheet, openAutoHeal, canViewMonitor, canCheckUpdates, checkUpdates, openStackApp,
    deploy, stop, restart, update, takeDown, remove, pin, unpin, toggleLabel,
    menuVisibility, openScheduleTask,
    canMuteNotifications, muteStackAll, muteStackDeploySuccess, muteStackMonitor, openStackMuteRules,
    ready,
  } = ctx;
  const { showDeploy, showStop, showRestart, showUpdate, showTakeDown } = menuVisibility;

  return useMemo(() => {
    const groups: MenuGroup[] = [];

    const inspect: MenuItem[] = [];
    if (canViewMonitor) {
      inspect.push({ id: 'alerts', label: 'Alerts', icon: BellRing, shortcut: 'A', onSelect: openAlertSheet });
      inspect.push({ id: 'auto-heal', label: 'Auto-Heal', icon: Activity, shortcut: 'H', onSelect: openAutoHeal });
    }
    if (canCheckUpdates) {
      inspect.push({ id: 'check-updates', label: 'Check updates', icon: RefreshCw, shortcut: 'U', onSelect: checkUpdates });
    }
    if (stackStatus === 'running' && canOpenApp) {
      inspect.push({ id: 'open-app', label: 'Open App', icon: ArrowUpRight, shortcut: '↗', onSelect: openStackApp });
    }
    if (canMuteNotifications) {
      inspect.push({
        id: 'mute',
        label: 'Mute',
        icon: BellOff,
        onSelect: () => {},
        subItems: [
          { id: 'mute-stack-all', label: 'Mute notifications for this stack', icon: BellOff, onSelect: muteStackAll },
          { id: 'mute-stack-deploy', label: 'Mute deploy success noise', icon: BellOff, onSelect: muteStackDeploySuccess },
          { id: 'mute-stack-monitor', label: 'Mute monitor alerts for this stack', icon: BellOff, onSelect: muteStackMonitor },
          { id: 'mute-stack-manage', label: 'Manage stack mute rules', icon: BellOff, onSelect: openStackMuteRules },
        ],
      });
    }
    groups.push({ id: 'inspect', items: inspect });

    const organize: MenuItem[] = [];
    if (canEditLabels) {
      organize.push({
        id: 'labels',
        label: 'Labels',
        icon: Tag,
        shortcut: 'L ›',
        onSelect: () => {},
        subItems: labels.map(l => ({
          id: `label:${l.id}`,
          label: l.name,
          icon: Tag,
          onSelect: () => toggleLabel(l.id),
        })),
      });
    }
    organize.push(
      isPinned
        ? { id: 'pin', label: 'Unpin', icon: PinOff, shortcut: 'P', onSelect: unpin }
        : { id: 'pin', label: 'Pin to top', icon: Pin, shortcut: 'P', onSelect: pin }
    );
    groups.push({ id: 'organize', items: organize });

    const lifecycle: MenuItem[] = [];
    if (canDeploy) {
      if (showDeploy) lifecycle.push({ id: 'deploy', label: 'Deploy', icon: Play, shortcut: '⌘↵', onSelect: deploy, disabled: isBusy });
      if (showStop) lifecycle.push({ id: 'stop', label: 'Stop', icon: Square, shortcut: '⌘.', onSelect: stop, disabled: isBusy });
      if (showRestart) lifecycle.push({ id: 'restart', label: 'Restart', icon: RotateCw, shortcut: '⌘R', onSelect: restart, disabled: isBusy });
      if (showUpdate) lifecycle.push({ id: 'update', label: 'Update', icon: Download, shortcut: '⌘↑', onSelect: update, disabled: isBusy });
      if (showTakeDown) lifecycle.push({ id: 'take-down', label: 'Take down', icon: ArrowDownToLine, shortcut: '⌘↓', onSelect: takeDown, disabled: isBusy || isSelfStack });
    }
    if (canDeploy && ready) lifecycle.push({ id: 'schedule', label: 'Schedule task', icon: CalendarClock, onSelect: openScheduleTask });
    if (lifecycle.length > 0) groups.push({ id: 'lifecycle', items: lifecycle });

    if (canDelete && ready) {
      groups.push({
        id: 'destructive',
        items: [{
          id: 'delete',
          label: 'Delete',
          icon: Trash2,
          shortcut: '⌘⌫',
          destructive: true,
          disabled: isSelfStack,
          onSelect: remove,
        }],
      });
    }

    return groups;
  }, [
    stackStatus, isSelfStack, canOpenApp, isBusy, canDelete, canDeploy, canEditLabels, isPinned, labels,
    ready, showDeploy, showStop, showRestart, showUpdate, showTakeDown,
    openAlertSheet, openAutoHeal, canViewMonitor, canCheckUpdates, checkUpdates, openStackApp,
    deploy, stop, restart, update, takeDown, remove, pin, unpin, toggleLabel, openScheduleTask,
    canMuteNotifications, muteStackAll, muteStackDeploySuccess, muteStackMonitor, openStackMuteRules,
  ]);
}
