import request from '@/utils/request'
import type { ShopGoodsComment, SubmitCommentParams, CommentPageParams } from './model'

/**
 * 提交商品评价
 * @desc 对接 Java 后端 /api/shop/shopGoodsComment
 */
export function submitGoodsComment(data: SubmitCommentParams) {
  return request.post<{ code: number; message: string }>('/shop/shopGoodsComment', {
    goodsId: data.goodsId,
    oid: data.oid || data.orderId,
    unique: data.unique,
    replyType: data.replyType,
    goodsScore: data.goodsScore,
    serviceScore: data.serviceScore || 5,
    comment: data.comment || data.content,
    pics: data.pics || data.images?.join(','),
  })
}

/**
 * 分页查询商品评价
 * @desc 对接 Java 后端 /api/shop/shopGoodsComment/page
 */
export function pageGoodsComment(params: CommentPageParams = {}) {
  return request.get<{
    code: number
    data: {
      records: ShopGoodsComment[]
      total: number
      size: number
      current: number
    }
  }>('/shop/shopGoodsComment/page', {
    params: {
      page: params.page || 1,
      size: params.pageSize || 10,
      goodsId: params.goodsId,
      oid: params.oid || params.orderId,
    },
  })
}

/**
 * 查询我的评价列表
 * @desc 对接 Java 后端 /api/shop/shopGoodsComment
 */
export function myGoodsComment(params: { page?: number; pageSize?: number } = {}) {
  return request.get<{
    code: number
    data: {
      records: ShopGoodsComment[]
      total: number
    }
  }>('/shop/shopGoodsComment', {
    params: {
      page: params.page || 1,
      size: params.pageSize || 10,
    },
  })
}

/**
 * 获取评价详情
 * @desc 对接 Java 后端 /api/shop/shopGoodsComment/{id}
 */
export function getGoodsComment(id: number) {
  return request.get<{ code: number; data: ShopGoodsComment }>(`/shop/shopGoodsComment/${id}`)
}
