/**
 * [MOCK-REPORTS] Reports模块DataSource
 */

export interface ReportData {
  id: string;
  type: 'performance' | 'profit' | 'sales' | 'inventory';
  period: string;
  generatedAt: string;
  data: any;
}

export interface PerformanceMetric {
  date: string;
  orders: number;
  revenue: number;
  profit: number;
  profitMargin: number;
  avgOrderValue: number;
}

export interface ProfitReport {
  category: string;
  revenue: number;
  cost: number;
  profit: number;
  margin: number;
  trend: 'up' | 'down' | 'stable';
}

export interface IReportsDataSource {
  fetchPerformanceReport(params: { startDate: string; endDate: string }): Promise<PerformanceMetric[]>;
  fetchProfitReport(params: { startDate: string; endDate: string }): Promise<ProfitReport[]>;
  fetchSalesReport(params: { startDate: string; endDate: string }): Promise<any>;
  exportReport(type: string, params: any): Promise<Blob>;
}

class MockReportsDataSource implements IReportsDataSource {
  async fetchPerformanceReport(params: { startDate: string; endDate: string }): Promise<PerformanceMetric[]> {
    return [
      { date: '2026-03-12', orders: 45, revenue: 8500, profit: 2125, profitMargin: 25, avgOrderValue: 188.89 },
      { date: '2026-03-13', orders: 52, revenue: 9200, profit: 2300, profitMargin: 25, avgOrderValue: 176.92 },
      { date: '2026-03-14', orders: 38, revenue: 7100, profit: 1775, profitMargin: 25, avgOrderValue: 186.84 },
      { date: '2026-03-15', orders: 61, revenue: 11500, profit: 2875, profitMargin: 25, avgOrderValue: 188.52 },
      { date: '2026-03-16', orders: 55, revenue: 10200, profit: 2550, profitMargin: 25, avgOrderValue: 185.45 },
      { date: '2026-03-17', orders: 48, revenue: 9100, profit: 2275, profitMargin: 25, avgOrderValue: 189.58 },
      { date: '2026-03-18', orders: 67, revenue: 12800, profit: 3200, profitMargin: 25, avgOrderValue: 191.04 },
    ];
  }

  async fetchProfitReport(params: { startDate: string; endDate: string }): Promise<ProfitReport[]> {
    return [
      { category: 'Electronics', revenue: 45000, cost: 31500, profit: 13500, margin: 30, trend: 'up' },
      { category: 'Accessories', revenue: 28000, cost: 19600, profit: 8400, margin: 30, trend: 'stable' },
      { category: 'Home & Garden', revenue: 15000, cost: 11250, profit: 3750, margin: 25, trend: 'down' },
      { category: 'Sports', revenue: 12000, cost: 9000, profit: 3000, margin: 25, trend: 'up' },
      { category: 'Clothing', revenue: 8000, cost: 6400, profit: 1600, margin: 20, trend: 'stable' },
    ];
  }

  async fetchSalesReport(params: { startDate: string; endDate: string }): Promise<any> {
    return {
      totalSales: 108000,
      totalOrders: 366,
      avgOrderValue: 295.08,
      topProducts: [
        { name: 'Wireless Headphones', sales: 15000, quantity: 300 },
        { name: 'USB-C Cable', sales: 8000, quantity: 1600 },
        { name: 'Phone Case', sales: 6000, quantity: 400 },
      ],
    };
  }

  async exportReport(type: string, params: any): Promise<Blob> {
    return new Blob(['report content'], { type: 'text/csv' });
  }
}

class ApiReportsDataSource implements IReportsDataSource {
  private baseUrl = '/api/reports';

  async fetchPerformanceReport(params: { startDate: string; endDate: string }): Promise<PerformanceMetric[]> {
    const res = await fetch(`${this.baseUrl}/performance?${new URLSearchParams(params)}`);
    return res.json();
  }

  async fetchProfitReport(params: { startDate: string; endDate: string }): Promise<ProfitReport[]> {
    const res = await fetch(`${this.baseUrl}/profit?${new URLSearchParams(params)}`);
    return res.json();
  }

  async fetchSalesReport(params: { startDate: string; endDate: string }): Promise<any> {
    const res = await fetch(`${this.baseUrl}/sales?${new URLSearchParams(params)}`);
    return res.json();
  }

  async exportReport(type: string, params: any): Promise<Blob> {
    const res = await fetch(`${this.baseUrl}/export`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ type, ...params }) });
    return res.blob();
  }
}

const useMock = process.env.REACT_APP_USE_MOCK === 'true';
export const reportsDataSource: IReportsDataSource = useMock ? new MockReportsDataSource() : new ApiReportsDataSource();
