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

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

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

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

  /**
   * 创建学习任务
   * @param task 任务数据
   * @returns Promise<ResultDomain<LearningTask>>
   */
  async createTask(task: LearningTask): Promise<ResultDomain<LearningTask>> {
    const response = await api.post<LearningTask>('/study/learning-task/create', task);
    return response.data;
  },

  /**
   * 更新学习任务
   * @param task 任务数据
   * @returns Promise<ResultDomain<LearningTask>>
   */
  async updateTask(task: LearningTask): Promise<ResultDomain<LearningTask>> {
    const response = await api.put<LearningTask>('/study/learning-task/update', task);
    return response.data;
  },

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

  /**
   * 发布学习任务
   * @param taskID 任务ID
   * @returns Promise<ResultDomain<LearningTask>>
   */
  async publishTask(taskID: string): Promise<ResultDomain<LearningTask>> {
    const response = await api.post<LearningTask>(`/study/learning-task/${taskID}/publish`);
    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>(`/study/learning-task/${taskID}/progress`, {
      userID
    });
    return response.data;
  }
};
