/**
 * @description 学习任务相关API
 * @author yslg
 * @since 2025-10-15
 */

import { api } from '@/apis/index';
import type { LearningTask, LearningProgress, ResultDomain, PageParam, TaskVO, TaskUser, TaskItemVO } from '@/types';

/**
 * 学习任务API服务
 */
export const learningTaskApi = {
  learningTaskPrefix: '/study/tasks',
  /**
   * 获取学习任务列表
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<LearningTask>>
   */
  async getTaskList(filter?: Partial<LearningTask>): Promise<ResultDomain<LearningTask>> {
    const response = await api.get<LearningTask>(`${this.learningTaskPrefix}/list`, filter);
    return response.data;
  },

  /**
   * 根据ID获取任务详情
   * @param taskID 任务ID
   * @returns Promise<ResultDomain<TaskVO>>
   */
  async getTaskById(taskID: string): Promise<ResultDomain<TaskVO>> {
    const response = await api.get<TaskVO>(`${this.learningTaskPrefix}/${taskID}`);
    return response.data;
  },

    /**
   * 根据ID获取用户任务详情
   * @param taskID 任务ID
   * @returns Promise<ResultDomain<TaskVO>>
   */
    async getUserTask(taskID: string): Promise<ResultDomain<TaskVO>> {
      const response = await api.get<TaskVO>(`${this.learningTaskPrefix}/${taskID}/user`);
      return response.data;
    },

  /**
   * 获取任务分页列表
   * @param pageParam 分页参数
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<LearningTask>>
   */
  async getTaskPage(pageParam: PageParam, filter: LearningTask): Promise<ResultDomain<LearningTask>> {
    const response = await api.post<LearningTask>(`${this.learningTaskPrefix}/page`, {pageParam, filter});
    return response.data;
  },
  /**
   * 创建学习任务
   * @param task 任务数据
   * @returns Promise<ResultDomain<TaskVO>>
   */
  async createTask(task: TaskVO): Promise<ResultDomain<TaskVO>> {
    const response = await api.post<TaskVO>(`${this.learningTaskPrefix}/task`, task);
    return response.data;
  },

  /**
   * 更新学习任务
   * @param task 任务数据
   * @returns Promise<ResultDomain<TaskVO>>
   */
  async updateTask(task: TaskVO): Promise<ResultDomain<TaskVO>> {
    const response = await api.put<TaskVO>(`${this.learningTaskPrefix}/task`, task);
    return response.data;
  },

  /**
   * 删除学习任务
   * @param taskID 任务ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteTask(taskID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`${this.learningTaskPrefix}/task/${taskID}`);
    return response.data;
  },

  /**
   * 发布学习任务
   * @param taskID 任务ID
   * @returns Promise<ResultDomain<LearningTask>>
   */
  async publishTask(task: LearningTask): Promise<ResultDomain<LearningTask>> {
    const response = await api.put<LearningTask>(`${this.learningTaskPrefix}/status`, task);
    return response.data;
  },

  /**
   * 获取用户任务进度
   * @param userID 用户ID
   * @param taskID 任务ID
   * @returns Promise<ResultDomain<LearningProgress>>
   */
  async getUserTaskProgress(userID: string, taskID: string): Promise<ResultDomain<LearningProgress>> {
    const response = await api.get<LearningProgress>(`${this.learningTaskPrefix}/task/${taskID}/progress`, {
      userID
    });
    return response.data;
  },

  /**
   * 分配任务给用户
   * @param taskID 任务ID
   * @param userID 用户ID
   * @returns Promise<ResultDomain<TaskUser>>
   */
  async assignTaskToUser(taskID: string, userID: string): Promise<ResultDomain<TaskUser>> {
    const response = await api.post<TaskUser>(`${this.learningTaskPrefix}/${taskID}/assign`, null, {
      params: { userID }
    });
    return response.data;
  },

  /**
   * 从用户移除任务
   * @param taskID 任务ID
   * @param userID 用户ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async removeTaskFromUser(taskID: string, userID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`${this.learningTaskPrefix}/${taskID}/user/${userID}`);
    return response.data;
  },

  /**
   * 用户获取个人任务列表（用户视角）
   * @param pageParam 分页参数
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<LearningTask>>
   */
  async getUserTaskPage(pageParam: PageParam, filter?: TaskItemVO): Promise<ResultDomain<LearningTask>> {
    const response = await api.post<LearningTask>(`${this.learningTaskPrefix}/users/page`, {
      pageParam,
      filter
    });
    return response.data;
  },

  async getUserProgress(userID: string): Promise<ResultDomain<TaskVO>> {
    const response = await api.post<TaskVO>(`${this.learningTaskPrefix}/user/progress/${userID}`);
    return response.data;
  },
};
