/**
 * [MOCK-013] 仪表盘模块DataSource
 * AI注意: 这是Mock实现，不是真实业务逻辑
 * 仅在USE_MOCK=true时启用 */

import { BaseDataSource, DataSourceFactory } from './baseDataSource';

export interface KPIData {
  title: string;
  value: string;
  change: number;
  status: 'up' | 'down';
  icon: React.ReactNode;
}

export interface TrendData {
  date: string;
  sales: number;
  orders: number;
}

export interface DashboardDataSource {
  getDashboardData(params: {
    shop: string;
    startDate?: string;
    endDate?: string;
    export?: boolean;
  }): Promise<{
    kpiData: KPIData[];
    trendData: TrendData[];
  }>;
  getTopProducts(limit?: number): Promise<Array<{
    id: string;
    name: string;
    sales: number;
    quantity: number;
  }>>;
  getRecentOrders(limit?: number): Promise<Array<{
    id: string;
    customer: string;
    amount: number;
    status: string;
    date: string;
  }>>;
}

export class DashboardDataSourceImpl extends BaseDataSource implements DashboardDataSource {
  async getDashboardData(params: {
    shop: string;
    startDate?: string;
    endDate?: string;
    export?: boolean;
  }): Promise<{
    kpiData: KPIData[];
    trendData: TrendData[];
  }> {
    try {
      // 模拟API延迟
      await this.delay(500);
      
      // 模拟数据
      const kpiData: KPIData[] = [
        {
          title: 'Total Sales',
          value: '¥128,500',
          change: 12.5,
          status: 'up',
          icon: null, // 图标将在组件中处理
        },
        {
          title: 'Orders',
          value: '2,850',
          change: 8.2,
          status: 'up',
          icon: null,
        },
        {
          title: 'Conversion',
          value: '4.8%',
          change: -1.2,
          status: 'down',
          icon: null,
        },
        {
          title: 'Avg Order Value',
          value: '¥45.1',
          change: 3.5,
          status: 'up',
          icon: null,
        },
      ];
      
      const trendData: TrendData[] = [
        { date: '1/1', sales: 12000, orders: 350 },
        { date: '1/2', sales: 15000, orders: 420 },
        { date: '1/3', sales: 18000, orders: 480 },
        { date: '1/4', sales: 16000, orders: 430 },
        { date: '1/5', sales: 20000, orders: 520 },
        { date: '1/6', sales: 22000, orders: 550 },
        { date: '1/7', sales: 25000, orders: 600 },
      ];
      
      return { kpiData, trendData };
    } catch (error) {
      return this.handleError(error, {
        kpiData: [],
        trendData: []
      });
    }
  }
  
  async getTopProducts(limit: number = 5): Promise<Array<{
    id: string;
    name: string;
    sales: number;
    quantity: number;
  }>> {
    try {
      // 模拟API延迟
      await this.delay(300);
      
      // 模拟数据
      return [
        { id: '1', name: 'Product A', sales: 15000, quantity: 300 },
        { id: '2', name: 'Product B', sales: 12000, quantity: 250 },
        { id: '3', name: 'Product C', sales: 10000, quantity: 200 },
        { id: '4', name: 'Product D', sales: 8000, quantity: 160 },
        { id: '5', name: 'Product E', sales: 6000, quantity: 120 },
      ].slice(0, limit);
    } catch (error) {
      return this.handleError(error, []);
    }
  }
  
  async getRecentOrders(limit: number = 5): Promise<Array<{
    id: string;
    customer: string;
    amount: number;
    status: string;
    date: string;
  }>> {
    try {
      // 模拟API延迟
      await this.delay(300);
      
      // 模拟数据
      return [
        { id: '1001', customer: 'Customer A', amount: 299, status: 'Completed', date: '2024-01-01' },
        { id: '1002', customer: 'Customer B', amount: 199, status: 'Pending', date: '2024-01-01' },
        { id: '1003', customer: 'Customer C', amount: 399, status: 'Completed', date: '2023-12-31' },
        { id: '1004', customer: 'Customer D', amount: 499, status: 'Processing', date: '2023-12-31' },
        { id: '1005', customer: 'Customer E', amount: 149, status: 'Completed', date: '2023-12-30' },
      ].slice(0, limit);
    } catch (error) {
      return this.handleError(error, []);
    }
  }
}

// 创建数据源实例
const dashboardDataSourceInstance = new DashboardDataSourceImpl();

// 注册到数据源工厂
DataSourceFactory.register('dashboard', dashboardDataSourceInstance);

export const dashboardDataSource = dashboardDataSourceInstance;