import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Config, ConfigParam } from './model';

/**
 * 分页查询配置
 */
export async function configData(params: ConfigParam) {
  const res = await request.get<ApiResult<PageResult<Config>>>(
    '/system/config/data',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询配置列表
 */
export async function listConfigs(params?: ConfigParam) {
  const res = await request.get<ApiResult<Config[]>>('/system/config', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加配置
 */
export async function addConfig(data: Config) {
  const res = await request.post<ApiResult<unknown>>('/system/config', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改配置
 */
export async function updateConfig(data: Config) {
  const res = await request.put<ApiResult<unknown>>('/system/config', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改配置状态
 */
export async function updateConfigStatus(id: number, status: number) {
  const res = await request.put<ApiResult<unknown>>('/system/config/status', {
    id,
    status
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改配置状态
 */
export async function updateConfigSort(id: number, sort: number) {
  const res = await request.put<ApiResult<unknown>>('/system/config/sort', {
    id,
    sort
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 批量删除配置
 */
export async function removeConfigs(ids: number[]) {
  const res = await request.delete<ApiResult<unknown>>('/system/config/batch', {
    data: ids
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 检查配置是否存在
 */
export async function checkExistence(
  field: string,
  value: string,
  id?: number
) {
  const res = await request.get<ApiResult<unknown>>('/system/config/existence', {
    params: { field, value, id }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 同步当前配置
 */
export async function syncConfigs() {
  const res = await request.put<ApiResult<Config[]>>('/system/config/sync', {});
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取当前配置表数据
 */
export async function infoConfigs() {
  const res = await request.get<ApiResult<any>>('/system/config/info');
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 修改配置数据
 */
export async function updateConfigData(data: any) {
  const res = await request.put<ApiResult<unknown>>('/system/config/data', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
