const formatDateAsString = (value) =>
  value instanceof Date ? value.toISOString() : value ?? null

function mapCommentToDealCommentResponse(comment) {
  return {
    id: comment.id,
    text: comment.text, // eÄŸer DB'de content ise burada text'e Ã§evir
    createdAt: formatDateAsString(comment.createdAt),
    parentId: comment.parentId ?? null,
    likeCount: Number.isFinite(comment.likeCount) ? comment.likeCount : 0,
    repliesCount: Number.isFinite(comment.repliesCount)
      ? comment.repliesCount
      : comment._count?.replies ?? 0,
    hasReplies: Number.isFinite(comment.repliesCount)
      ? comment.repliesCount > 0
      : (comment._count?.replies ?? 0) > 0,
    myLike: Boolean(comment.myLike),
    user: {
      id: comment.user.id,
      username: comment.user.username,
      avatarUrl: comment.user.avatarUrl ?? null,
    },
  }
}

function mapCommentsToDealCommentResponse(comments) {
  return comments.map(mapCommentToDealCommentResponse)
}


function mapCommentToUserCommentResponse(c) {
  return {
    ...mapCommentToDealCommentResponse(c),
    deal: { id: c.deal.id, title: c.deal.title },
  }
}

module.exports = {
  mapCommentToDealCommentResponse,
  mapCommentsToDealCommentResponse,
  mapCommentToUserCommentResponse
}
