/**
 * @description 系统模块相关API
 * @author yslg
 * @since 2025-10-25
 */

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

/**
 * 系统模块API服务
 */
export const moduleApi = {
  baseUrl: '/system/modules',
  /**
   * 查询模块列表
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<SysModule>>
   */
  async getModuleList(filter?: Partial<SysModule>): Promise<ResultDomain<SysModule>> {
    const response = await api.post<SysModule>(`${this.baseUrl}/list`, filter);
    return response.data;
  },

  /**
   * 根据模块ID查询模块信息
   * @param moduleID 模块ID
   * @returns Promise<ResultDomain<SysModule>>
   */
  async getModuleById(moduleID: string): Promise<ResultDomain<SysModule>> {
    const response = await api.get<SysModule>(`${this.baseUrl}/${moduleID}`);
    return response.data;
  },

  /**
   * 根据模块代码查询模块信息
   * @param code 模块代码
   * @returns Promise<ResultDomain<SysModule>>
   */
  async getModuleByCode(code: string): Promise<ResultDomain<SysModule>> {
    const response = await api.get<SysModule>(`${this.baseUrl}/code/${code}`);
    return response.data;
  },

  /**
   * 查询启用的模块列表
   * @returns Promise<ResultDomain<SysModule>>
   */
  async getActiveModules(): Promise<ResultDomain<SysModule>> {
    const response = await api.get<SysModule>(`${this.baseUrl}/active`);
    return response.data;
  },

  /**
   * 创建模块
   * @param module 模块信息
   * @returns Promise<ResultDomain<SysModule>>
   */
  async createModule(module: SysModule): Promise<ResultDomain<SysModule>> {
    const response = await api.post<SysModule>(`${this.baseUrl}/module`, module);
    return response.data;
  },

  /**
   * 更新模块
   * @param module 模块信息
   * @returns Promise<ResultDomain<SysModule>>
   */
  async updateModule(module: SysModule): Promise<ResultDomain<SysModule>> {
    const response = await api.put<SysModule>(`${this.baseUrl}/module`, module);
    return response.data;
  },

  /**
   * 删除模块
   * @param moduleID 模块ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteModule(moduleID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`${this.baseUrl}/module`, { moduleID });
    return response.data;
  },

  /**
   * 批量删除模块
   * @param moduleIDs 模块ID列表
   * @returns Promise<ResultDomain<boolean>>
   */
  async batchDeleteModules(moduleIDs: string[]): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`${this.baseUrl}/batch`, { moduleIDs });
    return response.data;
  },

  /**
   * 更新模块状态
   * @param moduleID 模块ID
   * @param status 状态（0禁用 1启用）
   * @returns Promise<ResultDomain<boolean>>
   */
  async updateModuleStatus(moduleID: string, status: number): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>(`${this.baseUrl}/${moduleID}/status/${status}`);
    return response.data;
  },

  /**
   * 更新模块排序
   * @param moduleID 模块ID
   * @param orderNum 排序号
   * @returns Promise<ResultDomain<boolean>>
   */
  async updateModuleOrder(moduleID: string, orderNum: number): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>(`${this.baseUrl}/${moduleID}/order/${orderNum}`);
    return response.data;
  },

  /**
   * 分页查询模块列表
   * @param filter 过滤条件
   * @param pageParam 分页参数
   * @returns Promise<ResultDomain<SysModule>>
   */
  async getModuleListPage(filter?: Partial<SysModule>, pageParam?: PageParam): Promise<ResultDomain<SysModule>> {
    const response = await api.post<SysModule>(`${this.baseUrl}/page`, {
      filter,
      pageParam: {pageNumber: pageParam?.page || 1, pageSize: pageParam?.size || 10}
    });
    return response.data;
  },

  /**
   * 统计模块数量
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<number>>
   */
  async countModules(filter?: Partial<SysModule>): Promise<ResultDomain<number>> {
    const response = await api.post<number>(`${this.baseUrl}/count`, filter);
    return response.data;
  },

  /**
   * 检查模块代码是否存在
   * @param code 模块代码
   * @param excludeID 排除的模块ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async checkModuleCodeExists(code: string, excludeID?: string): Promise<ResultDomain<boolean>> {
    const response = await api.get<boolean>(`${this.baseUrl}/check-code`, { 
      code, 
      excludeID 
    });
    return response.data;
  },

  /**
   * 在模块中创建权限
   * @param moduleID 模块ID
   * @param permission 权限信息
   * @returns Promise<ResultDomain<SysPermission>>
   */
  async createPermissionInModule(moduleID: string, permission: SysPermission): Promise<ResultDomain<SysPermission>> {
    const response = await api.post<SysPermission>(`${this.baseUrl}/${moduleID}/permissions`, permission);
    return response.data;
  },

  /**
   * 更新模块中的权限
   * @param moduleID 模块ID
   * @param permission 权限信息
   * @returns Promise<ResultDomain<SysPermission>>
   */
  async updatePermissionInModule(moduleID: string, permission: SysPermission): Promise<ResultDomain<SysPermission>> {
    const response = await api.put<SysPermission>(`${this.baseUrl}/${moduleID}/permissions`, permission);
    return response.data;
  },

  /**
   * 删除模块中的权限
   * @param moduleID 模块ID
   * @param permissionID 权限ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deletePermissionInModule(moduleID: string, permissionID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`${this.baseUrl}/${moduleID}/permissions/${permissionID}`);
    return response.data;
  },

  /**
   * 获取模块的权限列表
   * @param moduleID 模块ID
   * @returns Promise<ResultDomain<SysPermission>>
   */
  async getModulePermissions(moduleID: string): Promise<ResultDomain<SysPermission>> {
    const response = await api.get<SysPermission>(`${this.baseUrl}/${moduleID}/permissions`);
    return response.data;
  }
};

