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

/**
 * 积分兑换商品模型
 */
export interface ShopPointsProduct {
  id?: number;
  productName?: string;
  productImage?: string;
  productType?: number;
  couponId?: number;
  images?: string;
  description?: string;
  pointsPrice?: number;
  moneyPrice?: number;
  stock?: number;
  sales?: number;
  isHot?: number;
  sortNumber?: number;
  status?: number;
  deleted?: number;
  tenantId?: number;
  createTime?: string;
  updateTime?: string;
}

/**
 * 积分兑换商品查询参数
 */
export interface ShopPointsProductParam {
  page?: number;
  limit?: number;
  productName?: string;
  status?: number;
}

/**
 * 分页查询积分兑换商品
 */
export async function pageShopPointsProduct(params: ShopPointsProductParam) {
  const res = await request.get<ApiResult<PageResult<ShopPointsProduct>>>(
    MODULES_API_URL + '/shop/shop-points-product/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询积分兑换商品列表
 */
export async function listShopPointsProduct(params?: ShopPointsProductParam) {
  const res = await request.get<ApiResult<ShopPointsProduct[]>>(
    MODULES_API_URL + '/shop/shop-points-product',
    { params }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取积分兑换商品详情
 */
export async function getShopPointsProduct(id: number) {
  const res = await request.get<ApiResult<ShopPointsProduct>>(
    MODULES_API_URL + '/shop/shop-points-product/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加积分兑换商品
 */
export async function addShopPointsProduct(data: ShopPointsProduct) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-points-product',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改积分兑换商品
 */
export async function updateShopPointsProduct(data: ShopPointsProduct) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/shop-points-product',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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