// [MOCK] 产品选择模拟数据
import { ProductSelection } from '@/types/productSelection';

// 扩展类型定义
export interface ProductPool {
  id: string;
  name: string;
  category: string;
  platform: string;
  roi: number;
  selection_score: number;
  competition_level: 'LOW' | 'MEDIUM' | 'HIGH';
  trend: 'UP' | 'DOWN' | 'STABLE';
  selected_at?: string;
  image_url?: string;
  product_id?: string;
  tags?: string[];
  price?: number;
  cost_price?: number;
  profit?: number;
  sales_volume?: number;
  rating?: number;
  review_count?: number;
  source_platform?: string;
}

export interface SelectionRule {
  id: string;
  name: string;
  description: string;
  category?: string;
  criteria: any;
  enabled: boolean;
  selected_count: number;
  last_run_at?: string;
}

export interface ListingTask {
  id: string;
  product_id: string;
  status: 'PENDING' | 'PROCESSING' | 'COMPLETED' | 'FAILED';
  progress: number;
  created_at: string;
  updated_at: string;
}

export interface AutoListingConfig {
  enabled: boolean;
  max_tasks_per_day: number;
  min_roi: number;
  max_competition: 'LOW' | 'MEDIUM' | 'HIGH';
  categories: string[];
  batch_size?: number;
  interval_hours?: number;
  target_platforms?: string[];
  auto_pricing?: boolean;
  auto_inventory?: boolean;
  auto_description?: boolean;
  auto_images?: boolean;
}

// 常量
export const MOCK_CATEGORIES = ['electronics', 'home', 'fashion', 'beauty', 'sports'];
export const MOCK_PLATFORMS = ['Amazon', 'eBay', 'AliExpress', 'Wish', 'Shopee'];

// 模拟数据
export const mockProductPool: ProductPool[] = [
  {
    id: '1',
    name: 'Smartphone X',
    category: 'electronics',
    platform: 'Amazon',
    roi: 35.5,
    selection_score: 9.2,
    competition_level: 'MEDIUM',
    trend: 'UP',
    selected_at: new Date().toISOString()
  },
  {
    id: '2',
    name: 'Wireless Earbuds',
    category: 'electronics',
    platform: 'eBay',
    roi: 42.1,
    selection_score: 8.7,
    competition_level: 'LOW',
    trend: 'UP'
  },
  {
    id: '3',
    name: 'Fitness Tracker',
    category: 'sports',
    platform: 'AliExpress',
    roi: 28.3,
    selection_score: 7.9,
    competition_level: 'HIGH',
    trend: 'STABLE'
  }
];

export const mockSelectionRules: SelectionRule[] = [
  {
    id: '1',
    name: '高ROI产品',
    description: 'ROI大于30%的产品',
    criteria: { min_roi: 30 },
    enabled: true,
    selected_count: 15,
    last_run_at: new Date().toISOString()
  },
  {
    id: '2',
    name: '低竞争产品',
    description: '竞争度低的产品',
    criteria: { competition_level: 'LOW' },
    enabled: false,
    selected_count: 8
  }
];

export const mockListingTasks: ListingTask[] = [
  {
    id: '1',
    product_id: '1',
    status: 'COMPLETED',
    progress: 100,
    created_at: new Date().toISOString(),
    updated_at: new Date().toISOString()
  },
  {
    id: '2',
    product_id: '2',
    status: 'PENDING',
    progress: 0,
    created_at: new Date().toISOString(),
    updated_at: new Date().toISOString()
  }
];

export const mockAutoListingConfig: AutoListingConfig = {
  enabled: true,
  max_tasks_per_day: 50,
  min_roi: 25,
  max_competition: 'MEDIUM',
  categories: ['electronics', 'home']
};

// 模拟函数
export const getMockProductPool = (params?: any): ProductPool[] => {
  let result = [...mockProductPool];
  if (params) {
    if (params.category) {
      result = result.filter(p => p.category === params.category);
    }
    if (params.min_roi) {
      result = result.filter(p => p.roi >= params.min_roi);
    }
    if (params.max_roi) {
      result = result.filter(p => p.roi <= params.max_roi);
    }
    if (params.competition_level) {
      result = result.filter(p => p.competition_level === params.competition_level);
    }
    if (params.trend) {
      result = result.filter(p => p.trend === params.trend);
    }
    if (params.keyword) {
      const keyword = params.keyword.toLowerCase();
      result = result.filter(p => p.name.toLowerCase().includes(keyword));
    }
  }
  return result;
};

export const getMockSelectionRules = (enabled?: boolean): SelectionRule[] => {
  if (enabled !== undefined) {
    return mockSelectionRules.filter(rule => rule.enabled === enabled);
  }
  return [...mockSelectionRules];
};

