import { create } from 'zustand';
import { persist } from 'zustand/middleware';

export type HotkeyContext = 'global' | 'modal' | 'form' | 'table' | 'page';

interface RecentCommand {
  id: string;
  label: string;
  timestamp: number;
}

interface HotkeyState {
  // Feature toggles
  hotkeysEnabled: boolean;

  // Command palette state
  commandPaletteOpen: boolean;

  // Shortcuts help modal
  shortcutsHelpOpen: boolean;

  // Current context (determines which shortcuts are active)
  activeContext: HotkeyContext;

  // Recent commands for command palette
  recentCommands: RecentCommand[];

  // Actions
  setHotkeysEnabled: (enabled: boolean) => void;
  toggleHotkeys: () => void;

  openCommandPalette: () => void;
  closeCommandPalette: () => void;
  toggleCommandPalette: () => void;

  openShortcutsHelp: () => void;
  closeShortcutsHelp: () => void;
  toggleShortcutsHelp: () => void;

  setActiveContext: (context: HotkeyContext) => void;

  addRecentCommand: (command: Omit<RecentCommand, 'timestamp'>) => void;
  clearRecentCommands: () => void;
}

const MAX_RECENT_COMMANDS = 5;

export const useHotkeyStore = create<HotkeyState>()(
  persist(
    (set) => ({
      hotkeysEnabled: true,
      commandPaletteOpen: false,
      shortcutsHelpOpen: false,
      activeContext: 'global',
      recentCommands: [],

      setHotkeysEnabled: (enabled) => set({ hotkeysEnabled: enabled }),

      toggleHotkeys: () =>
        set((state) => ({ hotkeysEnabled: !state.hotkeysEnabled })),

      openCommandPalette: () => set({ commandPaletteOpen: true }),

      closeCommandPalette: () => set({ commandPaletteOpen: false }),

      toggleCommandPalette: () =>
        set((state) => ({ commandPaletteOpen: !state.commandPaletteOpen })),

      openShortcutsHelp: () => set({ shortcutsHelpOpen: true }),

      closeShortcutsHelp: () => set({ shortcutsHelpOpen: false }),

      toggleShortcutsHelp: () =>
        set((state) => ({ shortcutsHelpOpen: !state.shortcutsHelpOpen })),

      setActiveContext: (context) => set({ activeContext: context }),

      addRecentCommand: (command) =>
        set((state) => {
          // Remove if already exists
          const filtered = state.recentCommands.filter(
            (c) => c.id !== command.id
          );
          // Add to beginning with timestamp
          const updated = [
            { ...command, timestamp: Date.now() },
            ...filtered,
          ].slice(0, MAX_RECENT_COMMANDS);
          return { recentCommands: updated };
        }),

      clearRecentCommands: () => set({ recentCommands: [] }),
    }),
    {
      name: 'books-hotkey-storage',
      partialize: (state) => ({
        hotkeysEnabled: state.hotkeysEnabled,
        recentCommands: state.recentCommands,
      }),
    }
  )
);

// Selector hooks
export const useHotkeysEnabled = () =>
  useHotkeyStore((state) => state.hotkeysEnabled);

export const useCommandPaletteOpen = () =>
  useHotkeyStore((state) => state.commandPaletteOpen);

export const useShortcutsHelpOpen = () =>
  useHotkeyStore((state) => state.shortcutsHelpOpen);

export const useActiveHotkeyContext = () =>
  useHotkeyStore((state) => state.activeContext);

export const useRecentCommands = () =>
  useHotkeyStore((state) => state.recentCommands);
