import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import { ShopSpec, ShopSpecParam } from '@/api/shop/shopSpec/model';

/**
 * 分页查询规格
 */
export async function pageShopSpec(params: ShopSpecParam) {
  const res = await request.get<ApiResult<PageResult<ShopSpec>>>(
    '/shop/shop-spec/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询规格列表
 */
export async function listShopSpec(params?: ShopSpecParam) {
  const res = await request.get<ApiResult<ShopSpec[]>>(
    '/shop/shop-spec',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加规格
 */
export async function addShopSpec(data: ShopSpec) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-spec',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改规格
 */
export async function updateShopSpec(data: ShopSpec) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-spec',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除规格
 */
export async function removeShopSpec(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-spec/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

/**
 * 根据id查询规格
 */
export async function getShopSpec(id: number) {
  const res = await request.get<ApiResult<ShopSpec>>(
    '/shop/shop-spec/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
