// api/modules/home.ts
import { mainClient, skeletonClient } from '@/api/clients'
import type {
  IMyBooksResponse,
  IRecommendBooksResponse,
  ILabelListResponse,
  IBookListResponse,
  IVipInfoResponse,
  ISearchResponse
} from '@/types/book'

/**
 * 首页相关API
 */
export const bookHomeApi = {
  /**
   * 获取VIP信息
   */
  getVipInfo() {
    return mainClient.request<IVipInfoResponse>({
      url: 'bookAbroad/home/getVipInfo',
      method: 'POST',
      data: {}
    })
  },

  /**
   * 获取我的书单
   * @param current 当前页码
   * @param limit 每页数量
   */
  getMyBooks(current: number, limit: number) {
    return skeletonClient.request<IMyBooksResponse>({
      url: 'bookAbroad/home/getMyBooks',
      method: 'POST',
      data: { current, limit }
    })
  },

  /**
   * 获取推荐图书
   */
  getRecommendBooks() {
    return skeletonClient.request<IRecommendBooksResponse>({
      url: uni.getStorageSync('token') ? 'bookAbroad/home/getRecommendBooks' : 'visitor/bookAbroad/getRecommendBooks',
      method: 'POST',
      data: {}
    })
  },

  /**
   * 获取标签列表
   * @param type 0: 分类标签, 1: 活动标签
   */
  getBookLabelList(type: number) {
    return skeletonClient.request<ILabelListResponse>({
      url: uni.getStorageSync('token') ? 'bookAbroad/home/getBookAbroadLableList' : 'visitor/bookAbroad//getBookAbroadLableList',
      method: 'POST',
      data: { type }
    })
  },

  /**
   * 根据父级ID获取子标签列表
   * @param pid 父级标签ID
   */
  getSubLabelList(pid: number) {
    return skeletonClient.request<ILabelListResponse>({
      url: uni.getStorageSync('token') ? 'bookAbroad/home/getBookAbroadLableListByPid' : 'visitor/bookAbroad//getBookAbroadLableListByPid',
      method: 'POST',
      data: { pid }
    })
  },

  /**
   * 根据标签ID获取图书列表
   * @param lableId 标签ID（注意：原接口参数名为 lableId）
   */
  getBooksByLabel(lableId: number) {
    return skeletonClient.request<IBookListResponse>({
      url: uni.getStorageSync('token') ? 'bookAbroad/home/getAbroadBookListByLable' : 'visitor/bookAbroad/getAbroadBookListByLable',
      method: 'POST',
      data: { lableId }
    })
  },

  /**
   * 搜索图书
   * @param keyword 搜索关键字
   */
  searchBooks(data: {
    key: string,
    page: number,
    limit: number,
  }) {
    return mainClient.request<ISearchResponse>({
      url: 'bookAbroad/home/searchBook',
      method: 'POST',
      data
    })
  }
}
