/**
 * Dummy Orders Analytics Data
 * Structure matches /woonoow/v1/analytics/orders API response
 */

export interface OrdersOverview {
  total_orders: number;
  avg_order_value: number;
  fulfillment_rate: number;
  cancellation_rate: number;
  avg_processing_time: string;
  change_percent: number;
  previous_total_orders: number;
}

export interface OrdersChartData {
  date: string;
  orders: number;
  completed: number;
  processing: number;
  pending: number;
  cancelled: number;
  refunded: number;
  failed: number;
}

export interface OrdersByStatus {
  status: string;
  status_label: string;
  count: number;
  percentage: number;
  color: string;
}

export interface OrdersByHour {
  hour: number;
  orders: number;
}

export interface OrdersByDayOfWeek {
  day: string;
  day_number: number;
  orders: number;
}

export interface OrdersData {
  overview: OrdersOverview;
  chart_data: OrdersChartData[];
  by_status: OrdersByStatus[];
  by_hour: OrdersByHour[];
  by_day_of_week: OrdersByDayOfWeek[];
}

// Generate 30 days of orders data
const generateChartData = (): OrdersChartData[] => {
  const data: OrdersChartData[] = [];
  const today = new Date();
  
  for (let i = 29; i >= 0; i--) {
    const date = new Date(today);
    date.setDate(date.getDate() - i);
    
    const totalOrders = Math.floor(30 + Math.random() * 30);
    const completed = Math.floor(totalOrders * 0.65);
    const processing = Math.floor(totalOrders * 0.18);
    const pending = Math.floor(totalOrders * 0.10);
    const cancelled = Math.floor(totalOrders * 0.04);
    const refunded = Math.floor(totalOrders * 0.02);
    const failed = totalOrders - (completed + processing + pending + cancelled + refunded);
    
    data.push({
      date: date.toISOString().split('T')[0],
      orders: totalOrders,
      completed,
      processing,
      pending,
      cancelled,
      refunded,
      failed: Math.max(0, failed),
    });
  }
  
  return data;
};

// Generate orders by hour (0-23)
const generateByHour = (): OrdersByHour[] => {
  const hours: OrdersByHour[] = [];
  
  for (let hour = 0; hour < 24; hour++) {
    let orders = 0;
    
    // Peak hours: 9-11 AM, 1-3 PM, 7-9 PM
    if ((hour >= 9 && hour <= 11) || (hour >= 13 && hour <= 15) || (hour >= 19 && hour <= 21)) {
      orders = Math.floor(15 + Math.random() * 25);
    } else if (hour >= 6 && hour <= 22) {
      orders = Math.floor(5 + Math.random() * 15);
    } else {
      orders = Math.floor(Math.random() * 5);
    }
    
    hours.push({ hour, orders });
  }
  
  return hours;
};

export const DUMMY_ORDERS_DATA: OrdersData = {
  overview: {
    total_orders: 1242,
    avg_order_value: 277576,
    fulfillment_rate: 94.2,
    cancellation_rate: 3.8,
    avg_processing_time: '2.4 hours',
    change_percent: 12.5,
    previous_total_orders: 1104,
  },
  chart_data: generateChartData(),
  by_status: [
    {
      status: 'completed',
      status_label: 'Completed',
      count: 807,
      percentage: 65.0,
      color: '#10b981',
    },
    {
      status: 'processing',
      status_label: 'Processing',
      count: 224,
      percentage: 18.0,
      color: '#3b82f6',
    },
    {
      status: 'pending',
      status_label: 'Pending',
      count: 124,
      percentage: 10.0,
      color: '#f59e0b',
    },
    {
      status: 'cancelled',
      status_label: 'Cancelled',
      count: 50,
      percentage: 4.0,
      color: '#6b7280',
    },
    {
      status: 'refunded',
      status_label: 'Refunded',
      count: 25,
      percentage: 2.0,
      color: '#ef4444',
    },
    {
      status: 'failed',
      status_label: 'Failed',
      count: 12,
      percentage: 1.0,
      color: '#dc2626',
    },
  ],
  by_hour: generateByHour(),
  by_day_of_week: [
    { day: 'Monday', day_number: 1, orders: 186 },
    { day: 'Tuesday', day_number: 2, orders: 172 },
    { day: 'Wednesday', day_number: 3, orders: 164 },
    { day: 'Thursday', day_number: 4, orders: 178 },
    { day: 'Friday', day_number: 5, orders: 198 },
    { day: 'Saturday', day_number: 6, orders: 212 },
    { day: 'Sunday', day_number: 0, orders: 132 },
  ],
};
