import request from './request';

export interface WorksStats {
  totalCount: number;
  totalPlayCount: number;
  totalLikeCount: number;
  totalCommentCount: number;
  totalShareCount: number;
}

export interface CommentsStats {
  totalCount: number;
  unreadCount: number;
  todayCount: number;
}

export interface TrendData {
  dates: string[];
  fans: number[];
  views: number[];
  likes: number[];
  comments: number[];
  shares: number[];
  collects: number[];
}

export interface TrendParams {
  userId: number;
  days?: number;
  accountId?: number;
}

const PYTHON_API_URL = 'http://localhost:5005';

export const dashboardApi = {
  // 获取作品统计
  getWorksStats(): Promise<WorksStats> {
    return request.get('/api/works/stats');
  },

  // 获取评论统计
  getCommentsStats(): Promise<CommentsStats> {
    return request.get('/api/comments/stats');
  },

  // 获取数据趋势（调用 Python API）
  async getTrend(params: TrendParams): Promise<TrendData> {
    const queryParams = new URLSearchParams({
      user_id: params.userId.toString(),
      days: (params.days || 7).toString(),
    });
    if (params.accountId) {
      queryParams.append('account_id', params.accountId.toString());
    }
    
    const response = await fetch(`${PYTHON_API_URL}/work_day_statistics/trend?${queryParams}`);
    const result = await response.json();
    
    if (!result.success) {
      throw new Error(result.error || '获取数据趋势失败');
    }
    
    return result.data;
  },
};
