import request from '~/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { CmsAd, CmsAdParam } from './model';


/**
 * 分页查询广告位
 */
export async function pageCmsAd(params: CmsAdParam) {
  const res = await request.get<ApiResult<PageResult<CmsAd>>>(
    '/cms/cms-ad/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询广告位列表
 */
export async function listCmsAd(params?: CmsAdParam) {
  const res = await request.get<ApiResult<CmsAd[]>>(
    '/cms/cms-ad',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加广告位
 */
export async function addCmsAd(data: CmsAd) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-ad',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改广告位
 */
export async function updateCmsAd(data: CmsAd) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-ad',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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

/**
 * 根据id查询广告位
 */
export async function getCmsAd(id: number) {
  const res = await request.get<ApiResult<CmsAd>>(
    '/cms/cms-ad/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
