/**
 * @description 个人中心相关API
 * @author yslg
 * @since 2025-10-15
 */

import { api } from '@/apis/index';
import type { 
  UserCenterStatistics, 
  LearningChartData, 
  ResourceLearningStats, 
  ResultDomain, 
  UserVO
} from '@/types';

/**
 * 个人中心API服务
 */
export const userProfileApi = {
  /**
   * 获取个人信息
   * @returns Promise<ResultDomain<any>>
   */
  async getUserProfile(): Promise<ResultDomain<UserVO>> {
    const response = await api.get<UserVO>('/usercenter/profile/info');
    return response.data;
  },

  /**
   * 更新个人信息
   * @param userInfo 用户信息
   * @returns Promise<ResultDomain<any>>
   */
  async updateUserProfile(userInfo: any): Promise<ResultDomain<any>> {
    const response = await api.put<any>('/usercenter/profile/info/update', userInfo);
    return response.data;
  },

  /**
   * 上传用户头像
   * @param file 头像文件
   * @returns Promise<ResultDomain<string>>
   */
  async uploadAvatar(file: File): Promise<ResultDomain<string>> {
    const formData = new FormData();
    formData.append('file', file);
    
    const response = await api.upload<string>('/usercenter/profile/avatar/upload', formData);
    return response.data;
  },

  /**
   * 修改密码
   * @param oldPassword 旧密码
   * @param newPassword 新密码
   * @returns Promise<ResultDomain<boolean>>
   */
  async changePassword(oldPassword: string, newPassword: string): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>('/usercenter/profile/password/change', {
      oldPassword,
      newPassword
    });
    return response.data;
  },

  /**
   * 绑定手机号
   * @param phone 手机号
   * @param code 验证码
   * @returns Promise<ResultDomain<boolean>>
   */
  async bindPhone(phone: string, code: string): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>('/usercenter/profile/phone/bind', {
      phone,
      code
    });
    return response.data;
  },

  /**
   * 绑定邮箱
   * @param email 邮箱
   * @param code 验证码
   * @returns Promise<ResultDomain<boolean>>
   */
  async bindEmail(email: string, code: string): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>('/usercenter/profile/email/bind', {
      email,
      code
    });
    return response.data;
  },

  /**
   * 获取学习记录统计
   * @param timeRange 时间范围
   * @returns Promise<ResultDomain<UserCenterStatistics>>
   */
  async getLearningStatistics(timeRange?: string): Promise<ResultDomain<UserCenterStatistics>> {
    const response = await api.get<UserCenterStatistics>('/usercenter/profile/learning/statistics', {
      timeRange
    });
    return response.data;
  },

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

  /**
   * 获取资源学习次数图表数据
   * @param timeRange 时间范围
   * @returns Promise<ResultDomain<ResourceLearningStats>>
   */
  async getResourceLearningChart(timeRange?: string): Promise<ResultDomain<ResourceLearningStats>> {
    const response = await api.get<ResourceLearningStats>('/usercenter/profile/learning/resource-chart', {
      timeRange
    });
    return response.data;
  }
};
