import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { DocsBook, DocsBookParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询书籍
 */
export async function pageDocsBook(params: DocsBookParam) {
  const res = await request.get<ApiResult<PageResult<DocsBook>>>(
    MODULES_API_URL + '/cms/docs-book/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询书籍列表
 */
export async function listDocsBook(params?: DocsBookParam) {
  const res = await request.get<ApiResult<DocsBook[]>>(
    MODULES_API_URL + '/cms/docs-book',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加书籍
 */
export async function addDocsBook(data: DocsBook) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/docs-book',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改书籍
 */
export async function updateDocsBook(data: DocsBook) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/docs-book',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除书籍
 */
export async function removeDocsBook(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/docs-book/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除书籍
 */
export async function removeBatchDocsBook(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/docs-book/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改用户状态
 */
export async function updateDocsBookStatus(docsId?: number, status?: number) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/docs-book/status',
    {
      docsId,
      status
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询书籍
 */
export async function getDocsBook(id: number) {
  const res = await request.get<ApiResult<DocsBook>>(
    MODULES_API_URL + '/cms/docs-book/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查IP是否存在
 */
export async function checkExistence(
  field: string,
  value: string,
  id?: number
) {
  const res = await request.get<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/docs-book/existence',
    {
      params: { field, value, id }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
