import { apiJSON } from './http'

export interface CommentItem {
  id: number
  username: string
  content: string
  user_id: number
  parentId?: number
  toUser?: string
}

export interface CommentListResp {
  items: CommentItem[]
  total: number
}

export const fetchLatestComments = (page: number, pageSize: number) => {
  const offset = (page - 1) * pageSize
  return apiJSON<any>(`/api/comments?limit=100&offset=${offset}&pageSize=${pageSize}`)
}

export const postComment = (user_id: number, content: string) => {
  return apiJSON<{}>('/api/comments', {
    method: 'POST',
    body: JSON.stringify({ user_id, content })
  })
}

export const fetchCommentThread = (id: number) => {
  return apiJSON<any>(`/api/comments/${id}/replies`)
}

export const postReply = (rootId: number, content: string, user_id: number, response_id: number = -1) => {
  return apiJSON<{}>(`/api/comments/${rootId}/replies`, {
    method: 'POST',
    body: response_id === -1 ? JSON.stringify({ user_id, content }) : JSON.stringify({ user_id, content, response_id })
  })
}
