import {
  Bot,
  Database,
  FileArchive,
  FileImage,
  FileSignature,
  Files,
  FolderPlus,
  FolderOpen,
  Presentation,
  Settings,
  UserRound,
  Wand2
} from 'lucide-react'

import type { MenuItem } from '@renderer/types/app'

export const menuTree: MenuItem[] = [
  {
    id: 'tools',
    title: '小工具',
    icon: Wand2,
    children: [
      {
        id: 'tool-image-paths',
        title: '生成图片路径',
        path: '/tools/image-paths',
        icon: FileImage
      },
      { id: 'tool-comments', title: '生成评语', path: '/tools/comments', icon: Bot },
      { id: 'tool-reports', title: '生成报告', path: '/tools/reports', icon: Presentation },
      { id: 'tool-convert', title: '格式转换', path: '/tools/convert', icon: FileArchive },
      { id: 'tool-signature', title: '园长签名', path: '/tools/signature', icon: FileSignature }
    ]
  },
  {
    id: 'student',
    title: '学生管理',
    icon: Database,
    children: [
      { id: 'classes', title: '班级管理', path: '/student/classes', icon: FolderPlus },
      { id: 'students', title: '学生数据', path: '/student/list', icon: UserRound },
      { id: 'template', title: '模板管理', path: '/student/templates', icon: FolderOpen },
      { id: 'reports', title: '报告管理', path: '/student/reports', icon: Files },
      { id: 'backup', title: '备份恢复', path: '/student/backup', icon: FileArchive }
    ]
  },
  {
    id: 'settings',
    title: '设置',
    path: '/settings',
    icon: Settings
  }
]

export function findMenuItemByPath(
  pathname: string,
  menuItems: MenuItem[] = menuTree
): MenuItem | undefined {
  for (const item of menuItems) {
    if (item.path === pathname) {
      return item
    }

    const childMatch = item.children ? findMenuItemByPath(pathname, item.children) : undefined

    if (childMatch) {
      return childMatch
    }
  }

  return undefined
}

export function getOpenMenuIds(
  pathname: string,
  menuItems: MenuItem[] = menuTree,
  parents: string[] = []
): string[] {
  for (const item of menuItems) {
    const nextParents = [...parents, item.id]

    if (item.path === pathname) {
      return parents
    }

    if (item.children) {
      const childOpenIds = getOpenMenuIds(pathname, item.children, nextParents)

      if (childOpenIds.length > 0) {
        return childOpenIds
      }
    }
  }

  return []
}
