import { Injectable } from '@angular/core';
import { Observable, from, forkJoin, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { AdminDataService } from '../services/admin-data.service';

// 定义仪表盘统计数据接口
export interface DashboardStats {
  totalProjects: number;
  activeProjects: number;
  completedProjects: number;
  totalDesigners: number;
  totalCustomers: number;
  totalRevenue: number;
  projectTrend: { name: string; value: number }[];
  revenueData: { name: string; value: number }[];
}

@Injectable({
  providedIn: 'root'
})
export class AdminDashboardService {
  constructor(private adminData: AdminDataService) {}

  // 获取仪表盘统计数据
  getDashboardStats(): Observable<DashboardStats> {
    return forkJoin({
      totalProjects: from(this.adminData.count('Project')),
      activeProjects: from(this.adminData.count('Project', q => q.equalTo('status', '进行中'))),
      completedProjects: from(this.adminData.count('Project', q => q.equalTo('status', '已完成'))),
      totalDesigners: from(this.adminData.count('Profile')),
      totalCustomers: from(this.adminData.count('ContactInfo')),
      totalRevenue: from(this.calculateTotalRevenue())
    }).pipe(
      map(stats => ({
        ...stats,
        projectTrend: [],
        revenueData: []
      })),
      catchError(error => {
        console.error('加载仪表盘统计失败:', error);
        return of({
          totalProjects: 0,
          activeProjects: 0,
          completedProjects: 0,
          totalDesigners: 0,
          totalCustomers: 0,
          totalRevenue: 0,
          projectTrend: [],
          revenueData: []
        });
      })
    );
  }

  // 计算总收入
  private async calculateTotalRevenue(): Promise<number> {
    try {
      const settlements = await this.adminData.findAll('ProjectSettlement', {
        additionalQuery: q => q.equalTo('status', '已结算')
      });
      return settlements.reduce((sum, s) => sum + (s.get('amount') || 0), 0);
    } catch (error) {
      console.error('计算总收入失败:', error);
      return 0;
    }
  }

  // 获取最近活动记录 - 从ProjectChange和Project表获取
  getRecentActivities(): Observable<any[]> {
    return from(this.loadRecentActivities()).pipe(
      catchError(error => {
        console.error('加载最近活动失败:', error);
        return of([]);
      })
    );
  }

  private async loadRecentActivities(): Promise<any[]> {
    const activities: any[] = [];

    // 获取最近创建的项目
    const recentProjects = await this.adminData.findAll('Project', {
      limit: 10,
      descending: 'createdAt',
      include: ['customer']
    });

    recentProjects.forEach(project => {
      activities.push({
        id: project.id,
        type: 'project_created',
        title: project.get('title'),
        customer: project.get('customer')?.get('name') || '未知客户',
        time: project.get('createdAt')
      });
    });

    // 按时间排序
    activities.sort((a, b) => new Date(b.time).getTime() - new Date(a.time).getTime());
    return activities.slice(0, 20);
  }

  // 获取项目状态分布
  getProjectStatusDistribution(): Observable<any[]> {
    return from(this.loadProjectStatusDistribution()).pipe(
      catchError(error => {
        console.error('加载项目状态分布失败:', error);
        return of([]);
      })
    );
  }

  private async loadProjectStatusDistribution(): Promise<any[]> {
    const statusList = ['待分配', '进行中', '已完成', '已暂停', '已延期', '已取消'];
    const results = await Promise.all(
      statusList.map(async status => ({
        name: status,
        value: await this.adminData.count('Project', q => q.equalTo('status', status)),
        color: this.getStatusColor(status)
      }))
    );
    return results.filter(r => r.value > 0);
  }

  private getStatusColor(status: string): string {
    const colorMap: Record<string, string> = {
      '待分配': '#FFAA00',
      '进行中': '#165DFF',
      '已完成': '#00B42A',
      '已暂停': '#FF7D00',
      '已延期': '#F53F3F',
      '已取消': '#86909C'
    };
    return colorMap[status] || '#165DFF';
  }

  // 获取设计师工作量统计
  getDesignerWorkloadStats(): Observable<any[]> {
    return from(this.loadDesignerWorkloadStats()).pipe(
      catchError(error => {
        console.error('加载设计师工作量统计失败:', error);
        return of([]);
      })
    );
  }

  private async loadDesignerWorkloadStats(): Promise<any[]> {
    // 获取所有设计师
    const designers = await this.adminData.findAll('Profile', {
      additionalQuery: q => q.equalTo('roleName', '组员'),
      limit: 10
    });

    const stats = await Promise.all(
      designers.map(async designer => {
        const completed = await this.adminData.count('Project', q => {
          q.equalTo('assignee', designer.toPointer());
          q.equalTo('status', '已完成');
        });

        const inProgress = await this.adminData.count('Project', q => {
          q.equalTo('assignee', designer.toPointer());
          q.equalTo('status', '进行中');
        });

        return {
          name: designer.get('name'),
          completed,
          inProgress
        };
      })
    );

    return stats.sort((a, b) => b.completed - a.completed);
  }
}