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

import { api } from '@/apis/index';
import type { LearningRecord, LearningRecordStatistics, ResultDomain } from '@/types';

/**
 * 学习记录API服务
 */
export const learningRecordApi = {
  /**
   * 获取学习记录列表
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<LearningRecord>>
   */
  async getRecordList(filter?: Partial<LearningRecord>): Promise<ResultDomain<LearningRecord>> {
    const response = await api.post<LearningRecord>('/study/records/list', filter);
    return response.data;
  },

  /**
   * 创建学习记录
   * @param record 记录数据
   * @returns Promise<ResultDomain<LearningRecord>>
   */
  async createRecord(record: LearningRecord): Promise<ResultDomain<LearningRecord>> {
    const response = await api.post<LearningRecord>('/study/records/record', record);
    return response.data;
  },

  /**
   * 更新学习记录（禁用 loading 动画，避免影响视频播放）
   * @param record 记录数据
   * @returns Promise<ResultDomain<LearningRecord>>
   */
  async updateRecord(record: Partial<LearningRecord>): Promise<ResultDomain<LearningRecord>> {
    const response = await api.put<LearningRecord>('/study/records/record', record, {
      showLoading: false // 禁用 loading 动画
    } as any);
    return response.data;
  },

  /**
   * 删除学习记录
   * @param recordId 记录ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteRecord(recordId: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>('/study/records/record', { ID: recordId });
    return response.data;
  },

  /**
   * 标记学习完成（禁用 loading 动画，避免影响视频播放）
   * @param record 记录数据
   * @returns Promise<ResultDomain<LearningRecord>>
   */
  async markComplete(record: Partial<LearningRecord>): Promise<ResultDomain<LearningRecord>> {
    const response = await api.put<LearningRecord>('/study/records/complete', record, {
      showLoading: false // 禁用 loading 动画
    } as any);
    return response.data;
  },

  /**
   * 查询用户课程的学习记录
   * @param filter 过滤条件（包含 userID 和 resourceID）
   * @returns Promise<ResultDomain<LearningRecord>>
   */
  async getCourseLearningRecord(filter: Partial<LearningRecord>): Promise<ResultDomain<LearningRecord>> {
    const response = await api.post<LearningRecord>('/study/records/course/records', filter);
    return response.data;
  },
};
