/**
 * CommandPalette — 快速搜索面板 (Cmd+K)
 */

import { useState, useEffect, useRef, useCallback, useMemo } from 'react';
import { Dialog, DialogContent, InputBase, List, ListItemButton, ListItemIcon, ListItemText, Typography, Box, Divider } from '@mui/material';
import { Search, MessageSquare, Settings, Plus, Trash2 } from 'lucide-react';
import { useUIStore } from '@renderer/stores/ui-store';
import { useSessionStore } from '@renderer/stores/session-store';
import { useAgentStore } from '@renderer/stores/agent-store';

interface CommandItem { id: string; icon: typeof Search; label: string; description?: string; action: () => void; }

export function CommandPalette(): React.JSX.Element {
  const [open, setOpen] = useState(false);
  const [query, setQuery] = useState('');
  const [selectedIndex, setSelectedIndex] = useState(0);
  const inputRef = useRef<HTMLInputElement>(null);
  // 审计补充修复: 响应式订阅 sessions，使 getCommands 在 sessions 变化时重新构建
  // 原问题：getCommands 内部通过 useSessionStore.getState() 非响应式访问 sessions，
  // 当 sessions 变化但 query 不变时 commands 列表陈旧
  const sessions = useSessionStore((s) => s.sessions);

  useEffect(() => {
    const h = (e: KeyboardEvent) => { if (e.key === 'k' && (e.ctrlKey || e.metaKey)) { e.preventDefault(); setOpen((p) => !p); setQuery(''); setSelectedIndex(0); } };
    window.addEventListener('keydown', h); return () => window.removeEventListener('keydown', h);
  }, []);

  // M-19 修复: useEffect 返回 cleanup 清理 setTimeout，防止快速开关时 timer 堆积
  useEffect(() => {
    if (!open) return;
    const t = setTimeout(() => inputRef.current?.focus(), 50);
    return () => clearTimeout(t);
  }, [open]);

  const getCommands = useCallback((): CommandItem[] => {
    const cmds: CommandItem[] = [
      { id: 'new-session', icon: Plus, label: '新建会话', description: '创建一个新的对话会话', action: async () => {
        if (window.metona?.sessions?.create) {
          try {
            const r = await window.metona.sessions.create() as MetonaSessionInfo;
            useSessionStore.getState().addSession(r);
            useSessionStore.getState().setCurrentSession(r.id);
            useAgentStore.getState().setCurrentSession(r.id);
          } catch (err) {
            console.error('[CommandPalette]', err);
            import('@metona-team/metona-toast').then((mod) => mod.default.error('创建会话失败')).catch(() => {});
          }
        }
        setOpen(false);
      } },
      { id: 'clear', icon: Trash2, label: '清空当前会话', action: () => { useAgentStore.getState().clearMessages(); setOpen(false); } },
      { id: 'settings', icon: Settings, label: '打开设置', action: () => { useUIStore.getState().openSettings(); setOpen(false); } },
    ];
    if (query) {
      // 审计补充修复: 使用响应式 sessions（闭包捕获），而非 useSessionStore.getState()
      const filtered = sessions.filter((s) => s.title.toLowerCase().includes(query.toLowerCase()));
      for (const s of filtered.slice(0, 5)) cmds.push({ id: `s-${s.id}`, icon: MessageSquare, label: s.title, description: `${s.messageCount} 条消息`, action: () => { useSessionStore.getState().setCurrentSession(s.id); useAgentStore.getState().setCurrentSession(s.id); setOpen(false); } });
    }
    return cmds;
  }, [query, sessions]);

  // L-15 修复: 使用 useMemo 缓存 commands 列表，避免每次渲染都重新构建
  const commands = useMemo(() => getCommands(), [getCommands]);

  return (
    <Dialog open={open} onClose={() => setOpen(false)} maxWidth="sm" fullWidth slotProps={{ paper: { sx: { mt: '20vh', maxWidth: 520, borderRadius: 3, overflow: 'hidden' } } }}>
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 1.5, px: 2, py: 1.5, borderBottom: 1, borderColor: 'divider' }}>
        <Search size={16} style={{ color: '#8b8fa7', flexShrink: 0 }} />
        <InputBase inputRef={inputRef} value={query} onChange={(e) => { setQuery(e.target.value); setSelectedIndex(0); }}
          onKeyDown={(e) => {
            if (e.key === 'ArrowDown') { e.preventDefault(); setSelectedIndex((p) => Math.min(p + 1, commands.length - 1)); }
            else if (e.key === 'ArrowUp') { e.preventDefault(); setSelectedIndex((p) => Math.max(p - 1, 0)); }
            else if (e.key === 'Enter') { e.preventDefault(); commands[selectedIndex]?.action(); }
          }}
          placeholder="搜索会话、命令..." sx={{ flex: 1, fontSize: 13 }}
        />
      </Box>
      <List sx={{ maxHeight: 300, overflowY: 'auto', py: 0.5 }}>
        {commands.length === 0 ? (
          <Typography variant="caption" sx={{ textAlign: 'center', py: 3, display: 'block', color: 'text.secondary' }}>无匹配结果</Typography>
        ) : commands.map((cmd, i) => {
          const Icon = cmd.icon;
          return (
            <ListItemButton key={cmd.id} selected={i === selectedIndex} onClick={cmd.action} sx={{ px: 2, py: 1.25 }}>
              <ListItemIcon sx={{ minWidth: 32 }}><Icon size={14} style={{ color: '#818cf8' }} /></ListItemIcon>
              <ListItemText primary={<Typography variant="body2" sx={{ fontSize: 12 }}>{cmd.label}</Typography>} secondary={cmd.description ? <Typography variant="caption">{cmd.description}</Typography> : undefined} />
            </ListItemButton>
          );
        })}
      </List>
      <Divider />
      <Box sx={{ display: 'flex', gap: 2, px: 2, py: 1, fontSize: 10, color: 'text.disabled' }}>
        <span>↑↓ 导航</span><span>↵ 选择</span><span>Esc 关闭</span>
      </Box>
    </Dialog>
  );
}
