import request from '@/utils/request';
import type { ApiResult } from '@/api';
import type { ShopCart, ShopCartParam, AddToCartParam, UpdateCartNumParam } from './model';

/**
 * 获取购物车列表
 */
export async function listShopCart(params?: ShopCartParam) {
  const res = await request.get<ApiResult<ShopCart[]>>(
    '/shop/shop-cart',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 获取购物车分页列表
 */
export async function pageShopCart(params: ShopCartParam) {
  const res = await request.get<ApiResult<{ list: ShopCart[]; count: number }>>(
    '/shop/shop-cart/page',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 获取购物车数量
 */
export async function getShopCartCount() {
  const res = await request.get<ApiResult<{ count: number }>>(
    '/shop/shop-cart/count'
  );
  if (res.code === 0 && res.data) {
    return res.data.count;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加商品到购物车
 */
export async function addToCart(data: AddToCartParam) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-cart',
    data
  );
  if (res.code === 0) {
    return res.message || '添加成功';
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 更新购物车商品数量
 */
export async function updateCartNum(data: UpdateCartNumParam) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-cart/' + data.id,
    { quantity: data.num }
  );
  if (res.code === 0) {
    return res.message || '更新成功';
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 更新购物车商品选中状态
 */
export async function updateCartChecked(id: number, checked: boolean) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-cart/' + id,
    { selected: checked }
  );
  if (res.code === 0) {
    return res.message || '更新成功';
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 全选/取消全选购物车
 */
export async function updateCartAllChecked(checked: boolean) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-cart/selected',
    { selected: checked }
  );
  if (res.code === 0) {
    return res.message || '更新成功';
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除购物车商品
 */
export async function removeShopCart(id: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-cart/' + id
  );
  if (res.code === 0) {
    return res.message || '删除成功';
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除购物车商品
 */
export async function removeBatchShopCart(ids: number[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-cart/batch',
    { ids }
  );
  if (res.code === 0) {
    return res.message || '删除成功';
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 清空购物车
 */
export async function clearShopCart() {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-cart/clear'
  );
  if (res.code === 0) {
    return res.message || '清空成功';
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 获取购物车详情
 */
export async function getShopCart(id: number) {
  const res = await request.get<ApiResult<ShopCart>>(
    '/shop/shop-cart/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
