import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Service, ServiceParam } from './model';
import { OPEN_API_URL } from '@/config/setting';

/**
 * 分页查询服务承诺
 */
export async function pageServices(params: ServiceParam) {
  const res = await request.get<ApiResult<PageResult<Service>>>(
    OPEN_API_URL + '/shop/goods-service/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询服务承诺列表
 */
export async function listServices(params?: ServiceParam) {
  const res = await request.get<ApiResult<Service[]>>(
    OPEN_API_URL + '/shop/goods-service',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加服务承诺
 */
export async function addService(data: Service) {
  const res = await request.post<ApiResult<unknown>>(
    OPEN_API_URL + '/shop/goods-service',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改服务承诺
 */
export async function updateService(data: Service) {
  const res = await request.put<ApiResult<unknown>>(
    OPEN_API_URL + '/shop/goods-service',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除服务承诺
 */
export async function removeService(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    OPEN_API_URL + '/shop/goods-service/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除服务承诺
 */
export async function removeServices(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    OPEN_API_URL + '/shop/goods-service/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
