import type { Recordable } from '@vben/types';

import { requestClient } from '#/api/request';

export namespace SystemApiApi {
  export interface SystemApiItem {
    id: number;
    role_id?: number;
    api_id?: number;
    api_name: string;
    api_code: string;
    method: string;
    url: string;
    status: 0 | 1;
    description?: string;
    create_time?: string;
    update_time?: string;
  }

  export interface SystemApi {
    list: SystemApiItem[];
    total: number;
  }

  export interface SystemApiAllResponse {
    items: SystemApiItem[];
  }

  export interface SystemRoleApiResponse {
    items: null | SystemApiItem[];
  }

  export interface RoleApiItem {
    id: number;
    role_id: number;
    api_id: number;
    api_name: string;
    api_code: string;
    method: string;
    url: string;
    status: 0 | 1;
    description?: string;
  }

  export interface RoleApi {
    list: RoleApiItem[];
  }
}

/**
 * 获取API列表数据
 */
async function getApiList(params: Recordable<any>) {
  return requestClient.get<SystemApiApi.SystemApi>('/api/list', {
    params,
  });
}

/**
 * 获取API详情
 * @param id API ID
 */
async function getApiDetail(id: number) {
  return requestClient.get<SystemApiApi.SystemApiItem>(`/api/detail/${id}`);
}

/**
 * 创建API
 * @param data API数据
 */
async function createApi(
  data: Omit<SystemApiApi.SystemApiItem, 'create_time' | 'id' | 'update_time'>,
) {
  return requestClient.post('/api/create', data);
}

/**
 * 更新API
 * @param id API ID
 * @param data API数据
 */
async function updateApi(
  id: number,
  data: Omit<SystemApiApi.SystemApiItem, 'create_time' | 'id' | 'update_time'>,
) {
  return requestClient.put(`/api/update/${id}`, data);
}

/**
 * 删除API
 * @param id API ID
 */
async function deleteApi(id: number) {
  return requestClient.delete(`/api/delete/${id}`);
}

/**
 * 批量更新API状态
 * @param data.ids API ID数组
 * @param data.status 状态值
 */
async function batchUpdateApiStatus(data: { ids: number[]; status: 0 | 1 }) {
  return requestClient.put('/api/batch-update-status', data);
}

/**
 * 获取角色API权限列表
 * @param roleId 角色ID
 */
async function getRoleApiList(roleId: number) {
  return requestClient.get<SystemApiApi.SystemRoleApiResponse>(
    `/role/${roleId}/api/list`,
  );
}

/**
 * 分配角色API权限
 * @param data.api_ids API ID数组
 * @param data.role_id 角色ID
 */
async function assignRoleApi(data: { api_ids: number[]; role_id: number }) {
  return requestClient.post('/role/api/assign', data);
}

/**
 * 移除角色API权限
 * @param data.api_ids API ID数组
 * @param data.role_id 角色ID
 */
async function removeRoleApi(data: { api_ids: number[]; role_id: number }) {
  return requestClient.post('/role/api/remove', data);
}

/**
 * 更新角色API权限（全量更新）
 * @param data.api_ids API ID数组
 * @param data.role_id 角色ID
 */
async function updateRoleApi(data: { api_ids: number[]; role_id: number }) {
  return requestClient.put('/role/api/update', data);
}

/**
 * 获取所有API列表（用于权限分配）
 * @param params.status 状态过滤
 */
async function getAllApiList(params?: { status?: number }) {
  return requestClient.get<SystemApiApi.SystemApiAllResponse>('/api/all', {
    params,
  });
}

export {
  assignRoleApi,
  batchUpdateApiStatus,
  createApi,
  deleteApi,
  getAllApiList,
  getApiDetail,
  getApiList,
  getRoleApiList,
  removeRoleApi,
  updateApi,
  updateRoleApi,
};
