// 商户订单管理服务

interface Order {
  id: string;
  merchantId: string;
  merchantName: string;
  orderId: string;
  platform: string;
  totalAmount: number;
  status: 'PENDING' | 'PROCESSING' | 'SHIPPED' | 'DELIVERED' | 'CANCELLED' | 'REFUNDED';
  createdAt: string;
  updatedAt: string;
}

interface OrderResponse {
  data: Order[];
  total: number;
  page: number;
  pageSize: number;
}

// 模拟数据
const mockOrders: Order[] = [
  {
    id: '1',
    merchantId: '1',
    merchantName: '商户A',
    orderId: 'AMZ123456',
    platform: 'Amazon',
    totalAmount: 100.50,
    status: 'DELIVERED',
    createdAt: '2026-03-01T10:00:00Z',
    updatedAt: '2026-03-03T10:00:00Z',
  },
  {
    id: '2',
    merchantId: '1',
    merchantName: '商户A',
    orderId: 'EBAY789012',
    platform: 'eBay',
    totalAmount: 50.25,
    status: 'SHIPPED',
    createdAt: '2026-03-02T10:00:00Z',
    updatedAt: '2026-03-03T10:00:00Z',
  },
  {
    id: '3',
    merchantId: '2',
    merchantName: '商户B',
    orderId: 'SHOPEE345678',
    platform: 'Shopee',
    totalAmount: 75.75,
    status: 'PROCESSING',
    createdAt: '2026-03-03T10:00:00Z',
    updatedAt: '2026-03-03T10:00:00Z',
  },
  {
    id: '4',
    merchantId: '3',
    merchantName: '商户C',
    orderId: 'TIKTOK901234',
    platform: 'TikTok',
    totalAmount: 120.00,
    status: 'PENDING',
    createdAt: '2026-03-04T10:00:00Z',
    updatedAt: '2026-03-04T10:00:00Z',
  },
  {
    id: '5',
    merchantId: '1',
    merchantName: '商户A',
    orderId: 'AMZ567890',
    platform: 'Amazon',
    totalAmount: 200.00,
    status: 'CANCELLED',
    createdAt: '2026-03-05T10:00:00Z',
    updatedAt: '2026-03-05T10:00:00Z',
  },
];

// 获取商户订单列表
export const getMerchantOrders = async (): Promise<OrderResponse> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  return {
    data: mockOrders,
    total: mockOrders.length,
    page: 1,
    pageSize: 10,
  };
};

// 根据商户ID获取订单列表
export const getOrdersByMerchantId = async (merchantId: string): Promise<Order[]> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  return mockOrders.filter(o => o.merchantId === merchantId);
};

// 更新订单状态
export const updateOrderStatus = async (orderId: string, status: Order['status']): Promise<Order> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const index = mockOrders.findIndex(o => o.id === orderId);
  if (index === -1) {
    throw new Error('订单不存在');
  }
  mockOrders[index] = {
    ...mockOrders[index],
    status,
    updatedAt: new Date().toISOString(),
  };
  return mockOrders[index];
};

// 获取订单详情
export const getOrderById = async (orderId: string): Promise<Order> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const order = mockOrders.find(o => o.id === orderId);
  if (!order) {
    throw new Error('订单不存在');
  }
  return order;
};

// 搜索订单
export const searchOrders = async (keyword: string): Promise<Order[]> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  return mockOrders.filter(o => 
    o.orderId.includes(keyword) || 
    o.merchantName.toLowerCase().includes(keyword.toLowerCase())
  );
};