/**
 * @description 课程相关API
 * @author yslg
 * @since 2025-10-15
 */

import { api } from '@/apis/index';
import type { Course, CourseChapter, ResultDomain,CourseVO,PageRequest, PageParam, CourseNode } from '@/types';

/**
 * 课程API服务
 */
export const courseApi = {
  prefixCourse: '/study/courses',
  /**
   * 获取课程列表
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<Course>>
   */
  async getCourseList(filter?: Course): Promise<ResultDomain<Course>> {
    const response = await api.get<Course>(`${this.prefixCourse}/list`, filter);
    return response.data;
  },

  /**
   * 获取课程分页列表
   * @param PageRequest 分页请求
   * @returns Promise<ResultDomain<Course>>
   */
  async getCoursePage(pageParam: PageParam, filter?: Course): Promise<ResultDomain<Course>> {
    const response = await api.post<Course>(`${this.prefixCourse}/page`, {
      pageParam,
      filter
    });
    return response.data;
  },

  /**
   * 根据ID获取课程详情
   * @param courseID 课程ID
   * @returns Promise<ResultDomain<Course>>
   */
  async getCourseById(courseID: string): Promise<ResultDomain<CourseVO>> {
    const response = await api.get<CourseVO>(`${this.prefixCourse}/${courseID}`);
    return response.data;
  },

  /**
   * 创建课程
   * @param course 课程数据
   * @returns Promise<ResultDomain<Course>>
   */
  async createCourse(course: CourseVO): Promise<ResultDomain<CourseVO>> {
    const response = await api.post<CourseVO>(`${this.prefixCourse}/course`, course);
    return response.data;
  },

  /**
   * 更新课程
   * @param course 课程数据
   * @returns Promise<ResultDomain<Course>>
   */
  async updateCourse(courseVO: CourseVO): Promise<ResultDomain<CourseVO>> {
    const response = await api.put<CourseVO>(`${this.prefixCourse}/course`, courseVO);
    return response.data;
  },

  /**
   * 删除课程
   * @param courseID 课程ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteCourse(courseID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`${this.prefixCourse}/${courseID}`);
    return response.data;
  },

  /**
   * 更新课程状态
   * @param courseID 课程ID
   * @param status 状态
   * @returns Promise<ResultDomain<Course>>
   */
  async updateCourseStatus(course: Course): Promise<ResultDomain<Course>> {
    const response = await api.put<Course>(`${this.prefixCourse}/course/status`, course);
    return response.data;
  },

  /**
   * 创建课程章节
   * @param chapter 章节数据
   * @returns Promise<ResultDomain<CourseChapter>>
   */
  async createCourseChapter(chapter: CourseChapter): Promise<ResultDomain<CourseChapter>> {
    const response = await api.post<CourseChapter>(`${this.prefixCourse}/course/chapter`, chapter);
    return response.data;
  },

  /**
   * 更新课程章节
   * @param chapter 章节数据
   * @returns Promise<ResultDomain<CourseChapter>>
   */
  async updateCourseChapter(chapter: CourseChapter): Promise<ResultDomain<CourseChapter>> {
    const response = await api.put<CourseChapter>(`${this.prefixCourse}/course/chapter`, chapter);
    return response.data;
  },

  /**
   * 删除课程章节
   * @param chapterID 章节ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteCourseChapter(chapterID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`${this.prefixCourse}/course/chapter/${chapterID}`);
    return response.data;
  },

  /**
   * 创建课程章节节点
   * @param node 节点数据
   * @returns Promise<ResultDomain<CourseNode>>
   */
  async createCourseChapterNode(node: CourseNode): Promise<ResultDomain<CourseNode>> {
    const response = await api.post<CourseNode>(`${this.prefixCourse}/course/chapter/node`, node);
    return response.data;
  },

  /**
   * 更新课程章节节点
   * @param node 节点数据
   * @returns Promise<ResultDomain<CourseNode>>
   */
  async updateCourseChapterNode(node: CourseNode): Promise<ResultDomain<CourseNode>> {
    const response = await api.put<CourseNode>(`${this.prefixCourse}/course/chapter/node`, node);
    return response.data;
  },

  /**
   * 删除课程章节节点
   * @param nodeID 节点ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteCourseChapterNode(nodeID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`${this.prefixCourse}/course/chapter/node/${nodeID}`);
    return response.data;
  },

  /**
   * 增加课程浏览次数
   * @param courseID 课程ID
   * @returns Promise<ResultDomain<Course>>
   */
  async incrementViewCount(courseID: string): Promise<ResultDomain<Course>> {
    const response = await api.post<Course>(`${this.prefixCourse}/${courseID}/view`);
    return response.data;
  },

  /**
   * 增加课程学习人数
   * @param courseID 课程ID
   * @returns Promise<ResultDomain<Course>>
   */
  async incrementLearnCount(courseID: string): Promise<ResultDomain<Course>> {
    const response = await api.post<Course>(`${this.prefixCourse}/${courseID}/learn`);
    return response.data;
  },

  /**
   * 获取课程章节列表
   * @param courseID 课程ID
   * @returns Promise<ResultDomain<CourseChapter>>
   */
  async getCourseChapters(courseID: string): Promise<ResultDomain<CourseChapter>> {
    const response = await api.get<CourseChapter>(`${this.prefixCourse}/${courseID}/chapters`);
    return response.data;
  },

  /**
   * 根据ID获取章节详情
   * @param chapterID 章节ID
   * @returns Promise<ResultDomain<CourseChapter>>
   */
  async getChapterById(chapterID: string): Promise<ResultDomain<CourseChapter>> {
    const response = await api.get<CourseChapter>(`${this.prefixCourse}/chapter/${chapterID}`);
    return response.data;
  },

  /**
   * 创建课程章节
   * @param chapter 章节数据
   * @returns Promise<ResultDomain<CourseChapter>>
   */
  async createChapter(chapter: CourseChapter): Promise<ResultDomain<CourseChapter>> {
    const response = await api.post<CourseChapter>(`${this.prefixCourse}/chapter/create`, chapter);
    return response.data;
  },

  /**
   * 更新课程章节
   * @param chapter 章节数据
   * @returns Promise<ResultDomain<CourseChapter>>
   */
  async updateChapter(chapter: CourseChapter): Promise<ResultDomain<CourseChapter>> {
    const response = await api.put<CourseChapter>(`${this.prefixCourse}/chapter/update`, chapter);
    return response.data;
  },

  /**
   * 删除课程章节
   * @param chapterID 章节ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteChapter(chapterID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`${this.prefixCourse}/chapter/${chapterID}`);
    return response.data;
  },

  /**
   * 更新章节排序
   * @param chapterID 章节ID
   * @param orderNum 排序号
   * @returns Promise<ResultDomain<CourseChapter>>
   */
  async updateChapterOrder(chapterID: string, orderNum: number): Promise<ResultDomain<CourseChapter>> {
    const response = await api.put<CourseChapter>(`${this.prefixCourse}/chapter/${chapterID}/order`, null, {
      params: { orderNum }
    });
    return response.data;
  }
};
