// api/modules/course.ts
import { skeletonClient, mainClient } from '@/api/clients/index'
import type { IApiResponse } from '../types'
import type {
  ICourseCategoryResponse,
  IUserLateCourseListResponse,
  IMarketCourseListResponse,
  ICourseDetailResponse,
  IChapterListResponse,
  IChapterDetailResponse,
  IProductListResponse,
  IVipInfo
} from '@/types/course'
import type { ISearchRequest, ISearchResponse } from '@/types/search'
import type { ICommentListResponse, IAddCommentResponse, IComment } from '@/types/comment'

/**
 * 课程相关API
 */
export const courseApi = {
  /**
   * 获取课程分类树
   * @returns 分类数据
   */
  getCourseMedicalTree() {
    return mainClient.request<ICourseCategoryResponse>({
      url: 'medical/home/getCourseMedicalTree',
      method: 'POST',
      data: {}
    })
  },

  /**
   * 获取用户最近观看课程列表
   * @returns 观看记录列表
   */
  getUserLateCourseList() {
    return skeletonClient.request<IUserLateCourseListResponse>({
      url: 'medical/home/getUserLateCourseList',
      method: 'POST',
      data: {}
    })
  },

  /**
   * 获取市场课程列表（试听课程）
   * @param id 市场ID
   * @param limit 每页数量
   * @param page 页码
   * @returns 课程列表
   */
  getMarketCourseList(data: {
    id: number,
    page: number,
    limit: number
  }) {
    return skeletonClient.request<IMarketCourseListResponse>({
      url: uni.getStorageSync('token') ? 'medical/home/getMarketCourseList' : 'visitor/getMarketCourseList',
      method: 'POST',
      data
    })
  },

  /**
   * 搜索课程和商品
   * @param data 搜索参数 { keyWord, appName }
   * @returns 搜索结果
   */
  searchData(data: ISearchRequest) {
    return mainClient.request<ISearchResponse>({
      url: 'bookAbroad/home/searchCourse',
      method: 'POST',
      data
    })
  },

  /**
   * 获取课程详情
   * @param id 课程ID
   */
  getCourseDetail(id: number) {
    return skeletonClient.request<ICourseDetailResponse>({
      url: 'sociology/course/getCourseDetail',
      method: 'POST',
      data: { id }
    })
  },

  /**
   * 获取目录章节列表
   * @param id 目录ID
   */
  getCatalogueChapterList(id: number) {
    return mainClient.request<IChapterListResponse>({
      url: 'sociology/course/getCourseCatalogueChapterList',
      method: 'POST',
      data: { id }
    })
  },

  /**
   * 获取章节详情
   * @param id 章节ID
   */
  getChapterDetail(id: number) {
    return skeletonClient.request<IChapterDetailResponse>({
      url: 'sociology/course/getCourseCatalogueChapterDetail',
      method: 'POST',
      data: { id, load: false }
    })
  },

  /**
   * 领取免费课程
   * @param catalogueId 目录ID
   */
  startStudyForMF(catalogueId: number) {
    return mainClient.request<IApiResponse>({
      url: 'sociology/course/startStudyForMF',
      method: 'POST',
      data: { catalogueId }
    })
  },

  /**
   * 获取课程商品列表
   * @param id 目录ID
   */
  getProductListForCourse(id: number) {
    return mainClient.request<IProductListResponse>({
      url: 'sociology/product/getProductListForCourse',
      method: 'POST',
      data: { id }
    })
  },

  /**
   * 检查目录是否支持复读
   * @param courseCatalogueId 目录ID
   */
  checkRenewPayment(courseCatalogueId: number) {
    return skeletonClient.request<IApiResponse<{ canRelearn: boolean }>>({
      url: 'common/courseRelearn/courseCatalogueCanRelearn',
      method: 'POST',
      data: { courseCatalogueId }
    })
  },

  /**
   * 获取复读商品列表
   * @param catalogueId 目录ID
   */
  getRenewProductList(catalogueId: number) {
    return mainClient.request<IProductListResponse>({
      url: 'common/courseRelearn/relearnShopProductList',
      method: 'POST',
      data: { catalogueId }
    })
  },

  /**
   * 获取课程留言列表
   * @param courseId 课程ID
   * @param page 页码
   * @param limit 每页数量
   * @param userId 用户ID
   */
  getCourseComments(courseId: number, page: number, limit: number, userId: number) {
    return mainClient.request<ICommentListResponse>({
      url: 'common/courseGuestbook/getCourseGuestbookList',
      method: 'POST',
      data: { courseId, page, limit, userId, chapterId: '' }
    })
  },

  /**
   * 添加课程留言
   * @param data 留言数据
   */
  addCourseComment(data: {
    type: number
    courseId: number
    chapterId: string
    pid: number
    userId: number
    forUserId: number
    content: string
    images: string
  }) {
    return mainClient.request<IAddCommentResponse>({
      url: 'common/courseGuestbook/addCourseGuestbook',
      method: 'POST',
      data
    })
  },

  /**
   * 点赞留言
   * @param userId 用户ID
   * @param guestbookId 留言ID
   */
  likeComment(userId: number, guestbookId: number) {
    return mainClient.request<IApiResponse>({
      url: 'common/courseGuestbook/addCourseGuestbookSupport',
      method: 'POST',
      data: { userId, guestbookId }
    })
  },

  /**
   * 取消点赞
   * @param userId 用户ID
   * @param guestbookId 留言ID
   */
  unlikeComment(userId: number, guestbookId: number) {
    return mainClient.request<IApiResponse>({
      url: 'common/courseGuestbook/cancelCourseGuestbookSupport',
      method: 'POST',
      data: { userId, guestbookId }
    })
  },

  /**
   * 检查用户VIP权益
   * @param courseId 课程ID
   */
  checkCourseVip(courseId: number) {
    return skeletonClient.request<IApiResponse<{ userVip: IVipInfo | null }>>({
      url: 'common/userVip/ownCourseCatalogueByVip',
      method: 'POST',
      data: { courseId }
    })
  },

  /**
   * 获取课程需要的VIP类型
   * @param courseId 课程ID
   */
  getCourseVipModule(courseId: number) {
    return skeletonClient.request<IApiResponse<{ list: string[] }>>({
      url: 'common/userVip/getCourseVipModule',
      method: 'POST',
      data: { courseId }
    })
  }
}
