import { request } from '@/utils/request'
import type { ProductListItem, ProductDetail } from '@/types'

/**
 * 商品详情
 * 后端：GET /api/product/{id}
 */
export function getProductDetailApi(id: number) {
  return request<ProductDetail>({
    url: `/api/product/${id}`,
    method: 'GET'
  })
}

/**
 * 分类下的商品列表
 * 后端：GET /api/product/list?categoryIds=1,2&orderBy=sales_desc&limit=20
 *
 * @param categoryIds 单个或多个分类 ID
 * @param orderBy sales_desc / price_asc / price_desc / create_desc
 * @param limit 返回数量
 */
export function getProductListApi(params: {
  categoryIds?: number[]
  orderBy?: 'sales_desc' | 'price_asc' | 'price_desc' | 'create_desc'
  limit?: number
}) {
  const query: Record<string, string> = {}
  if (params.categoryIds?.length) {
    query.categoryIds = params.categoryIds.join(',')
  }
  if (params.orderBy) query.orderBy = params.orderBy
  if (params.limit !== undefined) query.limit = String(params.limit)
  return request<ProductListItem[]>({
    url: '/api/product/list',
    method: 'GET',
    params: query
  })
}

/** 热门商品 */
export function getHotProductsApi(limit = 10) {
  return request<ProductListItem[]>({
    url: '/api/product/hot',
    method: 'GET',
    params: { limit }
  })
}

/** 新品上市 */
export function getNewProductsApi(limit = 10) {
  return request<ProductListItem[]>({
    url: '/api/product/new',
    method: 'GET',
    params: { limit }
  })
}
