import request from '@/utils/request'
import type { ApiResult } from '@/api'
import type { ShopGoodsFavorite, ShopGoodsFavoriteParam } from './model'

// 添加收藏（关闭全局错误提示，由调用方自行处理）
export async function addShopGoodsFavorite(data: { goodsId: number }) {
  const res = await request.post<ApiResult<boolean>>('/shop/goods/favorite/add', data, { showError: false })
  if (res.code === 0) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

// 取消收藏（关闭全局错误提示，由调用方自行处理）
export async function removeShopGoodsFavorite(data: { goodsId: number }) {
  const res = await request.post<ApiResult<boolean>>('/shop/goods/favorite/remove', data, { showError: false })
  if (res.code === 0) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

// 查询收藏状态
export async function getShopGoodsFavoriteStatus(params: { goodsId: number }) {
  const res = await request.get<ApiResult<boolean>>('/shop/goods/favorite/status', params)
  if (res.code === 0) {
    return res.data
  }
  return Promise.reject(new Error(res.message))
}

// 收藏列表
export function listShopGoodsFavorite(params: ShopGoodsFavoriteParam) {
  return request.get<ShopGoodsFavorite[]>('/shop/goods/favorite/list', params)
}

// 收藏列表（分页）
export function pageShopGoodsFavorite(params: ShopGoodsFavoriteParam) {
  return request.get<{ list: ShopGoodsFavorite[]; total: number }>('/shop/goods/favorite/page', params)
}
