import React from 'react';
import { useNavigate } from 'react-router';
import { useAuth } from './hooks/AuthProvider';
import type { MenuProps } from 'antd';
import {
  User,
  LayoutDashboard,
  Users,
  Info,
  Database,
  FileText,
  LineChart,
  Code,
  Calendar,
  File,
  MessageSquare,
  Video
} from 'lucide-react';

export interface MenuItem {
  key: string;
  label: string;
  icon?: React.ReactNode;
  children?: MenuItem[];
  path?: string;
  permission?: string;
}

/**
 * 菜单搜索 Hook
 * 封装菜单搜索相关逻辑
 */
export const useMenuSearch = (menuItems: MenuItem[]) => {
  const [searchText, setSearchText] = React.useState('');

  // 过滤菜单项
  const filteredMenuItems = React.useMemo(() => {
    if (!searchText) return menuItems;
    
    const filterItems = (items: MenuItem[]): MenuItem[] => {
      return items
        .map(item => {
          // 克隆对象避免修改原数据
          const newItem = { ...item };
          if (newItem.children) {
            newItem.children = filterItems(newItem.children);
          }
          return newItem;
        })
        .filter(item => {
          // 保留匹配项或其子项匹配的项
          const match = item.label.toLowerCase().includes(searchText.toLowerCase());
          if (match) return true;
          if (item.children?.length) return true;
          return false;
        });
    };
    
    return filterItems(menuItems);
  }, [menuItems, searchText]);

  // 清除搜索
  const clearSearch = () => {
    setSearchText('');
  };

  return {
    searchText,
    setSearchText,
    filteredMenuItems,
    clearSearch
  };
};

export const useMenu = () => {
  const navigate = useNavigate();
  const { logout: handleLogout } = useAuth();
  const [collapsed, setCollapsed] = React.useState(false);
  const [openKeys, setOpenKeys] = React.useState<string[]>([]);

  // 基础菜单项配置
  const menuItems: MenuItem[] = [
    {
      key: 'dashboard',
      label: '控制台',
      icon: <LayoutDashboard className="h-4 w-4" />,
      path: '/admin/dashboard'
    },
    {
      key: 'users',
      label: '用户管理',
      icon: <Users className="h-4 w-4" />,
      path: '/admin/users',
      permission: 'user:manage'
    },
    {
      key: 'files',
      label: '文件管理',
      icon: <File className="h-4 w-4" />,
      path: '/admin/files',
      permission: 'file:manage'
    },
    {
      key: 'classroom-data',
      label: '教室数据管理',
      icon: <Database className="h-4 w-4" />,
      path: '/admin/classroom-data',
      permission: 'classroom:manage'
    },
    {
      key: 'submission-records',
      label: '提交记录管理',
      icon: <FileText className="h-4 w-4" />,
      path: '/admin/submission-records',
      permission: 'submission:manage'
    },
    {
      key: 'stock-data',
      label: '股票数据管理',
      icon: <LineChart className="h-4 w-4" />,
      path: '/admin/stock-data',
      permission: 'stock:manage'
    },
    {
      key: 'stock-xunlian-codes',
      label: '训练代码管理',
      icon: <Code className="h-4 w-4" />,
      path: '/admin/stock-xunlian-codes',
      permission: 'stock:manage'
    },
    {
      key: 'date-notes',
      label: '日期备注管理',
      icon: <Calendar className="h-4 w-4" />,
      path: '/admin/date-notes',
      permission: 'stock:manage'
    },
    {
      key: 'chat-messages',
      label: '聊天消息管理',
      icon: <MessageSquare className="h-4 w-4" />,
      path: '/admin/chat-messages',
      permission: 'chat:manage'
    },
    {
      key: 'vod-videos',
      label: '视频管理',
      icon: <Video className="h-4 w-4" />,
      path: '/admin/vod-videos',
      permission: 'vod:manage'
    }
  ];

  // 用户菜单项
  const userMenuItems: MenuProps['items'] = [
    {
      key: 'profile',
      label: '个人资料',
      icon: <User className="h-4 w-4" />
    },
    {
      key: 'logout',
      label: '退出登录',
      icon: <Info className="h-4 w-4" />,
      danger: true,
      onClick: () => handleLogout()
    }
  ];

  // 处理菜单点击
  const handleMenuClick = (item: MenuItem) => {
    if (item.path) {
      navigate(item.path);
    }
  };

  // 处理菜单展开变化
  const onOpenChange = (keys: string[]) => {
    const latestOpenKey = keys.find(key => openKeys.indexOf(key) === -1);
    setOpenKeys(latestOpenKey ? [latestOpenKey] : []);
  };

  return {
    menuItems,
    userMenuItems,
    openKeys,
    collapsed,
    setCollapsed,
    handleMenuClick,
    onOpenChange
  };
};