/**
 * @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.get<LearningRecord>('/study/learning-record/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/learning-record/create', record);
    return response.data;
  },

  /**
   * 更新学习记录
   * @param record 记录数据
   * @returns Promise<ResultDomain<LearningRecord>>
   */
  async updateRecord(record: LearningRecord): Promise<ResultDomain<LearningRecord>> {
    const response = await api.put<LearningRecord>('/study/learning-record/update', record);
    return response.data;
  },

  /**
   * 获取用户学习统计
   * @param userID 用户ID
   * @param timeRange 时间范围
   * @returns Promise<ResultDomain<LearningRecordStatistics>>
   */
  async getUserLearningStatistics(userID: string, timeRange?: string): Promise<ResultDomain<LearningRecordStatistics>> {
    const response = await api.get<LearningRecordStatistics>('/study/learning-record/statistics', {
      userID,
      timeRange
    });
    return response.data;
  },

  /**
   * 获取学习时长图表数据
   * @param userID 用户ID
   * @param timeRange 时间范围
   * @returns Promise<ResultDomain<LearningRecordStatistics>>
   */
  async getLearningDurationChart(userID: string, timeRange?: string): Promise<ResultDomain<LearningRecordStatistics>> {
    const response = await api.get<LearningRecordStatistics>('/study/learning-record/duration-chart', {
      userID,
      timeRange
    });
    return response.data;
  }
};
