import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopCategory, ShopCategoryParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询商品分类
 */
export async function pageShopCategory(params: ShopCategoryParam) {
  const res = await request.get<ApiResult<PageResult<ShopCategory>>>(
    MODULES_API_URL + '/shop/shop-category/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询全部商品分类
 */
export async function listShopCategory(params?: ShopCategoryParam) {
  const res = await request.get<ApiResult<ShopCategory[]>>(
    MODULES_API_URL + '/shop/shop-category',
    { params }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询商品分类
 */
export async function getShopCategory(id: number) {
  const res = await request.get<ApiResult<ShopCategory>>(
    MODULES_API_URL + '/shop/shop-category/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加商品分类
 */
export async function addShopCategory(data: ShopCategory) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-category',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改商品分类
 */
export async function updateShopCategory(data: ShopCategory) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-category',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除商品分类
 */
export async function removeShopCategory(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-category/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除商品分类
 */
export async function removeBatchShopCategory(
  data: (number | undefined)[]
) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-category/batch',
    { data }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
