import { request } from '@/utils/request'
import type { CartVO } from '@/types'

/**
 * 获取购物车
 * 后端：GET /api/cart
 */
export function getCartApi() {
  return request<CartVO>({
    url: '/api/cart',
    method: 'GET'
  })
}

/** 购物车商品数量（角标） */
export function getCartCountApi() {
  return request<number>({
    url: '/api/cart/count',
    method: 'GET'
  })
}

/** 添加商品 */
export function addCartItemApi(data: { productId: number; skuId: number; quantity: number }) {
  return request<void>({
    url: '/api/cart',
    method: 'POST',
    data
  })
}

/** 修改数量（PUT /api/cart/quantity） */
export function updateCartQuantityApi(data: { skuId: number; quantity: number }) {
  return request<void>({
    url: '/api/cart/quantity',
    method: 'PUT',
    data
  })
}

/** 切换单个选中（PUT /api/cart/{skuId}/selected?selected=0/1） */
export function updateCartItemSelectedApi(skuId: number, selected: 0 | 1) {
  return request<void>({
    url: `/api/cart/${skuId}/selected`,
    method: 'PUT',
    params: { selected }
  })
}

/** 全选/全不选 */
export function updateCartAllSelectedApi(selected: 0 | 1) {
  return request<void>({
    url: '/api/cart/all-selected',
    method: 'PUT',
    params: { selected }
  })
}

/** 删除单个 */
export function deleteCartItemApi(skuId: number) {
  return request<void>({
    url: `/api/cart/${skuId}`,
    method: 'DELETE'
  })
}

/** 批量删除（结算页） */
export function batchDeleteCartApi(skuIds: number[]) {
  return request<void>({
    url: '/api/cart',
    method: 'DELETE',
    data: skuIds
  })
}

/** 清空 */
export function clearCartApi() {
  return request<void>({
    url: '/api/cart/all',
    method: 'DELETE'
  })
}
