import request from '@/utils/request';
import type {ApiResult, PageResult} from '@/api/index';
import type {CmsArticle, CmsArticleParam} from './model';

/**
 * 分页查询文章
 */
export async function pageCmsArticle(params: CmsArticleParam) {
  const res = await request.get<ApiResult<PageResult<CmsArticle>>>(
    '/cms/cms-article/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询文章列表
 */
export async function listCmsArticle(params?: CmsArticleParam) {
  const res = await request.get<ApiResult<CmsArticle[]>>(
    '/cms/cms-article',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加文章
 */
export async function addCmsArticle(data: CmsArticle) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-article',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改文章
 */
export async function updateCmsArticle(data: CmsArticle) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-article',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除文章
 */
export async function removeCmsArticle(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-article/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除文章
 */
export async function removeBatchCmsArticle(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-article/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询文章
 */
export async function getCmsArticle(id: number) {
  const res = await request.get<ApiResult<CmsArticle>>(
    '/cms/cms-article/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

export async function getCount(params?: CmsArticleParam) {
  const res = await request.get<ApiResult<unknown>>('/cms/cms-article/data', {
    params
  });
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 上一篇
 * @param params
 */
export async function getPrevious(params?: CmsArticleParam) {
  const res = await request.get<ApiResult<CmsArticle>>(
    '/cms/cms-article/getPrevious/' + params?.articleId,
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 下一篇
 * @param params
 */
export async function getNext(params?: CmsArticleParam) {
  const res = await request.get<ApiResult<CmsArticle>>(
    '/cms/cms-article/getNext/' + params?.articleId,
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 验证文章密码
 * @param params
 */
export async function checkArticlePassword(params?: CmsArticleParam) {
  const res = await request.get<ApiResult<unknown>>(
    '/cms/cms-article/checkArticlePassword',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

export async function findTags(params?: CmsArticleParam) {
  const res = await request.get<ApiResult<CmsArticle[]>>(
    '/cms/cms-article/findTags',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

export async function pageTags(params?: CmsArticleParam) {
  const res = await request.get<ApiResult<CmsArticle[]>>(
    '/cms/cms-article/pageTags',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 按IDS查询文章
 * @param params
 */
export async function getByIds(params?: CmsArticleParam) {
  const res = await request.get<ApiResult<CmsArticle[]>>(
    '/cms/cms-article/getByIds',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

