/**
 * @description 菜单相关API
 * @author yslg
 * @since 2025-10-06
 */

import { api } from '@/apis/index';
import type { SysMenu, MenuTreeNode, SysPermission, ResultDomain } from '@/types';

/**
 * 菜单API服务
 */
export const menuApi = {
  /**
   * 获取当前用户的菜单树
   * @returns Promise<ResultDomain<MenuTreeNode>>
   */
  async getCurrentUserMenuTree(): Promise<ResultDomain<MenuTreeNode>> {
    const response = await api.get<MenuTreeNode>('/menus/user-tree');
    return response.data;
  },

  /**
   * 获取所有菜单列表
   * @returns Promise<ResultDomain<SysMenu>>
   */
  async getAllMenuList(): Promise<ResultDomain<SysMenu>> {
    const response = await api.post<SysMenu>('/menus/all');
    return response.data;
  },

  /**
   * 获取菜单列表
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<SysMenu>>
   */
  async getMenuList(filter: SysMenu): Promise<ResultDomain<SysMenu>> {
    const response = await api.post<SysMenu>('/menus/list', filter);
    return response.data;
  },

  /**
   * 获取菜单树
   * @returns Promise<ResultDomain<MenuTreeNode>>
   */
  async getMenuTree(): Promise<ResultDomain<MenuTreeNode>> {
    const response = await api.get<MenuTreeNode>('/menus/tree');
    return response.data;
  },

  /**
   * 根据ID获取菜单
   * @param menuID 菜单ID
   * @returns Promise<ResultDomain<SysMenu>>
   */
  async getMenuById(menuID: string): Promise<ResultDomain<SysMenu>> {
    const response = await api.get<SysMenu>(`/menus`, {  menuID  });
    return response.data;
  },

  /**
   * 创建菜单
   * @param menu 菜单信息
   * @returns Promise<ResultDomain<string>> 返回菜单ID
   */
  async createMenu(menu: SysMenu): Promise<ResultDomain<string>> {
    const response = await api.post<string>('/menu', menu);
    return response.data;
  },

  /**
   * 更新菜单
   * @param menuID 菜单ID
   * @param menu 菜单信息
   * @returns Promise<ResultDomain<boolean>>
   */
  async updateMenu(menu: SysMenu): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>(`/menus`, menu);
    return response.data;
  },

  /**
   * 删除菜单
   * @param menuID 菜单ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteMenu(menuID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/menus`, {  menuID  });
    return response.data;
  },

  /**
   * 获取子菜单列表
   * @param parentID 父菜单ID
   * @returns Promise<ResultDomain<SysMenu>>
   */
  async getChildMenus(parentID: string): Promise<ResultDomain<SysMenu>> {
    const response = await api.get<SysMenu>(`/menus/children`, {  parentID  });
    return response.data;
  },

  /**
   * 获取菜单权限
   * @param menuID 菜单ID
   * @returns Promise<ResultDomain<SysPermission>>
   */
  async getMenuPermission(menuID: string): Promise<ResultDomain<SysPermission>> {
    const response = await api.post<SysPermission>(`/menus/permission`, {  menuID  });
    return response.data;
  }
};

