import { request, type ApiResponse } from '@/utils/request'

// 菜单树
export interface MenuTree {
  id: number
  parentId: number
  name: string
  path: string | null
  component: string | null
  icon: string | null
  menuType: number
  permission: string | null
  sortOrder: number
  status: number
  isExternal: boolean
  isCache: boolean
  children: MenuTree[]
}

// 创建菜单请求
export interface CreateMenuRequest {
  parentId: number
  name: string
  path?: string
  component?: string
  icon?: string
  menuType: number  // 1=目录, 2=菜单, 3=按钮
  permission?: string
  sortOrder: number
  status: number    // 1=显示, 0=隐藏
  isExternal: boolean
  isCache: boolean
}

// 更新菜单请求
export interface UpdateMenuRequest {
  parentId: number
  name: string
  path?: string
  component?: string
  icon?: string
  menuType: number  // 1=目录, 2=菜单, 3=按钮
  permission?: string
  sortOrder: number
  status: number    // 1=显示, 0=隐藏
  isExternal: boolean
  isCache: boolean
}

// 获取用户菜单
export function getUserMenus(): Promise<ApiResponse<MenuTree[]>> {
  return request({
    url: '/admin/menus/user',
    method: 'get'
  })
}

// 获取菜单树
export function getMenuTree(): Promise<ApiResponse<MenuTree[]>> {
  return request({
    url: '/admin/menus',
    method: 'get'
  })
}

// 获取菜单详情
export function getMenuById(id: number): Promise<ApiResponse<MenuTree>> {
  return request({
    url: `/admin/menus/${id}`,
    method: 'get'
  })
}

// 创建菜单
export function createMenu(data: CreateMenuRequest): Promise<ApiResponse<number>> {
  return request({
    url: '/admin/menus',
    method: 'post',
    data
  })
}

// 更新菜单
export function updateMenu(id: number, data: UpdateMenuRequest): Promise<ApiResponse<null>> {
  return request({
    url: `/admin/menus/${id}`,
    method: 'put',
    data
  })
}

// 删除菜单
export function deleteMenu(id: number): Promise<ApiResponse<null>> {
  return request({
    url: `/admin/menus/${id}`,
    method: 'delete'
  })
}
