import React from 'react';
import { useNavigate } from 'react-router';
import { useAuth } from './hooks/AuthProvider';
import {
  Users,
  Settings,
  User,
  LogOut,
  BarChart3,
  LayoutDashboard,
  File,
  Megaphone,
  Tag,
  Package,
  Truck,
  Building,
  UserCheck,
  CreditCard,
  TrendingUp,
  MapPin,
  Printer,
} from 'lucide-react';

export interface MenuItem {
  key: string;
  label: string;
  icon?: React.ReactNode;
  children?: MenuItem[];
  path?: string;
  permission?: string;
  onClick?: () => void;
}

/**
 * 菜单搜索 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 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: 'analytics',
    //   label: '数据分析',
    //   icon: <BarChart3 className="h-4 w-4" />,
    //   path: '/admin/analytics',
    //   permission: 'analytics:view'
    // },
    {
      key: 'advertisements',
      label: '广告管理',
      icon: <Megaphone className="h-4 w-4" />,
      permission: 'advertisement:manage',
      children: [
        {
          key: 'advertisements-list',
          label: '广告列表',
          path: '/admin/advertisements',
          permission: 'advertisement:manage'
        },
        {
          key: 'advertisement-types',
          label: '广告类型',
          path: '/admin/advertisement-types',
          permission: 'advertisement:manage'
        }
      ]
    },
    {
      key: 'goods',
      label: '商品管理',
      icon: <Package className="h-4 w-4" />,
      permission: 'goods:manage',
      children: [
        {
          key: 'goods-list',
          label: '商品列表',
          path: '/admin/goods',
          permission: 'goods:manage'
        },
        {
          key: 'goods-categories',
          label: '商品分类',
          path: '/admin/goods-categories',
          permission: 'goods:manage'
        },
        // {
        //   key: 'express-companies',
        //   label: '快递公司',
        //   path: '/admin/express-companies',
        //   permission: 'goods:manage'
        // },
      ]
    },
    {
      key: 'orders',
      label: '订单管理',
      icon: <Truck className="h-4 w-4" />,
      permission: 'order:manage',
      children: [
        {
          key: 'orders-list',
          label: '订单列表',
          path: '/admin/orders',
          permission: 'order:manage'
        }
      ]
    },
    {
      key: 'suppliers',
      label: '供应商管理',
      icon: <Building className="h-4 w-4" />,
      path: '/admin/suppliers',
      permission: 'supplier:manage'
    },
    {
      key: 'merchants',
      label: '商户管理',
      icon: <Building className="h-4 w-4" />,
      path: '/admin/merchants',
      permission: 'merchant:manage'
    },
    // {
    //   key: 'agents',
    //   label: '代理商管理',
    //   icon: <UserCheck className="h-4 w-4" />,
    //   path: '/admin/agents',
    //   permission: 'agent:manage'
    // },
    {
      key: 'areas',
      label: '区域管理',
      icon: <MapPin className="h-4 w-4" />,
      path: '/admin/areas',
      permission: 'area:manage'
    },
    {
      key: 'delivery-addresses',
      label: '收货地址',
      icon: <MapPin className="h-4 w-4" />,
      path: '/admin/delivery-addresses',
      permission: 'user:manage'
    },
    // {
    //   key: 'cards',
    //   label: '卡券管理',
    //   icon: <CreditCard className="h-4 w-4" />,
    //   permission: 'card:manage',
    //   children: [
    //     {
    //       key: 'user-cards',
    //       label: '用户卡管理',
    //       path: '/admin/user-cards',
    //       permission: 'card:manage'
    //     },
    //     {
    //       key: 'user-card-balance-records',
    //       label: '余额记录',
    //       path: '/admin/user-card-balance-records',
    //       permission: 'card:manage'
    //     }
    //   ]
    // },
    {
      key: 'printing',
      label: '打印管理',
      icon: <Printer className="h-4 w-4" />,
      permission: 'printing:manage',
      children: [
        {
          key: 'printers',
          label: '打印机管理',
          path: '/admin/printers',
          permission: 'printing:manage'
        },
        {
          key: 'print-tasks',
          label: '打印任务',
          path: '/admin/print-tasks',
          permission: 'printing:manage'
        },
        {
          key: 'print-configs',
          label: '打印配置',
          path: '/admin/print-configs',
          permission: 'printing:manage'
        }
      ]
    },
    {
      key: 'system-configs',
      label: '系统配置',
      icon: <Settings className="h-4 w-4" />,
      path: '/admin/system-configs',
      permission: 'system-config:manage'
    },
  ];

  // 用户菜单项
  const userMenuItems = [
    // {
    //   key: 'profile',
    //   label: '个人资料',
    //   icon: <User className="mr-2 h-4 w-4" />,
    //   onClick: () => navigate('/admin/profile')
    // },
    // {
    //   key: 'settings',
    //   label: '账户设置',
    //   icon: <Settings className="mr-2 h-4 w-4" />,
    //   onClick: () => navigate('/admin/account-settings')
    // },
    // {
    //   type: 'separator',
    //   key: 'divider',
    // },
    {
      key: 'logout',
      label: '退出登录',
      icon: <LogOut className="mr-2 h-4 w-4" />,
      onClick: () => handleLogout()
    }
  ];

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

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