import request from '@/utils/request'
import type {
  ShopGroupBuy,
  ShopGroupBuyRecord,
  ShopGroupBuyPageParams,
} from './model'

/** 获取拼团商品列表 */
export function pageShopGroupBuy(params: ShopGroupBuyPageParams = {}) {
  return request.post<{ items: ShopGroupBuy[]; total: number }>('/shopGroupBuy/page', params)
}

/** 获取拼团商品详情 */
export function getShopGroupBuy(id: number) {
  return request.post<ShopGroupBuy>('/shopGroupBuy/get', { id })
}

/** 获取进行中的拼团记录（可参团） */
export function listShopGroupBuyRecords(groupBuyId: number) {
  return request.post<ShopGroupBuyRecord[]>('/shopGroupBuy/listRecords', { groupBuyId })
}

/** 发起拼团 */
export function createGroupBuy(data: { groupBuyId: number; goodsId: number; skuId: number; quantity: number }) {
  return request.post<ShopGroupBuyRecord>('/shopGroupBuy/create', data)
}

/** 参与拼团 */
export function joinGroupBuy(data: { recordId: number; goodsId: number; skuId: number; quantity: number }) {
  return request.post<ShopGroupBuyRecord>('/shopGroupBuy/join', data)
}

/** 获取我的拼团记录 */
export function myGroupBuyRecords(params: { page?: number; pageSize?: number } = {}) {
  return request.post<{ items: ShopGroupBuyRecord[]; total: number }>('/shopGroupBuy/myRecords', params)
}
