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


/**
 * 分页查询模型
 */
export async function pageCmsModel(params: CmsModelParam) {
  const res = await request.get<ApiResult<PageResult<CmsModel>>>(
    '/cms/cms-model/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询模型列表
 */
export async function listCmsModel(params?: CmsModelParam) {
  const res = await request.get<ApiResult<CmsModel[]>>(
    '/cms/cms-model',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加模型
 */
export async function addCmsModel(data: CmsModel) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-model',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改模型
 */
export async function updateCmsModel(data: CmsModel) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-model',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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

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