export const getMockListingTasks = (status?: string): ListingTask[] => {
  if (status) {
    return mockListingTasks.filter(task => task.status === status);
  }
  return [...mockListingTasks];
};

export const createMockSelectionRule = (data: Partial<SelectionRule>): SelectionRule => {
  const newRule: SelectionRule = {
    id: `NEW-${Date.now()}`,
    name: data.name || '新规则',
    description: data.description || '',
    criteria: data.criteria || {},
    enabled: data.enabled ?? true,
    selected_count: 0
  };
  mockSelectionRules.push(newRule);
  return newRule;
};

export const updateMockSelectionRule = (id: string, data: Partial<SelectionRule>): SelectionRule | undefined => {
  const index = mockSelectionRules.findIndex(rule => rule.id === id);
  if (index === -1) return undefined;
  
  mockSelectionRules[index] = {
    ...mockSelectionRules[index],
    ...data
  };
  return mockSelectionRules[index];
};

export const deleteMockSelectionRule = (id: string): void => {
  const index = mockSelectionRules.findIndex(rule => rule.id === id);
  if (index !== -1) {
    mockSelectionRules.splice(index, 1);
  }
};

export const createMockListingTask = (data: Partial<ListingTask>): ListingTask => {
  const newTask: ListingTask = {
    id: `NEW-${Date.now()}`,
    product_id: data.product_id || '',
    status: 'PENDING',
    progress: 0,
    created_at: new Date().toISOString(),
    updated_at: new Date().toISOString()
  };
  mockListingTasks.push(newTask);
  return newTask;
};

export const updateMockListingTask = (id: string, data: Partial<ListingTask>): ListingTask | undefined => {
  const index = mockListingTasks.findIndex(task => task.id === id);
  if (index === -1) return undefined;
  
  mockListingTasks[index] = {
    ...mockListingTasks[index],
    ...data,
    updated_at: new Date().toISOString()
  };
  return mockListingTasks[index];
};

export const deleteMockListingTask = (id: string): void => {
  const index = mockListingTasks.findIndex(task => task.id === id);
  if (index !== -1) {
    mockListingTasks.splice(index, 1);
  }
};

export const getMockAutoListingConfig = (): AutoListingConfig => {
  return { ...mockAutoListingConfig };
};

export const updateMockAutoListingConfig = (data: Partial<AutoListingConfig>): AutoListingConfig => {
  Object.assign(mockAutoListingConfig, data);
  return mockAutoListingConfig;
};

// 原有导出保持不变
export const mockProductSelections: ProductSelection[] = [
  {
    id: '1',
    name: '热销产品组合',
    description: '基于市场热度选择的产品组合',
    products: ['PROD001', 'PROD002', 'PROD003'],
    criteria: {
      minSales: 100,
      maxPrice: 500,
      categories: ['electronics', 'home'],
    },
    createdAt: new Date('2024-01-01').toISOString(),
    updatedAt: new Date('2024-01-01').toISOString(),
  },
  {
    id: '2',
    name: '高利润产品',
    description: '利润率高于30%的产品',
    products: ['PROD004', 'PROD005'],
    criteria: {
      minProfitMargin: 30,
      categories: ['electronics'],
    },
    createdAt: new Date('2024-01-02').toISOString(),
    updatedAt: new Date('2024-01-02').toISOString(),
  },
  {
    id: '3',
    name: '新品推荐',
    description: '最近30天内上架的产品',
    products: ['PROD006', 'PROD007', 'PROD008'],
    criteria: {
      maxDaysSinceAdded: 30,
      categories: ['fashion', 'beauty'],
    },
    createdAt: new Date('2024-01-03').toISOString(),
    updatedAt: new Date('2024-01-03').toISOString(),
  },
];

export const mockProductSelectionDetail = (id: string): ProductSelection | undefined => {
  return mockProductSelections.find(item => item.id === id);
};

export const mockCreateProductSelection = (data: Partial<ProductSelection>): ProductSelection => {
  const newItem: ProductSelection = {
    id: `NEW-${Date.now()}`,
    name: data.name || '新选择',
    description: data.description || '',
    products: data.products || [],
    criteria: data.criteria || {},
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  };
  mockProductSelections.push(newItem);
  return newItem;
};

export const mockUpdateProductSelection = (id: string, data: Partial<ProductSelection>): ProductSelection | undefined => {
  const index = mockProductSelections.findIndex(item => item.id === id);
  if (index === -1) return undefined;
  
  mockProductSelections[index] = {
    ...mockProductSelections[index],
    ...data,
    updatedAt: new Date().toISOString(),
  };
  return mockProductSelections[index];
};

export const mockDeleteProductSelection = (id: string): boolean => {
  const index = mockProductSelections.findIndex(item => item.id === id);
  if (index === -1) return false;
  mockProductSelections.splice(index, 1);
  return true;
};