// api/modules/book.ts
import { mainClient, skeletonClient } from '@/api/clients'
import type { IApiResponse } from '../types'
import type {
  IBook,
  IBookDetail,
  IPageData,
  IChapter,
  IChapterContent,
  IComment,
  IReadProgress
} from '@/types/book'
import type { IGoods } from '@/types/order'

/**
 * 书籍相关API
 */
export const bookApi = {
  /**
   * 获取我的书单列表
   * @param current 当前页码
   * @param limit 每页数量
   */
  getMyBooks(current: number, limit: number) {
    return mainClient.request<IApiResponse<{ page: IPageData<IBook> }>>({
      url: 'bookAbroad/home/getMyBooks',
      method: 'POST',
      data: { current, limit }
    })
  },

  /**
   * 获取书籍详情
   * @param bookId 书籍ID
   */
  getBookInfo(bookId: number) {
    return skeletonClient.request<IApiResponse<{ bookInfo: IBookDetail }>>({
      url: 'bookAbroad/home/getBookInfo',
      method: 'POST',
      data: { bookId }
    })
  },

  /**
   * 获取书籍统计数据
   * @param bookId 书籍ID
   */
  getBookReadCount(bookId: number) {
    return skeletonClient.request<IApiResponse<{
      readCount: number
      listenCount: number
      buyCount: number
    }>>({
      url: 'bookAbroad/home/getBookReadCount',
      method: 'POST',
      data: { bookId }
    })
  },

  /**
   * 获取推荐书籍
   * @param bookId 书籍ID
   */
  getRecommendBook(bookId: number) {
    return skeletonClient.request<IApiResponse<{ bookList: IBook[] }>>({
      url: 'bookAbroad/home/getRecommendBook',
      method: 'POST',
      data: { bookId }
    })
  },

  /**
   * 获取评论列表
   * @param bookId 书籍ID
   * @param current 当前页码
   * @param limit 每页数量
   */
  getBookComments(bookId: number, current: number, limit: number) {
    return mainClient.request<IApiResponse<{
      commentsTree: IComment[]
      commentsCount: number
    }>>({
      url: 'bookAbroad/getBookAbroadCommentTree',
      method: 'POST',
      data: { bookId, current, limit }
    })
  },

  /**
   * 发表评论
   * @param bookId 书籍ID
   * @param content 评论内容
   * @param pid 父评论ID，默认为0表示顶级评论
   */
  insertComment(bookId: number, content: string, pid: number = 0) {
    return mainClient.request<IApiResponse>({
      url: 'bookAbroad/insertBookAbroadComment',
      method: 'POST',
      data: { bookId, content, pid }
    })
  },

  /**
   * 点赞评论
   * @param commentId 评论ID
   */
  likeComment(commentId: number) {
    return mainClient.request<IApiResponse>({
      url: 'bookAbroad/insertBookAbroadCommentLike',
      method: 'POST',
      data: { commentId }
    })
  },

  /**
   * 取消点赞
   * @param commentId 评论ID
   */
  unlikeComment(commentId: number) {
    return mainClient.request<IApiResponse>({
      url: 'bookAbroad/delBookAbroadCommentLike',
      method: 'POST',
      data: { commentId }
    })
  },

  /**
   * 删除评论
   * @param commentId 评论ID
   */
  deleteComment(commentId: number) {
    return mainClient.request<IApiResponse>({
      url: 'bookAbroad/delBookAbroadComment',
      method: 'POST',
      data: { commentId }
    })
  },

  /**
   * 获取章节列表
   * @param bookId 书籍ID
   */
  getBookChapter(data: { bookId: number; language: string }) {
    return mainClient.request<IApiResponse<{ chapterList: IChapter[] }>>({
      url: 'bookAbroad/home/getBookChapter',
      method: 'POST',
      data
    })
  },

  /**
   * 获取章节内容
   * @param chapterId 章节ID
   */
  getChapterContent(chapterId: number) {
    return mainClient.request<IApiResponse<{ contentPage: IChapterContent[] }>>({
      url: 'bookAbroad/home/getBookChapterContent',
      method: 'POST',
      data: { chapterId }
    })
  },

  /**
   * 获取阅读进度
   * @param bookId 书籍ID
   */
  getReadProgress(bookId: number) {
    return mainClient.request<IApiResponse<{ bookReadRate: IReadProgress | null }>>({
      url: 'bookAbroad/home/getBookReadRate',
      method: 'POST',
      data: { bookId }
    })
  },

  /**
   * 保存阅读进度
   * @param bookId 书籍ID
   * @param chapterId 章节ID
   * @param contentId 内容ID
   */
  saveReadProgress(bookId: number, chapterId: number, contentId: number) {
    return mainClient.request<IApiResponse>({
      url: 'bookAbroad/home/insertBookReadRate',
      method: 'POST',
      data: { bookId, chapterId, contentId }
    })
  },

  /**
   * 获取书籍语言列表
   * @param bookId 书籍ID
   * @param chapterId 章节ID
   * @param contentId 内容ID
   */
  getBookLanguages(bookId: number) {
    return mainClient.request<IApiResponse>({
      url: 'bookAbroad/home/getBookLanguage',
      method: 'POST',
      data: { bookId }
    })
  },

  /**
   * 获取听书章节内容（带音频时间点）
   * @param chapterId 章节ID
   */
  getChapterContentListen(chapterId: number) {
    return mainClient.request<IApiResponse<{ bookChapterContents: any[] }>>({
      url: 'bookAbroad/home/getBookChapterContentListen',
      method: 'POST',
      data: { chapterId }
    })
  },

  /**
   * 获取购买商品信息
   * @param bookId 书籍ID
   */
  getBookGoods(bookId: number) {
    return mainClient.request<IApiResponse<{ goodsList: IGoods[] }>>({
      url: 'bookAbroad/home/getProductListForBook',
      method: 'POST',
      data: { bookId }
    })
  },
}
